Skip to content

Commit a42c711

Browse files
committed
Mixed layer 100 percent coverage
1 parent 9728505 commit a42c711

File tree

6 files changed

+162
-19
lines changed

6 files changed

+162
-19
lines changed

src/main/java/dev/compactmods/crafting/recipes/layers/MixedComponentRecipeLayer.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ public int getNumberFilledPositions() {
8484

8585
@Override
8686
public boolean matches(IRecipeComponents components, IRecipeLayerBlocks blocks) {
87-
if(!blocks.allIdentified()) {
88-
final Set<String> unknown = blocks.getUnknownComponents().collect(Collectors.toSet());
89-
return false;
90-
}
87+
if(!blocks.allIdentified()) return false;
9188

9289
final Collection<String> requiredKeys = this.componentLookup.getComponents();
9390
final Map<String, Integer> componentTotals = blocks.getKnownComponentTotals();
@@ -109,20 +106,14 @@ public boolean matches(IRecipeComponents components, IRecipeLayerBlocks blocks)
109106

110107
final Set<BlockPos> actual = blocks.getPositionsForComponent(required)
111108
.map(BlockPos::immutable).collect(Collectors.toSet());
109+
112110
final Set<BlockPos> expected = componentLookup.getPositionsForComponent(required)
113111
.map(BlockPos::immutable).collect(Collectors.toSet());
114112

115-
// Dry run - ensure component counts match actual v. expected
116-
if(expected.size() != actual.size()) {
117-
if(ServerConfig.RECIPE_MATCHING.get())
118-
CompactCrafting.RECIPE_LOGGER.debug("Failed to match: required vs. expected counts do not match. ");
119-
return false;
120-
}
121-
122113
if(!expected.equals(actual))
123114
{
124115
if(ServerConfig.RECIPE_MATCHING.get())
125-
CompactCrafting.RECIPE_LOGGER.debug("Failed to match: required components are missing.");
116+
CompactCrafting.RECIPE_LOGGER.debug("Failed to match: required components are missing or in incorrect spots.");
126117
return false;
127118
}
128119
}

src/test/java/dev/compactmods/crafting/tests/recipes/layers/FilledLayerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ void FilledLayerMatchesWorldInExactMatchScenario() {
124124
@Test
125125
@Tag("minecraft")
126126
void FailsMatchIfAllBlocksNotIdentified() {
127-
final TestBlockReader reader = RecipeTestUtil.getBlockReader("worlds/basic_harness_5x.json");
128-
final CCMiniRecipeComponents components = RecipeTestUtil.getComponentsFromRecipeFile("worlds/basic_harness_5x.json");
127+
final TestBlockReader reader = RecipeTestUtil.getBlockReader("worlds/basic_mixed_medium_iron.json");
128+
final CCMiniRecipeComponents components = RecipeTestUtil.getComponentsFromRecipeFile("worlds/basic_mixed_medium_iron.json");
129129

130130
Assertions.assertNotNull(reader);
131131

src/test/java/dev/compactmods/crafting/tests/recipes/layers/MixedLayerTests.java

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@
44
import java.util.Map;
55
import java.util.Optional;
66
import java.util.Set;
7+
import java.util.stream.Collectors;
78
import java.util.stream.Stream;
89
import com.google.gson.JsonElement;
910
import com.mojang.serialization.DataResult;
1011
import com.mojang.serialization.JsonOps;
12+
import dev.compactmods.crafting.api.components.IRecipeComponent;
13+
import dev.compactmods.crafting.api.components.IRecipeComponents;
1114
import dev.compactmods.crafting.api.field.MiniaturizationFieldSize;
1215
import dev.compactmods.crafting.api.recipe.layers.IRecipeLayer;
16+
import dev.compactmods.crafting.api.recipe.layers.IRecipeLayerBlocks;
1317
import dev.compactmods.crafting.recipes.MiniaturizationRecipe;
1418
import dev.compactmods.crafting.recipes.blocks.ComponentPositionLookup;
1519
import dev.compactmods.crafting.recipes.blocks.RecipeLayerBlocks;
1620
import dev.compactmods.crafting.recipes.components.CCMiniRecipeComponents;
1721
import dev.compactmods.crafting.recipes.layers.MixedComponentRecipeLayer;
22+
import dev.compactmods.crafting.recipes.layers.RecipeLayerUtil;
23+
import dev.compactmods.crafting.server.ServerConfig;
1824
import dev.compactmods.crafting.tests.recipes.util.RecipeTestUtil;
1925
import dev.compactmods.crafting.tests.util.FileHelper;
2026
import dev.compactmods.crafting.tests.world.TestBlockReader;
2127
import dev.compactmods.crafting.util.BlockSpaceUtil;
28+
import net.minecraft.util.Rotation;
2229
import net.minecraft.util.math.AxisAlignedBB;
2330
import net.minecraft.util.math.BlockPos;
2431
import org.junit.jupiter.api.Assertions;
@@ -27,6 +34,14 @@
2734

2835
public class MixedLayerTests {
2936

37+
@Tag("minecraft")
38+
@org.junit.jupiter.api.BeforeAll
39+
static void BeforeAllTests() {
40+
ServerConfig.RECIPE_REGISTRATION.set(true);
41+
ServerConfig.RECIPE_MATCHING.set(true);
42+
ServerConfig.FIELD_BLOCK_CHANGES.set(true);
43+
}
44+
3045
static MixedComponentRecipeLayer getLayerFromFile(String filename) {
3146
JsonElement layerJson = FileHelper.INSTANCE.getJsonFromFile(filename);
3247

@@ -76,7 +91,7 @@ void CanCreateLayerInstance() {
7691
@Tag("minecraft")
7792
void remapsUnknownComponents() {
7893
MiniaturizationRecipe recipe = RecipeTestUtil.getRecipeFromFile("recipes/ender_crystal.json");
79-
if(recipe == null)
94+
if (recipe == null)
8095
Assertions.fail();
8196

8297
Optional<IRecipeLayer> layer = recipe.getLayer(2);
@@ -94,8 +109,8 @@ void remapsUnknownComponents() {
94109
@Test
95110
@Tag("minecraft")
96111
void MixedLayerMatchesWorldInExactMatchScenario() {
97-
final TestBlockReader reader = RecipeTestUtil.getBlockReader("worlds/basic_harness_5x.json");
98-
final CCMiniRecipeComponents components = RecipeTestUtil.getComponentsFromRecipeFile("worlds/basic_harness_5x.json");
112+
final TestBlockReader reader = RecipeTestUtil.getBlockReader("worlds/basic_mixed_medium_iron.json");
113+
final CCMiniRecipeComponents components = RecipeTestUtil.getComponentsFromRecipeFile("worlds/basic_mixed_medium_iron.json");
99114

100115
Assertions.assertNotNull(reader);
101116

@@ -108,4 +123,109 @@ void MixedLayerMatchesWorldInExactMatchScenario() {
108123
final Boolean matched = Assertions.assertDoesNotThrow(() -> layer.matches(components, blocks));
109124
Assertions.assertTrue(matched, "Expected layer to match; layer did not match.");
110125
}
126+
127+
@Test
128+
@Tag("minecraft")
129+
void MixedCanFetchAKnownGoodPosition() {
130+
MixedComponentRecipeLayer layer = getLayerFromFile("layers/mixed/basic.json");
131+
Assertions.assertNotNull(layer);
132+
133+
Optional<String> spot = layer.getComponentForPosition(BlockPos.ZERO);
134+
Assertions.assertTrue(spot.isPresent());
135+
Assertions.assertEquals("I", spot.get());
136+
}
137+
138+
@Test
139+
@Tag("minecraft")
140+
void MixedCanFetchAListOfComponentPositions() {
141+
MixedComponentRecipeLayer layer = getLayerFromFile("layers/mixed/basic.json");
142+
Assertions.assertNotNull(layer);
143+
144+
final Stream<BlockPos> g = layer.getPositionsForComponent("G");
145+
Assertions.assertNotNull(g);
146+
147+
final Set<BlockPos> positions = g.map(BlockPos::immutable).collect(Collectors.toSet());
148+
Assertions.assertFalse(positions.isEmpty());
149+
Assertions.assertEquals(15, positions.size());
150+
}
151+
152+
@Test
153+
@Tag("minecraft")
154+
void MixedLayerDeniesMatchIfAllComponentsNotIdentified() {
155+
final TestBlockReader reader = RecipeTestUtil.getBlockReader("worlds/basic_mixed_medium_iron.json");
156+
final CCMiniRecipeComponents components = RecipeTestUtil.getComponentsFromRecipeFile("worlds/basic_mixed_medium_iron.json");
157+
158+
Assertions.assertNotNull(reader);
159+
160+
// Force that the - component is unregistered; in a real scenario the recipe system would have remapped it
161+
// to an empty component due to it existing in the layer spec. Here, we're testing if a legit component in the world
162+
// did not match.
163+
reader.source.getComponents().unregisterBlock("-");
164+
165+
final RecipeLayerBlocks blocks = RecipeLayerBlocks.create(reader, reader.source, BlockSpaceUtil.getLayerBounds(MiniaturizationFieldSize.MEDIUM, 0));
166+
167+
final MixedComponentRecipeLayer layer = getLayerFromFile("layers/mixed/basic.json");
168+
169+
final Boolean matched = Assertions.assertDoesNotThrow(() -> layer.matches(components, blocks));
170+
Assertions.assertFalse(matched, "Expected layer not to match; layer matched anyway.");
171+
}
172+
173+
@Test
174+
@Tag("minecraft")
175+
void MixedLayerDeniesMatchIfComponentCountDiffers() {
176+
final TestBlockReader reader = RecipeTestUtil.getBlockReader("worlds/single_layer_medium_filled_G.json");
177+
final CCMiniRecipeComponents components = RecipeTestUtil.getComponentsFromRecipeFile("worlds/single_layer_medium_filled_G.json");
178+
final MixedComponentRecipeLayer layer = getLayerFromFile("layers/mixed/basic.json");
179+
180+
Assertions.assertNotNull(reader);
181+
182+
final RecipeLayerBlocks blocks = RecipeLayerBlocks.create(reader, reader.source, BlockSpaceUtil.getLayerBounds(MiniaturizationFieldSize.MEDIUM, 0));
183+
184+
final IRecipeComponents readerComponents = reader.source.getComponents();
185+
final Map<String, IRecipeComponent> allComponents = readerComponents.getAllComponents();
186+
final int worldCompCount = allComponents.keySet().size();
187+
188+
final Set<String> layerComponents = layer.getComponents();
189+
final int layerCompCount = layerComponents.size();
190+
191+
Assertions.assertNotEquals(layerCompCount, worldCompCount);
192+
193+
final Boolean matched = Assertions.assertDoesNotThrow(() -> layer.matches(components, blocks));
194+
Assertions.assertFalse(matched, "Expected layer not to match due to component count mismatch; layer matched anyway.");
195+
}
196+
197+
@Test
198+
@Tag("minecraft")
199+
void MixedLayerDeniesMatchIfRequiredComponentsMissing() {
200+
String file = "worlds/basic_mixed_medium_redstone.json";
201+
final TestBlockReader reader = RecipeTestUtil.getBlockReader(file);
202+
final CCMiniRecipeComponents components = RecipeTestUtil.getComponentsFromRecipeFile(file);
203+
204+
Assertions.assertNotNull(reader);
205+
206+
final RecipeLayerBlocks blocks = RecipeLayerBlocks.create(reader, reader.source, BlockSpaceUtil.getLayerBounds(MiniaturizationFieldSize.MEDIUM, 0));
207+
208+
final MixedComponentRecipeLayer layer = getLayerFromFile("layers/mixed/basic.json");
209+
210+
final Boolean matched = Assertions.assertDoesNotThrow(() -> layer.matches(components, blocks));
211+
Assertions.assertFalse(matched, "Expected layer not to match due to missing required components; layer matched anyway.");
212+
}
213+
214+
@Test
215+
@Tag("minecraft")
216+
void MixedLayerDeniesMatchIfComponentsInWrongPositions() {
217+
String file = "worlds/basic_mixed_medium_iron.json";
218+
final TestBlockReader reader = RecipeTestUtil.getBlockReader(file);
219+
final CCMiniRecipeComponents components = RecipeTestUtil.getComponentsFromRecipeFile(file);
220+
221+
Assertions.assertNotNull(reader);
222+
223+
final RecipeLayerBlocks blocks = RecipeLayerBlocks.create(reader, reader.source, BlockSpaceUtil.getLayerBounds(MiniaturizationFieldSize.MEDIUM, 0));
224+
final IRecipeLayerBlocks rotated = RecipeLayerUtil.rotate(blocks, Rotation.CLOCKWISE_90);
225+
226+
final MixedComponentRecipeLayer layer = getLayerFromFile("layers/mixed/basic.json");
227+
228+
final Boolean matched = Assertions.assertDoesNotThrow(() -> layer.matches(components, rotated));
229+
Assertions.assertFalse(matched, "Expected layer not to match due to incorrect positions; layer matched anyway.");
230+
}
111231
}

src/test/java/dev/compactmods/crafting/tests/recipes/layers/RecipeLayerUtilTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class RecipeLayerUtilTests {
2121
@Test
2222
@Tag("minecraft")
2323
void CanRotate() {
24-
TestBlockReader reader = RecipeTestUtil.getBlockReader("worlds/basic_harness_5x.json");
24+
TestBlockReader reader = RecipeTestUtil.getBlockReader("worlds/basic_mixed_medium_iron.json");
2525
Assertions.assertNotNull(reader);
2626

2727
final RecipeLayerBlocks blocks = RecipeLayerBlocks.create(reader, reader.source, BlockSpaceUtil.getLayerBounds(MiniaturizationFieldSize.MEDIUM, 0));
@@ -71,7 +71,7 @@ void CanRotateWithUnknownComponent() {
7171
@Test
7272
@Tag("minecraft")
7373
void NonRotationCreatesCopiedInstance() {
74-
TestBlockReader reader = RecipeTestUtil.getBlockReader("worlds/basic_harness_5x.json");
74+
TestBlockReader reader = RecipeTestUtil.getBlockReader("worlds/basic_mixed_medium_iron.json");
7575
Assertions.assertNotNull(reader);
7676

7777
final RecipeLayerBlocks blocks = RecipeLayerBlocks.create(reader, reader.source, BlockSpaceUtil.getLayerBounds(MiniaturizationFieldSize.MEDIUM, 0));
File renamed without changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"layers": [
3+
{
4+
"type": "compactcrafting:mixed",
5+
"pattern": [
6+
["R", "G", "G", "G", "G"],
7+
["G", "-", "-", "-", "G"],
8+
["G", "-", "O", "-", "G"],
9+
["G", "-", "-", "-", "G"],
10+
["G", "G", "G", "G", "G"]
11+
]
12+
}
13+
],
14+
"components": {
15+
"G": {
16+
"type": "compactcrafting:block",
17+
"block": "minecraft:glass"
18+
},
19+
"O": {
20+
"type": "compactcrafting:block",
21+
"block": "minecraft:obsidian"
22+
},
23+
"R": {
24+
"type": "compactcrafting:block",
25+
"block": "minecraft:redstone_block"
26+
}
27+
},
28+
"outputs": [{
29+
"id": "minecraft:diamond",
30+
"Count": 1
31+
}]
32+
}

0 commit comments

Comments
 (0)