24
24
25
25
import dev .galacticraft .machinelib .api .block .entity .RecipeMachineBlockEntity ;
26
26
import dev .galacticraft .machinelib .api .gametest .annotation .MachineTest ;
27
+ import dev .galacticraft .machinelib .api .gametest .recipe .IngredientSupplier ;
27
28
import dev .galacticraft .machinelib .api .machine .MachineType ;
28
29
import dev .galacticraft .machinelib .api .storage .MachineItemStorage ;
29
30
import net .minecraft .gametest .framework .GameTestAssertException ;
30
31
import net .minecraft .gametest .framework .GameTestGenerator ;
32
+ import net .minecraft .gametest .framework .GameTestHelper ;
31
33
import net .minecraft .gametest .framework .TestFunction ;
32
34
import net .minecraft .world .Container ;
33
35
import net .minecraft .world .item .Items ;
47
49
* @see RecipeMachineBlockEntity
48
50
*/
49
51
public abstract class RecipeGameTest <C extends Container , R extends Recipe <C >, Machine extends RecipeMachineBlockEntity <C , R >> extends MachineGameTest <Machine > {
50
- private final int inputSlotsStart ;
51
- private final int inputSlotsLength ;
52
52
private final int outputSlotsStart ;
53
53
private final int outputSlotsLength ;
54
+ protected final int recipeRuntime ;
54
55
55
- protected RecipeGameTest (@ NotNull MachineType <Machine , ?> type , int inputSlot , int outputSlot ) {
56
- this (type , inputSlot , 1 , outputSlot );
56
+ private final List <IngredientSupplier <C , R , Machine >> conditions ;
57
+
58
+ protected RecipeGameTest (@ NotNull MachineType <Machine , ?> type , List <IngredientSupplier <C , R , Machine >> conditions , int recipeRuntime ) {
59
+ this (type , conditions , -1 , 0 , recipeRuntime );
57
60
}
58
61
59
- protected RecipeGameTest (@ NotNull MachineType <Machine , ?> type , int inputSlotsStart , int inputSlotsLength , int outputSlot ) {
60
- this (type , inputSlotsStart , inputSlotsLength , outputSlot , 1 );
62
+ protected RecipeGameTest (@ NotNull MachineType <Machine , ?> type , List < IngredientSupplier < C , R , Machine >> conditions , int outputSlot , int recipeRuntime ) {
63
+ this (type , conditions , outputSlot , 1 , recipeRuntime );
61
64
}
62
65
63
- protected RecipeGameTest (@ NotNull MachineType <Machine , ?> type , int inputSlotsStart , int inputSlotsLength , int outputSlotsStart , int outputSlotsLength ) {
66
+ protected RecipeGameTest (@ NotNull MachineType <Machine , ?> type , List < IngredientSupplier < C , R , Machine >> conditions , int outputSlotsStart , int outputSlotsLength , int recipeRuntime ) {
64
67
super (type );
65
68
66
- this .inputSlotsStart = inputSlotsStart ;
67
- this .inputSlotsLength = inputSlotsLength ;
68
69
this .outputSlotsStart = outputSlotsStart ;
69
70
this .outputSlotsLength = outputSlotsLength ;
71
+ this .recipeRuntime = recipeRuntime ;
72
+ this .conditions = conditions ;
70
73
}
71
74
72
- protected abstract void fulfillRunRequirements (@ NotNull Machine machine );
73
- protected abstract int getRecipeRuntime ();
74
- protected abstract void createValidRecipe (@ NotNull MachineItemStorage storage );
75
-
76
75
protected boolean anyRecipeCrafted (@ NotNull MachineItemStorage storage ) {
77
76
return !storage .isEmpty (this .outputSlotsStart , this .outputSlotsLength );
78
77
}
79
78
80
- protected void createInvalidRecipe (@ NotNull MachineItemStorage storage ) {
81
- for (int i = 0 ; i < this .inputSlotsLength ; i ++) {
82
- storage .getSlot (this .inputSlotsStart + i ).set (Items .BARRIER , 1 );
83
- }
84
- }
85
-
86
79
protected void fillOutputSlots (@ NotNull MachineItemStorage storage ) {
87
80
for (int i = 0 ; i < this .outputSlotsLength ; i ++) {
88
81
storage .getSlot (this .outputSlotsStart + i ).set (Items .BARRIER , 1 );
89
82
}
90
83
}
91
84
92
- @ MachineTest (group = "recipe" , workTime = 1 )
85
+ protected void fulfillRunConditions (Machine machine ) {
86
+ for (IngredientSupplier <C , R , Machine > condition : this .conditions ) {
87
+ condition .fulfillRunRequirements (machine );
88
+ }
89
+ }
90
+
91
+ @ MachineTest (group = "recipe" )
93
92
public Runnable initialize (Machine machine ) {
94
- this .fulfillRunRequirements (machine );
95
- this .createValidRecipe (machine .itemStorage ());
93
+ this .fulfillRunConditions (machine );
96
94
return () -> {
97
95
if (machine .getActiveRecipe () == null ) {
98
96
throw new GameTestAssertException ("Failed to find recipe!" );
99
97
}
100
98
};
101
99
}
102
100
103
- @ MachineTest (group = "recipe" , workTime = 1 )
104
- public Runnable invalid (Machine machine ) {
105
- this .fulfillRunRequirements (machine );
106
- this .createInvalidRecipe (machine .itemStorage ());
107
- return () -> {
108
- if (machine .getActiveRecipe () != null ) {
109
- throw new GameTestAssertException ("Crafting something despite recipe being invalid!" );
110
- }
111
- };
112
- }
113
-
114
- @ MachineTest (group = "recipe" , workTime = 1 )
101
+ @ MachineTest (group = "recipe" )
115
102
public Runnable full (Machine machine ) {
116
- this .fulfillRunRequirements (machine );
103
+ this .fulfillRunConditions (machine );
117
104
this .fillOutputSlots (machine .itemStorage ());
118
- this . createValidRecipe ( machine . itemStorage ());
105
+
119
106
return () -> {
120
107
if (machine .getActiveRecipe () != null ) {
121
108
throw new GameTestAssertException ("Crafting something despite the output being full!" );
122
109
}
123
110
};
124
111
}
125
112
126
- @ MachineTest (group = "recipe" , workTime = 1 )
127
- public Runnable craft (Machine machine ) {
128
- this .fulfillRunRequirements (machine );
129
- this .fillOutputSlots (machine .itemStorage ());
130
- this .createValidRecipe (machine .itemStorage ());
131
- return () -> {
132
- if (machine .getActiveRecipe () != null ) {
133
- throw new GameTestAssertException ("Crafting something despite the output being full!" );
113
+ protected void tryCraft (GameTestHelper helper ) {
114
+ Machine machine = createMachine (helper );
115
+
116
+ fulfillRunConditions (machine );
117
+
118
+ helper .runAfterDelay (this .recipeRuntime , () -> {
119
+ if (!this .anyRecipeCrafted (machine .itemStorage ())) {
120
+ helper .fail ("Failed to craft recipe!" , machine .getBlockPos ());
121
+ } else {
122
+ helper .succeed ();
134
123
}
135
- };
124
+ }) ;
136
125
}
137
126
138
- // @InstantTest(group = "recipe")
139
- // public Runnable recipeNoResources(Machine machine) {
140
- // this.createValidRecipe(machine.itemStorage());
141
- // return () -> {
142
- // if (machine.getMaxProgress() != 0) { //fixme: recipe detection works differently
143
- // throw new GameTestAssertException("Machine is crafting a recipe, despite not having relevant resources (e.g. energy)!");
144
- // }
145
- // };
146
- // }
147
-
148
- @ MachineTest (group = "recipe" , workTime = 1 )
149
- public Runnable resourcesNoRecipe (Machine machine ) {
150
- this .fulfillRunRequirements (machine );
151
- return () -> {
152
- if (machine .getActiveRecipe () != null ) {
153
- throw new GameTestAssertException ("Machine is doing something, despite not having a recipe!" );
127
+ protected void tryCraftPartial (GameTestHelper helper , int ignored ) {
128
+ Machine machine = createMachine (helper );
129
+
130
+ for (int i = 0 ; i < this .conditions .size (); i ++) {
131
+ if (i == ignored ) continue ;
132
+ this .conditions .get (i ).fulfillRunRequirements (machine );
133
+ }
134
+
135
+ helper .runAfterDelay (this .recipeRuntime , () -> {
136
+ if (this .anyRecipeCrafted (machine .itemStorage ())) {
137
+ helper .fail ("Partial recipe %s crafted!" .formatted (ignored ), machine .getBlockPos ());
138
+ } else {
139
+ helper .succeed ();
154
140
}
155
- };
141
+ }) ;
156
142
}
157
143
158
144
@ Override
@@ -161,18 +147,12 @@ public Runnable resourcesNoRecipe(Machine machine) {
161
147
public @ NotNull List <TestFunction > registerTests () {
162
148
List <TestFunction > tests = super .registerTests ();
163
149
// variable runtime
164
- tests .add (this .createTest ("recipe" , "craft" , STRUCTURE_3x3 , this .getRecipeRuntime (), 1 , helper -> {
165
- Machine machine = createMachine (helper );
166
- this .fulfillRunRequirements (machine );
167
- this .createValidRecipe (machine .itemStorage ());
168
- helper .runAfterDelay (this .getRecipeRuntime (), () -> {
169
- if (!this .anyRecipeCrafted (machine .itemStorage ())) {
170
- helper .fail ("Failed to craft recipe!" , machine .getBlockPos ());
171
- } else {
172
- helper .succeed ();
173
- }
174
- });
175
- }));
150
+ tests .add (this .createTest ("recipe" , "craft" , STRUCTURE_3x3 , this .recipeRuntime , 1 , this ::tryCraft ));
151
+
152
+ for (int i = 0 ; i < this .conditions .size (); i ++) {
153
+ int finalI = i ;
154
+ tests .add (this .createTest ("recipe" , "craft.partial." + i , STRUCTURE_3x3 , this .recipeRuntime , 1 , helper -> this .tryCraftPartial (helper , finalI )));
155
+ }
176
156
return tests ;
177
157
}
178
158
}
0 commit comments