Skip to content

Commit 9728505

Browse files
committed
Remove IRecipeBlocks test class, replace with a IBlockReader impl
1 parent 50b2010 commit 9728505

31 files changed

+460
-319
lines changed

src/api/java/dev/compactmods/crafting/api/components/IRecipeBlockComponent.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package dev.compactmods.crafting.api.components;
22

3+
import net.minecraft.block.Block;
34
import net.minecraft.block.BlockState;
45

56
public interface IRecipeBlockComponent extends IRecipeComponent {
7+
68
boolean matches(BlockState state);
79

10+
Block getBlock();
811
BlockState getRenderState();
912

1013
boolean didErrorRendering();

src/api/java/dev/compactmods/crafting/api/components/IRecipeComponents.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public interface IRecipeComponents {
1616
boolean hasBlock(String key);
1717

1818
void registerBlock(String key, IRecipeBlockComponent component);
19+
void unregisterBlock(String key);
20+
1921
void registerOther(String key, IRecipeComponent component);
2022

2123
int size();

src/api/java/dev/compactmods/crafting/api/recipe/IMiniaturizationRecipe.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Collection;
44
import java.util.Optional;
5+
import java.util.stream.Stream;
56
import dev.compactmods.crafting.api.components.IRecipeComponents;
67
import dev.compactmods.crafting.api.recipe.layers.IRecipeLayer;
78
import net.minecraft.item.ItemStack;
@@ -24,4 +25,6 @@ public interface IMiniaturizationRecipe {
2425
IRecipeComponents getComponents();
2526

2627
void setOutputs(Collection<ItemStack> outputs);
28+
29+
Stream<IRecipeLayer> getLayers();
2730
}

src/api/java/dev/compactmods/crafting/api/recipe/layers/IRecipeLayerBlocks.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface IRecipeLayerBlocks {
1919
* @param relative The relative location of the blockspace, in the layer.
2020
* @return A filled Optional if the state is mapped, empty otherwise.
2121
*/
22-
Optional<BlockState> getStateAtPosition(BlockPos relative);
22+
BlockState getStateAtPosition(BlockPos relative);
2323

2424
Stream<BlockPos> getPositions();
2525

@@ -29,11 +29,16 @@ public interface IRecipeLayerBlocks {
2929
*/
3030
int getNumberKnownComponents();
3131

32+
void rebuildComponentTotals();
33+
3234
Map<String, Integer> getKnownComponentTotals();
3335

3436
AxisAlignedBB getBounds();
3537

3638
boolean allIdentified();
3739

40+
Stream<BlockPos> getUnmappedPositions();
41+
Stream<String> getUnknownComponents();
42+
3843
Stream<BlockPos> getPositionsForComponent(String component);
3944
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ public int getNumberLayers() {
325325
return layers.size();
326326
}
327327

328+
@Override
329+
public Stream<IRecipeLayer> getLayers() {
330+
return layers.values().stream();
331+
}
332+
328333
public IRecipeComponents getComponents() {
329334
return this.components;
330335
}

src/main/java/dev/compactmods/crafting/recipes/blocks/ComponentPositionLookup.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ public ComponentPositionLookup() {
2020

2121
public void add(BlockPos location, String component) {
2222
components.putIfAbsent(location, component);
23+
componentTotals.putIfAbsent(component, 0);
24+
25+
// Increment totals to keep in sync
26+
componentTotals.put(component, componentTotals.get(component) + 1);
2327
}
2428

2529
public Collection<String> getComponents() {
26-
return components.values();
30+
return componentTotals.keySet();
2731
}
2832

2933
public Stream<BlockPos> getAllPositions() {
@@ -35,10 +39,6 @@ public boolean containsLocation(BlockPos location) {
3539
}
3640

3741
public Map<String, Integer> getComponentTotals() {
38-
if (!this.components.isEmpty() && !this.componentTotals.isEmpty())
39-
return this.componentTotals;
40-
41-
rebuildComponentTotals();
4242
return this.componentTotals;
4343
}
4444

src/main/java/dev/compactmods/crafting/recipes/blocks/ComponentPositionLookupCodec.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public <T> DataResult<ComponentPositionLookup> read(DynamicOps<T> ops, T input)
3535
}
3636

3737
lookup.components.putAll(RecipeHelper.convertMultiArrayToMap(mappedToArray));
38+
lookup.rebuildComponentTotals();
3839

3940
return DataResult.success(lookup);
4041
});

src/main/java/dev/compactmods/crafting/recipes/blocks/RecipeLayerBlocks.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public RecipeLayerBlocks(IRecipeLayerBlocks original) {
3333
if (key.isPresent()) lookup.components.put(pos.immutable(), key.get());
3434
else unmatchedStates.add(pos.immutable());
3535

36-
original.getStateAtPosition(pos).ifPresent(state -> states.put(pos.immutable(), state));
36+
BlockState oState = original.getStateAtPosition(pos);
37+
states.put(pos.immutable(), oState);
3738
});
3839
}
3940

@@ -60,13 +61,12 @@ public static RecipeLayerBlocks create(IBlockReader blocks, MiniaturizationRecip
6061
// Pre-populate a set of component keys from the recipe instance, so we don't have to do it later
6162
Optional<String> compKey = recipe.getRecipeComponentKey(state);
6263
if (compKey.isPresent())
63-
instance.lookup.components.put(normalizedPos, compKey.get());
64+
instance.lookup.add(normalizedPos, compKey.get());
6465
else
6566
instance.unmatchedStates.add(normalizedPos);
6667

6768
});
6869

69-
instance.lookup.rebuildComponentTotals();
7070
return instance;
7171
}
7272

@@ -77,11 +77,21 @@ public AxisAlignedBB getBounds() {
7777

7878
@Override
7979
public boolean allIdentified() {
80-
boolean lookupHasAllKeys = this.lookup.getComponents()
81-
.stream()
82-
.allMatch(states::containsKey);
80+
return unmatchedStates.isEmpty();
81+
}
82+
83+
@Override
84+
public Stream<BlockPos> getUnmappedPositions() {
85+
return unmatchedStates.stream();
86+
}
8387

84-
return lookupHasAllKeys && unmatchedStates.isEmpty();
88+
@Override
89+
public Stream<String> getUnknownComponents() {
90+
return getUnmappedPositions()
91+
.map(lookup::getRequiredComponentKeyForPosition)
92+
.filter(Optional::isPresent)
93+
.map(Optional::get)
94+
.distinct();
8595
}
8696

8797
@Override
@@ -96,8 +106,8 @@ public Optional<String> getComponentAtPosition(BlockPos relative) {
96106
}
97107

98108
@Override
99-
public Optional<BlockState> getStateAtPosition(BlockPos relative) {
100-
return Optional.ofNullable(states.get(relative));
109+
public BlockState getStateAtPosition(BlockPos relative) {
110+
return states.get(relative);
101111
}
102112

103113
@Override
@@ -115,6 +125,11 @@ public int getNumberKnownComponents() {
115125
return this.getKnownComponentTotals().keySet().size();
116126
}
117127

128+
@Override
129+
public void rebuildComponentTotals() {
130+
lookup.rebuildComponentTotals();
131+
}
132+
118133
@Override
119134
public Map<String, Integer> getKnownComponentTotals() {
120135
return lookup.componentTotals;

src/main/java/dev/compactmods/crafting/recipes/components/CCMiniRecipeComponents.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public void registerBlock(String key, IRecipeBlockComponent component) {
6161
blockComponents.put(key, component);
6262
}
6363

64+
@Override
65+
public void unregisterBlock(String key) {
66+
blockComponents.remove(key);
67+
}
68+
6469
@Override
6570
public void registerOther(String key, IRecipeComponent component) {
6671
otherComponents.put(key, component);

src/main/java/dev/compactmods/crafting/recipes/components/EmptyBlockComponent.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dev.compactmods.crafting.api.components.IRecipeBlockComponent;
55
import dev.compactmods.crafting.api.components.IRecipeComponent;
66
import dev.compactmods.crafting.api.components.RecipeComponentType;
7+
import net.minecraft.block.Block;
78
import net.minecraft.block.BlockState;
89
import net.minecraft.block.Blocks;
910

@@ -19,6 +20,11 @@ public boolean matches(BlockState state) {
1920
return state.isAir();
2021
}
2122

23+
@Override
24+
public Block getBlock() {
25+
return Blocks.AIR;
26+
}
27+
2228
@Override
2329
public BlockState getRenderState() {
2430
return Blocks.AIR.defaultBlockState();

0 commit comments

Comments
 (0)