-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathRecipeMapTest.java
More file actions
323 lines (266 loc) · 13.5 KB
/
RecipeMapTest.java
File metadata and controls
323 lines (266 loc) · 13.5 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package gregtech.api.recipes;
import gregtech.Bootstrap;
import gregtech.api.GTValues;
import gregtech.api.items.metaitem.MetaItem;
import gregtech.api.recipes.builders.SimpleRecipeBuilder;
import gregtech.api.recipes.map.AbstractMapIngredient;
import gregtech.api.recipes.map.MapFluidIngredient;
import gregtech.api.recipes.map.MapItemStackIngredient;
import gregtech.api.recipes.map.MapOreDictIngredient;
import gregtech.api.unification.material.Materials;
import gregtech.api.util.GTUtility;
import gregtech.common.items.MetaItems;
import gregtech.loaders.recipe.VanillaStandardRecipes;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.oredict.OreDictionary;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsNot;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static gregtech.api.unification.material.Materials.*;
import static org.hamcrest.CoreMatchers.*;
public class RecipeMapTest {
@BeforeAll
public static void bootstrap() {
Bootstrap.perform();
}
RecipeMap<SimpleRecipeBuilder> map;
private static int mapId = 0;
@BeforeEach
public void setupRecipes() {
map = new RecipeMapBuilder<>("test_reactor_" + mapId++, new SimpleRecipeBuilder().EUt(30))
.itemInputs(2)
.itemOutputs(2)
.fluidInputs(3)
.fluidOutputs(2)
.build();
map.recipeBuilder()
.notConsumable(new ItemStack(Blocks.COBBLESTONE))
.outputs(new ItemStack(Blocks.STONE))
.EUt(1).duration(1)
.buildAndRegister();
map.recipeBuilder()
.notConsumable(new ItemStack(Blocks.COBBLESTONE))
.outputs(new ItemStack(Blocks.STONE))
.EUt(1).duration(1)
.buildAndRegister();
map.recipeBuilder()
.notConsumable(new ItemStack(Blocks.COBBLESTONE))
.outputs(new ItemStack(Blocks.STONE))
.EUt(2).duration(1)
.buildAndRegister();
map.recipeBuilder()
.inputs(new ItemStack(Blocks.STONE))
.notConsumable(FluidRegistry.WATER)
.outputs(new ItemStack(Blocks.STONE))
.EUt(1).duration(1)
.buildAndRegister();
map.recipeBuilder()
.fluidInputs(Epichlorohydrin.getFluid(144))
.fluidInputs(Naphtha.getFluid(3000))
.fluidInputs(NitrogenDioxide.getFluid(1000))
.fluidOutputs(Epoxy.getFluid(288))
.duration(240).EUt(30).buildAndRegister();
}
@Test
public void findRecipe() {
MatcherAssert.assertThat(map.getRecipeList().size(), is(3));
Recipe r = map.findRecipe(1, Collections.singletonList(new ItemStack(Blocks.COBBLESTONE)),
Collections.singletonList(null));
MatcherAssert.assertThat(r, notNullValue());
// This test is failing for me locally -dan
Recipe r2 = map.findRecipe(1, Collections.singletonList(new ItemStack(Blocks.STONE)),
Collections.singletonList(new FluidStack(FluidRegistry.WATER, 1)));
MatcherAssert.assertThat(r2, notNullValue());
}
// This test fails
@Test
public void findRecipeFluidOnly() {
Recipe r = map.findRecipe(30,
Collections.singletonList(ItemStack.EMPTY),
Arrays.asList(
Epichlorohydrin.getFluid(144),
Naphtha.getFluid(3000),
NitrogenDioxide.getFluid(1000)));
MatcherAssert.assertThat(r, notNullValue());
}
@Test
public void removeRecipe() {
Recipe r = map.findRecipe(30,
Collections.singletonList(ItemStack.EMPTY),
Arrays.asList(
Epichlorohydrin.getFluid(144),
Naphtha.getFluid(3000),
NitrogenDioxide.getFluid(1000)));
MatcherAssert.assertThat(r, notNullValue());
assert map.removeRecipe(r);
MatcherAssert.assertThat(map.findRecipe(30,
Collections.singletonList(ItemStack.EMPTY),
Arrays.asList(
Epichlorohydrin.getFluid(144),
Naphtha.getFluid(3000),
NitrogenDioxide.getFluid(1000))),
nullValue());
MatcherAssert.assertThat(map.getRecipeList().size(), is(2));
}
@Test
public void recipeLookupIgnoresStackAmount() {
MapItemStackIngredient ingFromStack = new MapItemStackIngredient(
new ItemStack(Blocks.COBBLESTONE), 0, null);
RecipeBuilder r = new RecipeBuilder<>()
.inputs(new ItemStack(Blocks.COBBLESTONE))
.inputs(new ItemStack(Blocks.COBBLESTONE, 2))
.outputs(new ItemStack(Blocks.STONE))
.EUt(1).duration(1);
Recipe rec = (Recipe) r.build().getResult();
MapItemStackIngredient ing0FromGTRecipeInput = new MapItemStackIngredient(
rec.getInputs().get(0).getInputStacks()[0], rec.getInputs().get(0));
MapItemStackIngredient ing1FromGTRecipeInput = new MapItemStackIngredient(
rec.getInputs().get(1).getInputStacks()[0], rec.getInputs().get(1));
MatcherAssert.assertThat(ingFromStack, equalTo(ing0FromGTRecipeInput));
MatcherAssert.assertThat(ingFromStack, equalTo(ing1FromGTRecipeInput));
MatcherAssert.assertThat(ing0FromGTRecipeInput, equalTo(ing1FromGTRecipeInput));
}
@Test
public void recipeLookupIgnoresFluidStackAmount() {
MapFluidIngredient ingFromStack = new MapFluidIngredient(
new FluidStack(FluidRegistry.getFluid("water"), 1000));
RecipeBuilder r = new RecipeBuilder<>()
.fluidInputs(new FluidStack(FluidRegistry.getFluid("water"), 1000))
.fluidInputs(new FluidStack(FluidRegistry.getFluid("water"), 1001))
.outputs(new ItemStack(Blocks.STONE))
.EUt(1).duration(1);
Recipe rec = (Recipe) r.build().getResult();
MapFluidIngredient ing0FromGTRecipeInput = new MapFluidIngredient(rec.getFluidInputs().get(0));
MapFluidIngredient ing1FromGTRecipeInput = new MapFluidIngredient(rec.getFluidInputs().get(1));
MatcherAssert.assertThat(ingFromStack, equalTo(ing0FromGTRecipeInput));
MatcherAssert.assertThat(ingFromStack, equalTo(ing1FromGTRecipeInput));
MatcherAssert.assertThat(ing0FromGTRecipeInput, equalTo(ing1FromGTRecipeInput));
}
@Test
public void GTRecipeInputEquals() {
RecipeBuilder r = new RecipeBuilder<>()
.inputs(new ItemStack(Blocks.COBBLESTONE))
.inputs(new ItemStack(Blocks.COBBLESTONE, 2))
.input("cobblestone", 2)
.outputs(new ItemStack(Blocks.STONE))
.EUt(1).duration(1);
MatcherAssert.assertThat(r.inputs.get(0), new IsNot<>(equalTo(r.inputs.get(1))));
MatcherAssert.assertThat(r.inputs.get(1), new IsNot<>(equalTo(r.inputs.get(2))));
}
@Test
public void MapIngredientEquals() {
RecipeBuilder r = new RecipeBuilder<>()
.inputs(new ItemStack(Blocks.COBBLESTONE))
.inputs(new ItemStack(Blocks.COBBLESTONE, 2))
.input("cobblestone", 2)
.outputs(new ItemStack(Blocks.STONE))
.EUt(1).duration(1);
Recipe rec = (Recipe) r.build().getResult();
MapItemStackIngredient ing0FromGTRecipeInput = new MapItemStackIngredient(
rec.getInputs().get(0).getInputStacks()[0], rec.getInputs().get(0));
MapOreDictIngredient ing1FromGTRecipeInput = new MapOreDictIngredient(OreDictionary.getOreID("cobblestone"));
MatcherAssert.assertThat(ing0FromGTRecipeInput, new IsNot<>(equalTo(ing1FromGTRecipeInput)));
MatcherAssert.assertThat(ing1FromGTRecipeInput, new IsNot<>(equalTo(ing0FromGTRecipeInput)));
MatcherAssert.assertThat(rec.getInputs().get(0).acceptsStack(new ItemStack(Blocks.COBBLESTONE)), is(true));
MatcherAssert.assertThat(rec.getInputs().get(1).acceptsStack(new ItemStack(Blocks.COBBLESTONE)), is(true));
}
@Test
public void MapHashCollision() {
RecipeBuilder r = new RecipeBuilder<>()
.inputs(new ItemStack(Blocks.COBBLESTONE))
.inputs(new ItemStack(Blocks.COBBLESTONE, 2))
.inputs(new ItemStack(Blocks.STONE, 2))
.input("cobblestone", 2)
.outputs(new ItemStack(Blocks.STONE))
.EUt(1).duration(1);
Recipe rec = (Recipe) r.build().getResult();
// create the MapItemStackIngredient and call hashCode so the hash cached to the "hash" field
MapItemStackIngredient ing0FromGTRecipeInput = new MapItemStackIngredient(
rec.getInputs().get(0).getInputStacks()[0], rec.getInputs().get(0));
ing0FromGTRecipeInput.hashCode();
MapItemStackIngredient ing1FromGTRecipeInput = new MapItemStackIngredient(
rec.getInputs().get(1).getInputStacks()[0], rec.getInputs().get(0));
ing1FromGTRecipeInput.hashCode();
MapItemStackIngredient ing2FromGTRecipeInput = new MapItemStackIngredient(
rec.getInputs().get(2).getInputStacks()[0], rec.getInputs().get(0));
ing1FromGTRecipeInput.hashCode();
// Reflection so the equals in AbstractMapIngredient doesn't return false due to anonymous class check failure
try {
Field hash = AbstractMapIngredient.class.getDeclaredField("hash");
hash.setAccessible(true);
hash.set(ing0FromGTRecipeInput, 1);
hash.set(ing1FromGTRecipeInput, 1);
hash.set(ing2FromGTRecipeInput, 1);
} catch (IllegalAccessException | NoSuchFieldException e) {
throw new RuntimeException(e);
}
// Add the cobblestone ingredients to the map, which should considered equals.
Object2ObjectOpenHashMap<MapItemStackIngredient, Object> map = new Object2ObjectOpenHashMap<>();
map.put(ing0FromGTRecipeInput, ing0FromGTRecipeInput);
map.put(ing1FromGTRecipeInput, ing1FromGTRecipeInput);
MatcherAssert.assertThat(map.keySet().size(), is(1));
// Add the stone, which is not equal and is a new key
map.put(ing2FromGTRecipeInput, ing2FromGTRecipeInput);
MatcherAssert.assertThat(map.keySet().size(), is(2));
}
@Test
public void wildcardInput() {
// test that all variants of a wildcard input can be used to find a recipe
RecipeMap<?> recipeMap = new RecipeMapBuilder<>("test", new SimpleRecipeBuilder())
.itemInputs(1)
.itemOutputs(4)
.build();
recipeMap.recipeBuilder()
.inputs(new ItemStack(Blocks.STAINED_HARDENED_CLAY, 1, GTValues.W))
.outputs(new ItemStack(Blocks.COBBLESTONE, 1))
.EUt(1).duration(1)
.buildAndRegister();
for (int i = 0; i < 16; i++) { // Blocks.STAINED_HARDENED_CLAY has 16 variants
Recipe recipe = recipeMap.findRecipe(Integer.MAX_VALUE,
Collections.singletonList(new ItemStack(Blocks.STAINED_HARDENED_CLAY, 1, i)),
Collections.emptyList());
MatcherAssert.assertThat(recipe, notNullValue());
}
}
@Test
public void testInputs() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
for (MetaItem<?> item : MetaItems.ITEMS) {
item.registerSubItems();
}
Method dyingCleaningRecipes = VanillaStandardRecipes.class.getDeclaredMethod("dyingCleaningRecipes");
dyingCleaningRecipes.setAccessible(true);
dyingCleaningRecipes.invoke(null);
FluidStack dye = Materials.CHEMICAL_DYES[1].getFluid(GTValues.L);
Method prepareRecipeFind = RecipeMap.class.getDeclaredMethod("prepareRecipeFind", Collection.class,
Collection.class);
prepareRecipeFind.setAccessible(true);
// noinspection unchecked
List<List<AbstractMapIngredient>> list = (List<List<AbstractMapIngredient>>) prepareRecipeFind.invoke(map,
Collections.singletonList(new ItemStack(Blocks.WOOL)),
Collections.singletonList(GTUtility.copy(dye)));
// noinspection unchecked
List<List<AbstractMapIngredient>> list2 = (List<List<AbstractMapIngredient>>) prepareRecipeFind.invoke(map,
Collections.singletonList(new ItemStack(Blocks.WOOL)),
Collections.singletonList(Chlorine.getFluid(50)));
MatcherAssert.assertThat("the first two ingredients are not equal!",
list.get(0).get(0).equals(list2.get(0).get(0)));
List<ItemStack> wool = Arrays.asList(new ItemStack(Blocks.WOOL));
List<FluidStack> fluidDye = Arrays.asList(GTUtility.copy(dye));
Recipe recipe = RecipeMaps.CHEMICAL_BATH_RECIPES.find(
wool, fluidDye, r -> r.matches(false, wool, fluidDye));
MatcherAssert.assertThat("recipe could not be found!", recipe != null);
}
}