-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathRecipeLogicTest.java
More file actions
243 lines (203 loc) · 11.9 KB
/
RecipeLogicTest.java
File metadata and controls
243 lines (203 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.gregtechceu.gtceu.api.machine.trait;
import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.GTValues;
import com.gregtechceu.gtceu.api.machine.multiblock.WorkableMultiblockMachine;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
import com.gregtechceu.gtceu.common.machine.multiblock.part.ItemBusPartMachine;
import com.gregtechceu.gtceu.gametest.util.TestUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.gametest.framework.BeforeBatch;
import net.minecraft.gametest.framework.GameTest;
import net.minecraft.gametest.framework.GameTestHelper;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.gametest.GameTestHolder;
import net.minecraftforge.gametest.PrefixGameTestTemplate;
import static com.gregtechceu.gtceu.gametest.util.TestUtils.getMetaMachine;
@PrefixGameTestTemplate(false)
@GameTestHolder(GTCEu.MOD_ID)
public class RecipeLogicTest {
private static GTRecipeType LCR_RECIPE_TYPE;
private static GTRecipeType CR_RECIPE_TYPE;
@BeforeBatch(batch = "RecipeLogic")
public static void prepare(ServerLevel level) {
LCR_RECIPE_TYPE = TestUtils.createRecipeType("recipe_logic_test_lcr");
CR_RECIPE_TYPE = TestUtils.createRecipeType("recipe_logic_test_cr");
LCR_RECIPE_TYPE.getLookup().addRecipe(LCR_RECIPE_TYPE
.recipeBuilder(GTCEu.id("test_multiblock_recipelogic"))
.inputItems(new ItemStack(Blocks.COBBLESTONE))
.outputItems(new ItemStack(Blocks.STONE))
.EUt(GTValues.VA[GTValues.HV]).duration(1)
.buildRawRecipe());
LCR_RECIPE_TYPE.getLookup().addRecipe(LCR_RECIPE_TYPE
.recipeBuilder(GTCEu.id("test_multiblock_recipelogic_16_items"))
.inputItems(new ItemStack(Blocks.STONE, 16))
.outputItems(new ItemStack(Blocks.STONE))
.EUt(GTValues.VA[GTValues.HV]).duration(1)
.buildRawRecipe());
CR_RECIPE_TYPE.getLookup().addRecipe(CR_RECIPE_TYPE
.recipeBuilder(GTCEu.id("test_singleblock_recipelogic"))
.inputItems(new ItemStack(Blocks.COBBLESTONE))
.outputItems(new ItemStack(Blocks.STONE))
.EUt(GTValues.VA[GTValues.HV]).duration(1)
.buildRawRecipe());
}
private record BusHolder(ItemBusPartMachine inputBus1, ItemBusPartMachine inputBus2, ItemBusPartMachine outputBus1,
WorkableMultiblockMachine controller) {}
/**
* Retrieves the busses for this specific template and force a multiblock structure check
*
* @param helper the GameTestHelper
* @return the busses, in the BusHolder record.
*/
private static RecipeLogicTest.BusHolder getBussesAndForm(GameTestHelper helper) {
WorkableMultiblockMachine controller = (WorkableMultiblockMachine) getMetaMachine(
helper.getBlockEntity(new BlockPos(1, 2, 0)));
TestUtils.formMultiblock(controller);
controller.setRecipeType(LCR_RECIPE_TYPE);
ItemBusPartMachine inputBus1 = (ItemBusPartMachine) getMetaMachine(
helper.getBlockEntity(new BlockPos(2, 1, 0)));
ItemBusPartMachine inputBus2 = (ItemBusPartMachine) getMetaMachine(
helper.getBlockEntity(new BlockPos(2, 2, 0)));
ItemBusPartMachine outputBus1 = (ItemBusPartMachine) getMetaMachine(
helper.getBlockEntity(new BlockPos(0, 1, 0)));
return new RecipeLogicTest.BusHolder(inputBus1, inputBus2, outputBus1, controller);
}
@GameTest(template = "lcr_input_separation", batch = "RecipeLogic")
public static void recipeLogicMultiBlockTest(GameTestHelper helper) {
BlockEntity holder = helper.getBlockEntity(new BlockPos(1, 2, 0));
RecipeLogicTest.BusHolder busHolder = getBussesAndForm(helper);
helper.assertTrue(busHolder.controller.isFormed(), "Controller didn't form after structure check");
helper.assertTrue(busHolder.controller.getParts().size() == 6,
"Controller didn't register all 6 parts after structure check, only registered " +
busHolder.controller.getParts().size());
RecipeLogic recipeLogic = busHolder.controller.getRecipeLogic();
recipeLogic.findAndHandleRecipe();
// No recipe found
helper.assertFalse(recipeLogic.isActive(), "Recipe logic is active, even when it shouldn't be");
helper.assertTrue(recipeLogic.getLastRecipe() == null,
"Recipe logic has somehow found a recipe, when there should be none");
// Put an item in the inventory that will trigger recipe recheck
NotifiableItemStackHandler inputSlots = busHolder.inputBus1.getInventory();
NotifiableItemStackHandler outputSlots = busHolder.outputBus1.getInventory();
inputSlots.insertItem(0, new ItemStack(Blocks.COBBLESTONE, 16), false);
recipeLogic.findAndHandleRecipe();
helper.assertFalse(recipeLogic.getLastRecipe() == null,
"Last recipe is empty, even though recipe logic should've found a recipe.");
helper.assertTrue(recipeLogic.isActive(), "Recipelogic is inactive, when it should be active.");
int stackCount = inputSlots.getStackInSlot(0).getCount();
helper.assertTrue(stackCount == 15, "Count is wrong (should be 15, when it's %s)".formatted(stackCount));
// Save a reference to the old recipe so we can make sure it's getting reused
GTRecipe prev = recipeLogic.getLastRecipe();
// Finish the recipe, the output should generate, and the next iteration should begin
recipeLogic.serverTick();
helper.assertTrue(recipeLogic.getLastRecipe().equals(prev), "lastRecipe is wrong");
helper.assertTrue(
TestUtils.isItemStackEqual(outputSlots.getStackInSlot(0),
new ItemStack(Blocks.STONE, 1)),
"wrong output stack.");
helper.assertTrue(recipeLogic.isActive(), "RecipeLogic is not active, when it should be.");
// Complete the second iteration, but the machine stops because its output is now full
// Fill up the recipe with enough stone to complete 1 more recipe and then nothing more.
outputSlots.setStackInSlot(0,
new ItemStack(Blocks.STONE, 63));
for (int i = 1; i < outputSlots.getSlots(); i++) {
outputSlots.setStackInSlot(i,
new ItemStack(Blocks.STONE, 64));
}
recipeLogic.serverTick();
helper.assertFalse(recipeLogic.isActive(), "RecipeLogic is active, when it shouldn't be.");
// Try to process again and get failed out because of full buffer.
recipeLogic.serverTick();
helper.assertFalse(recipeLogic.isActive(), "Recipelogic is active, when it shouldn't be.");
// Some room is freed in the output bus, so we can continue now.
outputSlots.setStackInSlot(0, ItemStack.EMPTY);
recipeLogic.serverTick();
helper.assertTrue(recipeLogic.isActive(), "RecipeLogic didn't start running again");
recipeLogic.serverTick();
helper.assertTrue(
TestUtils.isItemStackEqual(outputSlots.getStackInSlot(0),
new ItemStack(Blocks.STONE, 1)),
"Wrong stack.");
// Finish.
helper.succeed();
}
// spotless:off
// Blocked by LDLib sync issues
/*
@GameTest(template = "singleblock_charged_cr", batch = "RecipeLogic")
public static void recipeLogicSingleBlockTest(GameTestHelper helper) {
WorkableTieredMachine machine = (WorkableTieredMachine) getMetaMachine(
helper.getBlockEntity(new BlockPos(0, 1, 0)));
machine.setRecipeType(CR_RECIPE_TYPE);
NotifiableItemStackHandler inputSlots = (NotifiableItemStackHandler) machine
.getCapabilitiesFlat(IO.IN, ItemRecipeCapability.CAP).get(0);
NotifiableItemStackHandler outputSlots = (NotifiableItemStackHandler) machine
.getCapabilitiesFlat(IO.OUT, ItemRecipeCapability.CAP).get(0);
RecipeLogic recipeLogic = machine.getRecipeLogic();
recipeLogic.findAndHandleRecipe();
// no recipe found
helper.assertFalse(recipeLogic.isActive(), "Recipe logic is active, even when it shouldn't be");
helper.assertTrue(recipeLogic.getLastRecipe() == null,
"Recipe logic has somehow found a recipe, when there should be none");
inputSlots.insertItem(0, new ItemStack(Blocks.COBBLESTONE, 16), false);
inputSlots.onContentsChanged();
recipeLogic.findAndHandleRecipe();
helper.assertFalse(recipeLogic.getLastRecipe() == null,
"Last recipe is empty, even though recipe logic should've found a recipe.");
helper.assertTrue(recipeLogic.isActive(), "Recipelogic is inactive, when it should be active.");
int stackCount = inputSlots.getStackInSlot(0).getCount();
helper.assertTrue(stackCount == 15, "Count is wrong (should be 15, when it's %s)".formatted(stackCount));
// Save a reference to the old recipe so we can make sure it's getting reused
ResourceLocation prev = recipeLogic.getLastRecipe().getId();
// Finish the recipe, the output should generate, and the next iteration should begin
recipeLogic.serverTick();
helper.assertTrue(recipeLogic.getLastRecipe().getId().equals(prev), "lastRecipe is wrong");
helper.assertTrue(TestUtils.isItemStackEqual(
outputSlots.getStackInSlot(0),
new ItemStack(Blocks.STONE, 1)),
"wrong output stack.");
helper.assertTrue(recipeLogic.isActive(), "RecipeLogic is not active, when it should be.");
// Complete the second iteration, but the machine stops because its output is now full
outputSlots.setStackInSlot(0,
new ItemStack(Blocks.STONE, 63));
outputSlots.setStackInSlot(1,
new ItemStack(Blocks.STONE, 64));
recipeLogic.serverTick();
helper.assertFalse(recipeLogic.isActive(), "RecipeLogic is active, when it shouldn't be.");
// Try to process again and get failed out because of full buffer.
recipeLogic.serverTick();
helper.assertFalse(recipeLogic.isActive(), "Recipelogic is active, when it shouldn't be.");
// Some room is freed in the output bus, so we can continue now.
outputSlots.setStackInSlot(0, ItemStack.EMPTY);
recipeLogic.serverTick();
helper.assertTrue(recipeLogic.isActive(), "RecipeLogic didn't start running again");
recipeLogic.serverTick();
helper.assertTrue(
TestUtils.isItemStackEqual(
outputSlots.getStackInSlot(0),
new ItemStack(Blocks.STONE, 1)),
"Wrong stack.");
// Finish.
helper.succeed();
}
*/
// spotless:on
// Test for putting both ingredients in the same bus in 2 stacks.
@GameTest(template = "lcr_input_separation", batch = "RecipeLogicTest")
public static void recipeLogicInTwoStacksTest(GameTestHelper helper) {
RecipeLogicTest.BusHolder busHolder = getBussesAndForm(helper);
busHolder.inputBus1.getInventory().setStackInSlot(0, new ItemStack(Blocks.STONE, 10));
busHolder.inputBus1.getInventory().setStackInSlot(1, new ItemStack(Blocks.STONE, 6));
helper.succeedWhen(() -> {
helper.assertTrue(
TestUtils.isItemStackEqual(busHolder.outputBus1.getInventory().getStackInSlot(0),
new ItemStack(Blocks.STONE)),
"Crafting items in same bus failed, expected STONE but was " +
busHolder.outputBus1.getInventory().getStackInSlot(0).getDisplayName());
});
}
}