Skip to content

Commit 199b5d7

Browse files
committed
Implement basic mixed component recipe layer
1 parent b5d588c commit 199b5d7

File tree

2 files changed

+111
-13
lines changed

2 files changed

+111
-13
lines changed

src/main/java/com/robotgryphon/compactcrafting/core/Registration.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.robotgryphon.compactcrafting.items.FieldProjectorItem;
77
import com.robotgryphon.compactcrafting.recipes.IRecipeLayer;
88
import com.robotgryphon.compactcrafting.recipes.MiniaturizationRecipe;
9-
import com.robotgryphon.compactcrafting.recipes.SingleComponentRecipeLayer;
9+
import com.robotgryphon.compactcrafting.recipes.MixedComponentRecipeLayer;
1010
import net.minecraft.block.Block;
1111
import net.minecraft.block.Blocks;
1212
import net.minecraft.block.material.Material;
@@ -83,40 +83,42 @@ public class Registration {
8383
{
8484
MiniaturizationRecipe rec = new MiniaturizationRecipe();
8585

86-
Set<BlockPos> template = new HashSet<>();
86+
Set<BlockPos> glassColl = new HashSet<>();
87+
Set<BlockPos> handleColl = new HashSet<>();
8788

88-
BlockPos[] complexPattern = new BlockPos[] {
89-
// Glass
89+
BlockPos[] glass = new BlockPos[]{
9090
new BlockPos(3, 0, 0),
9191
new BlockPos(4, 0, 0),
9292
new BlockPos(2, 0, 1),
9393
new BlockPos(5, 0, 1),
9494
new BlockPos(2, 0, 2),
9595
new BlockPos(5, 0, 2),
9696
new BlockPos(3, 0, 3),
97-
new BlockPos(4, 0, 3),
97+
new BlockPos(4, 0, 3)
98+
};
9899

99-
// Tail
100+
BlockPos[] handle = new BlockPos[]{
100101
new BlockPos(2, 0, 3),
101102
new BlockPos(1, 0, 4),
102103
new BlockPos(0, 0, 5)
103104
};
104105

105-
Collections.addAll(template, complexPattern);
106+
Collections.addAll(glassColl, glass);
107+
Collections.addAll(handleColl, handle);
106108

107-
rec.setLayers(new IRecipeLayer[]{
108-
new SingleComponentRecipeLayer("O", template),
109-
new SingleComponentRecipeLayer("G", template)
109+
MixedComponentRecipeLayer mixed = new MixedComponentRecipeLayer();
110+
mixed.addMultiple("S", handleColl);
111+
mixed.addMultiple("G", glassColl);
110112

111-
});
113+
rec.setLayers(new IRecipeLayer[]{mixed});
112114

113115
rec.catalyst = Items.ANVIL;
114116
rec.outputs = new ItemStack[]{
115117
new ItemStack(Items.CRYING_OBSIDIAN, 11)
116118
};
117119

118-
rec.addComponent("O", Blocks.OBSIDIAN.getDefaultState());
119-
rec.addComponent("G", Blocks.GLOWSTONE.getDefaultState());
120+
rec.addComponent("S", Blocks.STONE.getDefaultState());
121+
rec.addComponent("G", Blocks.GLASS.getDefaultState());
120122

121123
return rec;
122124
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.robotgryphon.compactcrafting.recipes;
2+
3+
import com.robotgryphon.compactcrafting.util.BlockSpaceUtil;
4+
import net.minecraft.util.math.AxisAlignedBB;
5+
import net.minecraft.util.math.BlockPos;
6+
import net.minecraft.world.IWorldReader;
7+
8+
import java.util.Collection;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
public class MixedComponentRecipeLayer implements IRecipeLayer {
13+
private AxisAlignedBB dimensions;
14+
private Map<BlockPos, String> componentLookup;
15+
private Map<String, Integer> totalCache;
16+
17+
public MixedComponentRecipeLayer() {
18+
this.dimensions = AxisAlignedBB.withSizeAtOrigin(0, 0, 0);
19+
this.componentLookup = new HashMap<>();
20+
}
21+
22+
public void add(String component, BlockPos location) {
23+
componentLookup.putIfAbsent(location, component);
24+
this.dimensions = BlockSpaceUtil.getBoundsForBlocks(componentLookup.keySet());
25+
}
26+
27+
public void addMultiple(String component, Collection<BlockPos> locations) {
28+
boolean recalc = false;
29+
for(BlockPos loc : locations) {
30+
if(!componentLookup.containsKey(loc)) {
31+
componentLookup.put(loc, component);
32+
recalc = true;
33+
}
34+
}
35+
36+
if(recalc)
37+
this.dimensions = BlockSpaceUtil.getBoundsForBlocks(componentLookup.keySet());
38+
}
39+
40+
@Override
41+
public boolean hasPadding(IWorldReader world, MiniaturizationRecipe recipe) {
42+
return false;
43+
}
44+
45+
@Override
46+
public AxisAlignedBB getDimensions() {
47+
return this.dimensions;
48+
}
49+
50+
@Override
51+
public Map<String, Integer> getComponentTotals() {
52+
if(this.totalCache != null)
53+
return this.totalCache;
54+
55+
Map<String, Integer> totals = new HashMap<>();
56+
componentLookup.forEach((pos, comp) -> {
57+
int prev = 0;
58+
if(!totals.containsKey(comp))
59+
totals.put(comp, 0);
60+
else
61+
prev = totals.get(comp);
62+
63+
totals.replace(comp, prev + 1);
64+
});
65+
66+
this.totalCache = totals;
67+
68+
return this.totalCache;
69+
}
70+
71+
@Override
72+
public String getRequiredComponentKeyForPosition(BlockPos pos) {
73+
if(componentLookup.containsKey(pos))
74+
return componentLookup.get(pos);
75+
76+
return null;
77+
}
78+
79+
@Override
80+
public Collection<BlockPos> getNonAirPositions() {
81+
return componentLookup.keySet();
82+
}
83+
84+
@Override
85+
public boolean isPositionRequired(BlockPos pos) {
86+
return componentLookup.containsKey(pos);
87+
}
88+
89+
@Override
90+
public int getNumberFilledPositions() {
91+
return getComponentTotals()
92+
.values()
93+
.stream()
94+
.reduce(0, Integer::sum);
95+
}
96+
}

0 commit comments

Comments
 (0)