Skip to content

Commit 0930faf

Browse files
committed
Pile 'o test fixes
1 parent 040159d commit 0930faf

13 files changed

+216
-168
lines changed

src/main/java/dev/compactmods/crafting/recipes/MiniaturizationRecipeCodec.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ public <T> DataResult<Pair<MiniaturizationRecipe, T>> decode(DynamicOps<T> ops,
8282
recipe.recalculateDimensions();
8383

8484
final Optional<T> catalystNode = ops.get(input, "catalyst").result();
85-
if(!catalystNode.isPresent()) {
86-
if(debugOutput)
85+
if (!catalystNode.isPresent()) {
86+
if (debugOutput)
8787
CompactCrafting.LOGGER.warn("No catalyst node defined in recipe; this is likely a bad file!");
8888
} else {
8989
final Optional<T> catalystType = ops.get(catalystNode.get(), "type").result();
90-
if(!catalystType.isPresent()) {
91-
if(debugOutput)
90+
if (!catalystType.isPresent()) {
91+
if (debugOutput)
9292
CompactCrafting.LOGGER.warn("Error: no catalyst type defined; falling back to the itemstack handler.");
9393

9494
final ItemStack stackData = ItemStack.CODEC.fieldOf("catalyst").codec()
@@ -117,7 +117,7 @@ public <T> DataResult<Pair<MiniaturizationRecipe, T>> decode(DynamicOps<T> ops,
117117
return DataResult.error(errorBuilder.toString(), Pair.of(recipe, input), Lifecycle.stable());
118118
}
119119

120-
if(recipe.getOutputs().length == 0) {
120+
if (recipe.getOutputs().length == 0) {
121121
errorBuilder.append("No outputs were defined.");
122122
return DataResult.error(errorBuilder.toString(), Pair.of(recipe, input), Lifecycle.stable());
123123
}
@@ -153,7 +153,13 @@ public <T> DataResult<T> encode(MiniaturizationRecipe recipe, DynamicOps<T> ops,
153153

154154
ICatalystMatcher catalystItem = recipe.getCatalyst();
155155

156-
DataResult<T> catalyst = CatalystMatcherCodec.MATCHER_CODEC.encodeStart(ops, catalystItem);
156+
DataResult<T> catalyst;
157+
if (catalystItem != null) {
158+
catalyst = CatalystMatcherCodec.MATCHER_CODEC.encodeStart(ops, catalystItem);
159+
} else {
160+
CompactCrafting.RECIPE_LOGGER.warn("Catalyst appears to be missing.");
161+
catalyst = DataResult.success(null);
162+
}
157163

158164
DataResult<T> outputs = ItemStack.CODEC.listOf()
159165
.encodeStart(ops, ImmutableList.copyOf(recipe.getOutputs()));
@@ -165,10 +171,13 @@ public <T> DataResult<T> encode(MiniaturizationRecipe recipe, DynamicOps<T> ops,
165171
if (recipe.hasSpecifiedSize())
166172
builder.add("recipeSize", Codec.INT.encodeStart(ops, recipe.getRecipeSize()));
167173

168-
return builder.add("layers", layers)
169-
.add("components", components)
170-
.add("catalyst", catalyst)
171-
.add("outputs", outputs)
174+
var b = builder.add("layers", layers)
175+
.add("components", components);
176+
177+
if (catalystItem != null)
178+
b.add("catalyst", catalyst);
179+
180+
return b.add("outputs", outputs)
172181
.build(prefix);
173182
}
174183
}

src/main/java/dev/compactmods/crafting/recipes/MiniaturizationRecipeSerializer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.minecraft.resources.ResourceLocation;
1111
import net.minecraft.world.item.crafting.RecipeSerializer;
1212
import net.minecraftforge.registries.ForgeRegistryEntry;
13+
import org.jetbrains.annotations.NotNull;
1314

1415
public class MiniaturizationRecipeSerializer extends ForgeRegistryEntry<RecipeSerializer<?>>
1516
implements RecipeSerializer<MiniaturizationRecipe> {
@@ -35,12 +36,14 @@ public MiniaturizationRecipe fromJson(ResourceLocation recipeId, JsonObject json
3536

3637
@Nullable
3738
@Override
38-
public MiniaturizationRecipe fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer) {
39+
public MiniaturizationRecipe fromNetwork(@NotNull ResourceLocation recipeId, @NotNull FriendlyByteBuf buffer) {
3940
boolean debugReg = ServerConfig.RECIPE_REGISTRATION.get();
4041
if (debugReg) CompactCrafting.LOGGER.debug("Starting recipe read: {}", recipeId);
4142

4243
if(!buffer.isReadable() || buffer.readableBytes() == 0) {
43-
CompactCrafting.LOGGER.error("Recipe not readable from buffer: {}", recipeId);
44+
if(debugReg)
45+
CompactCrafting.LOGGER.error("Recipe not readable from buffer: {}", recipeId);
46+
4447
return null;
4548
}
4649

@@ -53,7 +56,7 @@ public MiniaturizationRecipe fromNetwork(ResourceLocation recipeId, FriendlyByte
5356
}
5457

5558
@Override
56-
public void toNetwork(FriendlyByteBuf buffer, MiniaturizationRecipe recipe) {
59+
public void toNetwork(@NotNull FriendlyByteBuf buffer, @NotNull MiniaturizationRecipe recipe) {
5760
boolean debugReg = ServerConfig.RECIPE_REGISTRATION.get();
5861
if(debugReg)
5962
CompactCrafting.LOGGER.debug("Sending recipe over network: {}", recipe.getRecipeIdentifier());

src/main/java/dev/compactmods/crafting/server/ServerConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class ServerConfig {
3030

3131
private static ForgeConfigSpec.EnumValue<FieldDestabilizeHandling> FIELD_DESTABILIZE_HANDLING;
3232
public static FieldDestabilizeHandling DESTABILIZE_HANDLING = FieldDestabilizeHandling.RESTORE_ALL;
33-
33+
3434
static {
3535
generateConfig();
3636
}

src/test/java/dev/compactmods/crafting/tests/recipes/MiniaturizationRecipes.java renamed to src/test/java/dev/compactmods/crafting/tests/recipes/MiniaturizationRecipeTests.java

Lines changed: 86 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
import net.minecraft.world.item.ItemStack;
2424
import net.minecraft.world.level.block.Blocks;
2525
import net.minecraft.world.level.block.state.BlockState;
26+
import net.minecraftforge.gametest.GameTestHolder;
27+
import net.minecraftforge.gametest.PrefixGameTestTemplate;
2628
import org.junit.jupiter.api.Assertions;
2729
import org.junit.jupiter.api.Test;
2830

29-
// @MCTestClass
30-
public class MiniaturizationRecipes {
31+
@PrefixGameTestTemplate(false)
32+
@GameTestHolder(CompactCrafting.MOD_ID)
33+
public class MiniaturizationRecipeTests {
3134

3235
@Nullable
3336
private static MiniaturizationRecipe getRecipe(GameTestHelper testHelper, String name) {
@@ -60,36 +63,30 @@ void IsSpecialRecipe() {
6063
Assertions.assertTrue(recipe.isSpecial());
6164
}
6265

63-
64-
// @StructureFile("ender_crystal")
65-
@GameTest(template = "recipes/ender_crystal", templateNamespace = CompactCrafting.MOD_ID)
66+
@GameTest(template = "recipes/ender_crystal")
6667
public static void FakesFakeInventories(final GameTestHelper test) {
6768
MiniaturizationRecipe recipe = new MiniaturizationRecipe();
6869

6970
try {
7071
boolean matched = recipe.matches(new FakeInventory(), test.getLevel());
71-
if(!matched)
72+
if (!matched)
7273
test.fail("Expected fake inventory to always match.");
73-
}
74-
75-
catch (Exception e) {
74+
} catch (Exception e) {
7675
test.fail(e.getMessage());
7776
}
7877

7978
test.succeed();
8079
}
8180

82-
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
81+
@GameTest(template = "empty_medium")
8382
public static void FakesAssemble(final GameTestHelper test) {
8483
MiniaturizationRecipe recipe = new MiniaturizationRecipe();
8584

8685
try {
8786
ItemStack result = recipe.assemble(new FakeInventory());
88-
if(!result.isEmpty())
87+
if (!result.isEmpty())
8988
test.fail("Expected an empty result");
90-
}
91-
92-
catch(Exception e) {
89+
} catch (Exception e) {
9390
test.fail(e.getMessage());
9491
}
9592

@@ -107,54 +104,52 @@ void FakesCanCraftDimensions() {
107104
});
108105
}
109106

110-
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
107+
@GameTest(template = "empty_medium")
111108
public static void FakesResultItem(final GameTestHelper test) {
112109
MiniaturizationRecipe recipe = new MiniaturizationRecipe();
113110

114-
try{
111+
try {
115112
final ItemStack result = recipe.getResultItem();
116-
if(!result.isEmpty())
113+
if (!result.isEmpty())
117114
test.fail("Expected recipe result to be empty.");
118-
}
119-
120-
catch(Exception e) {
115+
} catch (Exception e) {
121116
test.fail(e.getMessage());
122117
}
123118

124119
test.succeed();
125120
}
126121

127-
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
122+
@GameTest(template = "empty_medium")
128123
public static void RecipeSuppliesBasicMinecraftRegistrationInfo(final GameTestHelper test) {
129124
final MiniaturizationRecipe enderCrystal = getRecipe(test, "ender_crystal");
130-
if(enderCrystal == null)
125+
if (enderCrystal == null)
131126
return;
132127

133128
final var serializer = enderCrystal.getSerializer();
134-
if(serializer == null)
129+
if (serializer == null)
135130
test.fail("Did not get a recipe serializer from the recipe class.");
136131

137132
final var type = enderCrystal.getType();
138-
if(type == null)
133+
if (type == null)
139134
test.fail("Did not get a recipe type from the recipe class.");
140135

141136
test.succeed();
142137
}
143138

144139

145-
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
140+
@GameTest(template = "empty_medium")
146141
public static void RecipeReturnsEmptyIfLayerNotRegistered(final GameTestHelper test) {
147142
final MiniaturizationRecipe enderCrystal = getRecipe(test, "ender_crystal");
148143
Objects.requireNonNull(enderCrystal);
149144

150145
final Optional<IRecipeLayer> layer = enderCrystal.getLayer(999);
151-
if(layer.isPresent())
146+
if (layer.isPresent())
152147
test.fail("Layer should not have been present.");
153148

154149
test.succeed();
155150
}
156151

157-
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
152+
@GameTest(template = "empty_medium")
158153
public static void FitsInCorrectFieldSizes(final GameTestHelper test) {
159154
final MiniaturizationRecipe enderCrystal = getRecipe(test, "ender_crystal");
160155
Objects.requireNonNull(enderCrystal);
@@ -169,36 +164,50 @@ public static void FitsInCorrectFieldSizes(final GameTestHelper test) {
169164

170165
// TODO: GameTestGenerator?
171166
for (MiniaturizationFieldSize bs : badSizes)
172-
if(enderCrystal.fitsInFieldSize(bs))
167+
if (enderCrystal.fitsInFieldSize(bs))
173168
test.fail("Fit in bad field size: " + bs);
174169

175170
for (MiniaturizationFieldSize gs : goodSizes)
176-
if(!enderCrystal.fitsInFieldSize(gs))
171+
if (!enderCrystal.fitsInFieldSize(gs))
177172
test.fail("Did not fit in field size: " + gs);
178173

179174
test.succeed();
180175
}
181176

182-
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
177+
@GameTest(template = "empty_medium")
183178
public static void CanGetComponentTotals(final GameTestHelper test) {
184179
final MiniaturizationRecipe recipe = getRecipe(test, "ender_crystal");
185-
Assertions.assertNotNull(recipe);
180+
Objects.requireNonNull(recipe);
186181

187-
final Map<String, Integer> totals = Assertions.assertDoesNotThrow(recipe::getComponentTotals);
188-
Assertions.assertNotNull(totals);
189-
Assertions.assertEquals(2, totals.size()); // expect 2 (G, O)
182+
final Map<String, Integer> totals = recipe.getComponentTotals();
183+
if (totals == null) {
184+
test.fail("Returned totals should not be null.");
185+
return;
186+
}
190187

191-
for (String key : new String[]{"G", "O"})
192-
Assertions.assertTrue(totals.containsKey(key), "Totals did not contain key: " + key);
188+
if (2 != totals.size()) {
189+
// expect 2 (G, O)
190+
test.fail("Expected exactly two components found (G,O). Got (" + String.join(",", totals.keySet()) + ")");
191+
}
193192

194-
final Map<String, Integer> maybeCached = Assertions.assertDoesNotThrow(recipe::getComponentTotals);
195-
Assertions.assertSame(totals, maybeCached);
193+
for (String key : new String[]{"G", "O"}) {
194+
if (!totals.containsKey(key))
195+
test.fail("Totals did not contain key: " + key);
196+
}
196197

197-
final Integer totalObsidian = Assertions.assertDoesNotThrow(() -> recipe.getComponentRequiredCount("O"));
198-
Assertions.assertEquals(1, totalObsidian);
198+
final Map<String, Integer> maybeCached = recipe.getComponentTotals();
199+
if(totals != maybeCached) {
200+
test.fail("Component totals should be cached after first call.");
201+
}
202+
203+
final int totalObsidian = recipe.getComponentRequiredCount("O");
204+
if(totalObsidian != 1)
205+
test.fail("Expected exactly 1 obsidian block to be required.");
206+
207+
test.succeed();
199208
}
200209

201-
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
210+
@GameTest(template = "empty_medium")
202211
public static void UnregisteredBlockReturnsZeroCount(final GameTestHelper test) {
203212
final MiniaturizationRecipe recipe = getRecipe(test, "ender_crystal");
204213
Assertions.assertNotNull(recipe);
@@ -207,32 +216,43 @@ public static void UnregisteredBlockReturnsZeroCount(final GameTestHelper test)
207216
Assertions.assertEquals(0, required);
208217
}
209218

210-
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
219+
@GameTest(template = "empty_medium")
211220
public static void HasCraftingTime(final GameTestHelper test) {
212221
final MiniaturizationRecipe recipe = getRecipe(test, "ender_crystal");
213-
Assertions.assertNotNull(recipe);
222+
Objects.requireNonNull(recipe);
223+
224+
final int required = recipe.getCraftingTime();
225+
if(required == 0)
226+
test.fail("Expected recipe to have a default, non-zero crafting time.");
214227

215-
final int required = Assertions.assertDoesNotThrow(recipe::getCraftingTime);
216-
Assertions.assertNotEquals(0, required);
228+
test.succeed();
217229
}
218230

219231

220-
@GameTest(template = "recipes/ender_crystal", templateNamespace = CompactCrafting.MOD_ID)
232+
@GameTest(template = "recipes/ender_crystal")
221233
public static void MatchesExactStructure(final GameTestHelper test) {
222234
final BlockPos zero = test.relativePos(BlockPos.ZERO);
223235
final MiniaturizationRecipe enderCrystal = getRecipe(test, "ender_crystal");
224236
final IRecipeBlocks blocks = RecipeBlocks
225237
.create(test.getLevel(), enderCrystal.getComponents(), enderCrystal.getDimensions().move(zero))
226238
.normalize();
227239

228-
Assertions.assertDoesNotThrow(() -> {
240+
try {
229241
boolean matched = enderCrystal.matches(blocks);
230-
Assertions.assertTrue(matched);
231-
});
242+
if(!matched) {
243+
test.fail("Recipe should have matched.");
244+
}
245+
246+
test.succeed();
247+
}
248+
249+
catch(Exception e) {
250+
test.fail(e.getMessage());
251+
}
232252
}
233253

234254

235-
@GameTest(template = "recipes/ender_crystal", templateNamespace = CompactCrafting.MOD_ID)
255+
@GameTest(template = "recipes/ender_crystal")
236256
public static void RecipeFailsIfUnidentifiedBlock(final GameTestHelper test) {
237257
final MiniaturizationRecipe enderCrystal = getRecipe(test, "ender_crystal");
238258
Assertions.assertNotNull(enderCrystal);
@@ -250,18 +270,27 @@ public static void RecipeFailsIfUnidentifiedBlock(final GameTestHelper test) {
250270
});
251271
}
252272

253-
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
273+
@GameTest(template = "empty_medium")
254274
public static void CanStreamLayerInfo(final GameTestHelper test) {
255275
final MiniaturizationRecipe enderCrystal = getRecipe(test, "ender_crystal");
256-
final Stream<IRecipeLayer> strem = Assertions.assertDoesNotThrow(enderCrystal::getLayers);
276+
Objects.requireNonNull(enderCrystal);
257277

258-
Assertions.assertNotNull(strem);
278+
final Stream<IRecipeLayer> layers1 = enderCrystal.getLayers();
259279

260-
final Set<IRecipeLayer> layers = strem.collect(Collectors.toSet());
261-
Assertions.assertEquals(5, layers.size());
280+
if(layers1 == null) {
281+
test.fail("Recipe did not create a stream of layers correctly.");
282+
return;
283+
}
284+
285+
final Set<IRecipeLayer> layers = layers1.collect(Collectors.toSet());
286+
if(5 != layers.size()) {
287+
test.fail("Expected 5 layers; got " + layers.size());
288+
}
289+
290+
test.succeed();
262291
}
263292

264-
@GameTest(template = "recipes/ender_crystal", templateNamespace = CompactCrafting.MOD_ID)
293+
@GameTest(template = "recipes/ender_crystal")
265294
public static void RecipeFailsIfDifferentDimensions(final GameTestHelper test) {
266295
final MiniaturizationRecipe recipe = getRecipe(test, "compact_walls");
267296
Assertions.assertNotNull(recipe);
@@ -275,7 +304,7 @@ public static void RecipeFailsIfDifferentDimensions(final GameTestHelper test) {
275304
}
276305

277306

278-
@GameTest(template = "recipes/empty_medium", templateNamespace = CompactCrafting.MOD_ID)
307+
@GameTest(template = "recipes/empty_medium")
279308
public static void RecipeFailsIfNoRotationsMatched(final GameTestHelper test) {
280309
final MiniaturizationRecipe recipe = getRecipe(test, "ender_crystal");
281310
Assertions.assertNotNull(recipe);

0 commit comments

Comments
 (0)