1+
2+ package mekanism .common .tests .component ;
3+
4+ import com .google .common .collect .Sets ;
5+ import java .util .Collection ;
6+ import java .util .EnumSet ;
7+ import java .util .Map .Entry ;
8+ import java .util .Set ;
9+ import java .util .stream .Collectors ;
10+ import mekanism .api .RelativeSide ;
11+ import mekanism .common .attachments .component .AttachedSideConfig ;
12+ import mekanism .common .attachments .component .AttachedSideConfig .LightConfigInfo ;
13+ import mekanism .common .block .interfaces .IHasTileEntity ;
14+ import mekanism .common .lib .transmitter .TransmissionType ;
15+ import mekanism .common .registries .MekanismBlocks ;
16+ import mekanism .common .registries .MekanismDataComponents ;
17+ import mekanism .common .tests .helpers .MekGameTestHelper ;
18+ import mekanism .common .tile .component .TileComponentConfig ;
19+ import mekanism .common .tile .component .config .ConfigInfo ;
20+ import mekanism .common .tile .prefab .TileEntityConfigurableMachine ;
21+ import mekanism .generators .common .registries .GeneratorsBlocks ;
22+ import net .minecraft .core .BlockPos ;
23+ import net .minecraft .gametest .framework .GameTest ;
24+ import net .minecraft .world .item .ItemStack ;
25+ import net .minecraft .world .level .block .Block ;
26+ import net .neoforged .neoforge .registries .DeferredHolder ;
27+ import net .neoforged .testframework .annotation .ForEachTest ;
28+ import net .neoforged .testframework .annotation .TestHolder ;
29+ import net .neoforged .testframework .gametest .EmptyTemplate ;
30+
31+ @ ForEachTest (groups = "component.item" )
32+ public class ItemPropertiesTest {
33+
34+ @ GameTest
35+ @ EmptyTemplate
36+ @ TestHolder (description = "Validate we don't have any transmission types missing from side configs, and that we don't define a side for ones we are disabled on." )
37+ public static void validateSideConfigs (final MekGameTestHelper helper ) {
38+ helper .succeedIf (() -> {
39+ validateSideConfigs (helper , MekanismBlocks .BLOCKS .getPrimaryEntries ());
40+ validateSideConfigs (helper , GeneratorsBlocks .BLOCKS .getPrimaryEntries ());
41+ });
42+ }
43+
44+ private static void validateSideConfigs (MekGameTestHelper helper , Collection <DeferredHolder <Block , ? extends Block >> blocks ) {
45+ BlockPos center = helper .absolutePos (new BlockPos (0 , 2 , 0 ));
46+ for (DeferredHolder <Block , ? extends Block > holder : blocks ) {
47+ Block block = holder .get ();
48+ ItemStack stack = new ItemStack (block );
49+ if (!stack .isEmpty () && block instanceof IHasTileEntity <?> hasTileEntity &&
50+ //TODO: Would we rather actually place the block?
51+ hasTileEntity .newBlockEntity (center , block .defaultBlockState ()) instanceof TileEntityConfigurableMachine configurable ) {
52+ if (stack .get (MekanismDataComponents .EJECTOR ) == null ) {
53+ helper .fail ("Block " + holder .getId () + " is missing a default ejector data component" );
54+ }
55+ AttachedSideConfig defaultConfig = stack .get (MekanismDataComponents .SIDE_CONFIG );
56+ if (defaultConfig == null ) {
57+ helper .fail ("Block " + holder .getId () + " is missing a default side config data component" );
58+ } else {
59+ TileComponentConfig config = configurable .getConfig ();
60+ Set <TransmissionType > keys = defaultConfig .configInfo ().keySet ();
61+ Set <TransmissionType > transmissionTypes = EnumSet .copyOf (config .getTransmissions ());
62+ if (!keys .containsAll (transmissionTypes )) {
63+ helper .fail ("Block " + holder .getId () + " is missing side configs for the following transmission types: " +
64+ Sets .difference (transmissionTypes , keys ).stream ().map (TransmissionType ::getTransmission ).collect (Collectors .joining (", " )));
65+ } else if (keys .size () != transmissionTypes .size ()) {
66+ helper .fail ("Block " + holder .getId () + " has side configs for the following transmission types, but the BE doesn't: " +
67+ Sets .difference (keys , transmissionTypes ).stream ().map (TransmissionType ::getTransmission ).collect (Collectors .joining (", " )));
68+ } else {
69+ for (Entry <TransmissionType , LightConfigInfo > entry : defaultConfig .configInfo ().entrySet ()) {
70+ TransmissionType type = entry .getKey ();
71+ ConfigInfo info = config .getConfig (type );
72+ helper .assertTrue (info != null , "Config " + type .getTransmission () + " cannot be null" );
73+ for (RelativeSide side : entry .getValue ().sideConfig ().keySet ()) {
74+ if (!info .isSideEnabled (side )) {
75+ helper .fail ("Block " + holder .getId () + " has side config for type: " + type .getTransmission () + " on side: " +
76+ side .name () + ", but the BE has side configs disabled on that side." );
77+ }
78+ }
79+ }
80+ }
81+ }
82+ } else if (!stack .isEmpty ()) {
83+ if (stack .get (MekanismDataComponents .EJECTOR ) != null ) {
84+ helper .fail ("Block " + holder .getId () + " is has a default ejector data component, but the tile doesn't" );
85+ }
86+ if (stack .get (MekanismDataComponents .SIDE_CONFIG ) != null ) {
87+ helper .fail ("Block " + holder .getId () + " is has a default side config data component, but the tile doesn't" );
88+ }
89+ }
90+ }
91+ }
92+ }
0 commit comments