Skip to content

Commit d8fa842

Browse files
committed
Implement hollow layer type; overhaul fluid layers
1 parent 17c9d50 commit d8fa842

File tree

9 files changed

+195
-26
lines changed

9 files changed

+195
-26
lines changed

src/main/java/com/robotgryphon/compactcrafting/recipes/MiniaturizationRecipe.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.robotgryphon.compactcrafting.field.FieldProjectionSize;
44
import com.robotgryphon.compactcrafting.field.MiniaturizationFieldBlockData;
5+
import com.robotgryphon.compactcrafting.recipes.exceptions.MiniaturizationRecipeException;
6+
import com.robotgryphon.compactcrafting.recipes.layers.IDynamicRecipeLayer;
57
import com.robotgryphon.compactcrafting.recipes.layers.IFixedLayerDimensions;
68
import com.robotgryphon.compactcrafting.recipes.layers.IRecipeLayer;
79
import com.robotgryphon.compactcrafting.util.BlockSpaceUtil;
@@ -65,6 +67,13 @@ private void recalculateDimensions() {
6567
}
6668

6769
this.dimensions = new AxisAlignedBB(Vector3d.ZERO, new Vector3d(x, height, z));
70+
71+
// Update all the dynamic recipe layers
72+
Arrays.stream(this.layers)
73+
.filter(layer -> layer instanceof IDynamicRecipeLayer)
74+
.forEach(dynamicLayer -> {
75+
((IDynamicRecipeLayer) dynamicLayer).setRecipeDimensions(dimensions);
76+
});
6877
}
6978

7079
public boolean addComponent(String key, BlockState block) {
@@ -195,7 +204,7 @@ public int getComponentRequiredCount(String i) {
195204

196205
int required = 0;
197206
for (IRecipeLayer layer : this.layers) {
198-
Map<String, Integer> layerTotals = layer.getComponentTotals(this.dimensions);
207+
Map<String, Integer> layerTotals = layer.getComponentTotals();
199208

200209
if (layerTotals.containsKey(i))
201210
required += layerTotals.get(i);
@@ -234,7 +243,7 @@ public boolean areLayerPositionsCorrect(IRecipeLayer layer, AxisAlignedBB fieldF
234243
return false;
235244

236245
int totalFilled = filledPositions.length;
237-
int requiredFilled = layer.getNumberFilledPositions(this.dimensions);
246+
int requiredFilled = layer.getNumberFilledPositions();
238247

239248
// Early exit if we don't have the correct number of blocks in the layer
240249
if (totalFilled != requiredFilled)
@@ -293,4 +302,17 @@ public ItemStack getCatalyst() {
293302
public void setCatalyst(ItemStack c) {
294303
this.catalyst = c;
295304
}
305+
306+
public void setFluidDimensions(AxisAlignedBB dimensions) throws MiniaturizationRecipeException {
307+
if(Arrays.stream(this.layers).anyMatch(layer -> !(layer instanceof IDynamicRecipeLayer)))
308+
throw new MiniaturizationRecipeException("Tried to set fluid dimensions when a non-fluid layer exists.");
309+
310+
this.dimensions = dimensions;
311+
Arrays.stream(this.layers)
312+
.filter(l -> l instanceof IDynamicRecipeLayer)
313+
.forEach(layer -> {
314+
IDynamicRecipeLayer dynamic = (IDynamicRecipeLayer) layer;
315+
dynamic.setRecipeDimensions(dimensions);
316+
});
317+
}
296318
}

src/main/java/com/robotgryphon/compactcrafting/recipes/json/LayerDeserializer.java

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

33
import com.google.gson.*;
44
import com.robotgryphon.compactcrafting.recipes.json.loaders.FilledLayerLoader;
5+
import com.robotgryphon.compactcrafting.recipes.json.loaders.HollowLayerLoader;
56
import com.robotgryphon.compactcrafting.recipes.json.loaders.ILayerLoader;
67
import com.robotgryphon.compactcrafting.recipes.json.loaders.MixedLayerLoader;
78
import com.robotgryphon.compactcrafting.recipes.layers.IRecipeLayer;
@@ -10,6 +11,11 @@
1011
import java.util.Optional;
1112

1213
public class LayerDeserializer implements JsonDeserializer<IRecipeLayer> {
14+
15+
private static ILayerLoader MIXED = new MixedLayerLoader();
16+
private static ILayerLoader HOLLOW = new HollowLayerLoader();
17+
private static ILayerLoader FILLED = new FilledLayerLoader();
18+
1319
@Override
1420
public IRecipeLayer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
1521
JsonObject root = json.getAsJsonObject();
@@ -21,20 +27,19 @@ public IRecipeLayer deserialize(JsonElement json, Type typeOfT, JsonDeserializat
2127
String type = root.get("type").getAsString();
2228
switch(type.toLowerCase()) {
2329
case "mixed":
24-
// Mixed layer type, use that loader
25-
loader = Optional.of(new MixedLayerLoader());
30+
// Mixed layer type
31+
loader = Optional.of(MIXED);
2632
break;
2733

2834
case "filled":
2935
case "solid":
3036
// Filled layer type
31-
// Single or make new FilledLayerType?
32-
loader = Optional.of(new FilledLayerLoader());
37+
loader = Optional.of(FILLED);
3338
break;
3439

3540
case "hollow":
3641
// Hollow layer type
37-
loader = Optional.empty();
42+
loader = Optional.of(HOLLOW);
3843
break;
3944

4045
default:
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.robotgryphon.compactcrafting.recipes.json.loaders;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParseException;
5+
import com.robotgryphon.compactcrafting.recipes.layers.HollowComponentRecipeLayer;
6+
import com.robotgryphon.compactcrafting.recipes.layers.IRecipeLayer;
7+
8+
public class HollowLayerLoader implements ILayerLoader {
9+
@Override
10+
public IRecipeLayer createLayerFromDefinition(JsonObject layer) {
11+
if(!layer.has("component"))
12+
throw new JsonParseException("Hollow layer definition does not have an associated component key.");
13+
14+
String component = layer.get("component").getAsString();
15+
16+
return new HollowComponentRecipeLayer(component);
17+
}
18+
}

src/main/java/com/robotgryphon/compactcrafting/recipes/layers/FilledComponentRecipeLayer.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,39 @@
88
import java.util.Map;
99
import java.util.stream.Collectors;
1010

11-
public class FilledComponentRecipeLayer implements IRecipeLayer {
11+
public class FilledComponentRecipeLayer implements IRecipeLayer, IDynamicRecipeLayer {
1212

1313
private String componentKey;
14+
private AxisAlignedBB recipeDimensions;
1415

1516
public FilledComponentRecipeLayer(String component) {
1617
this.componentKey = component;
1718
}
1819

1920
@Override
20-
public Map<String, Integer> getComponentTotals(AxisAlignedBB recipeDims) {
21-
return Collections.singletonMap(componentKey, getNumberFilledPositions(recipeDims));
21+
public Map<String, Integer> getComponentTotals() {
22+
return Collections.singletonMap(componentKey, getNumberFilledPositions());
2223
}
2324

2425
@Override
2526
public String getRequiredComponentKeyForPosition(BlockPos pos) {
2627
return componentKey;
2728
}
2829

30+
/**
31+
* Gets a set of non-air positions that are required for the layer to match.
32+
* This is expected to trim the air positions off the edges and return the positions with NW
33+
* in the 0, 0 position.
34+
*
35+
* @return
36+
*/
2937
@Override
30-
public Collection<BlockPos> getNonAirPositions(AxisAlignedBB recipeDims) {
31-
AxisAlignedBB layerBounds = new AxisAlignedBB(0, 0, 0, recipeDims.getXSize(), 1, recipeDims.getZSize());
38+
public Collection<BlockPos> getNonAirPositions() {
39+
AxisAlignedBB layerBounds = new AxisAlignedBB(0, 0, 0, recipeDimensions.getXSize(), 1, recipeDimensions.getZSize());
3240
return BlockPos.getAllInBox(layerBounds)
3341
.parallel()
3442
.map(BlockPos::toImmutable)
3543
.collect(Collectors.toSet());
36-
3744
}
3845

3946
@Override
@@ -42,11 +49,31 @@ public boolean isPositionRequired(BlockPos pos) {
4249
}
4350

4451
@Override
45-
public int getNumberFilledPositions(AxisAlignedBB recipeDims) {
46-
return (int) Math.ceil(recipeDims.getXSize() * recipeDims.getYSize());
52+
public int getNumberFilledPositions() {
53+
return (int) Math.ceil(recipeDimensions.getXSize() * recipeDimensions.getYSize());
4754
}
4855

4956
public void setComponent(String component) {
5057
this.componentKey = component;
5158
}
59+
60+
/**
61+
* Used to update a recipe layer to change the size of the recipe base.
62+
*
63+
* @param dimensions The new dimensions of the recipe.
64+
*/
65+
@Override
66+
public void setRecipeDimensions(AxisAlignedBB dimensions) {
67+
this.recipeDimensions = dimensions;
68+
this.recalculateRequirements();
69+
}
70+
71+
/**
72+
* Used to recalculate dynamic-sized recipe layers. Expected to be called
73+
* any time components or base recipe dimensions change.
74+
*/
75+
@Override
76+
public void recalculateRequirements() {
77+
78+
}
5279
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.robotgryphon.compactcrafting.recipes.layers;
2+
3+
import net.minecraft.util.math.AxisAlignedBB;
4+
import net.minecraft.util.math.BlockPos;
5+
6+
import java.util.Collection;
7+
import java.util.Collections;
8+
import java.util.Map;
9+
import java.util.Set;
10+
import java.util.stream.Collectors;
11+
12+
public class HollowComponentRecipeLayer implements IRecipeLayer, IDynamicRecipeLayer {
13+
14+
private String componentKey;
15+
private AxisAlignedBB recipeDimensions;
16+
private Collection<BlockPos> filledPositions;
17+
18+
public HollowComponentRecipeLayer(String component) {
19+
this.componentKey = component;
20+
}
21+
22+
@Override
23+
public Map<String, Integer> getComponentTotals() {
24+
return Collections.singletonMap(componentKey, getNumberFilledPositions());
25+
}
26+
27+
@Override
28+
public String getRequiredComponentKeyForPosition(BlockPos pos) {
29+
return componentKey;
30+
}
31+
32+
@Override
33+
public Collection<BlockPos> getNonAirPositions() {
34+
return this.filledPositions;
35+
}
36+
37+
@Override
38+
public boolean isPositionRequired(BlockPos pos) {
39+
return true;
40+
}
41+
42+
@Override
43+
public int getNumberFilledPositions() {
44+
return filledPositions.size();
45+
}
46+
47+
public void setComponent(String component) {
48+
this.componentKey = component;
49+
}
50+
51+
@Override
52+
public void setRecipeDimensions(AxisAlignedBB dimensions) {
53+
this.recipeDimensions = dimensions;
54+
this.recalculateRequirements();
55+
}
56+
57+
/**
58+
* Used to recalculate dynamic-sized recipe layers. Expected to be called
59+
* any time components or base recipe dimensions change.
60+
*/
61+
@Override
62+
public void recalculateRequirements() {
63+
this.filledPositions = getWallPositions();
64+
}
65+
66+
public Collection<BlockPos> getWallPositions() {
67+
AxisAlignedBB layerBounds = new AxisAlignedBB(0, 0, 0, recipeDimensions.getXSize() - 1, 0, recipeDimensions.getZSize() - 1);
68+
AxisAlignedBB insideBounds = layerBounds.offset(1, 0, 1).contract(2, 0, 2);
69+
70+
Set<BlockPos> positions = BlockPos.getAllInBox(layerBounds)
71+
.map(BlockPos::toImmutable)
72+
.collect(Collectors.toSet());
73+
74+
Set<BlockPos> inside = BlockPos.getAllInBox(insideBounds)
75+
.map(BlockPos::toImmutable)
76+
.collect(Collectors.toSet());
77+
78+
positions.removeAll(inside);
79+
return positions;
80+
}
81+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.robotgryphon.compactcrafting.recipes.layers;
2+
3+
import net.minecraft.util.math.AxisAlignedBB;
4+
5+
public interface IDynamicRecipeLayer {
6+
/**
7+
* Used to update a recipe layer to change the size of the recipe base.
8+
* @param dimensions The new dimensions of the recipe.
9+
*/
10+
void setRecipeDimensions(AxisAlignedBB dimensions);
11+
12+
/**
13+
* Used to recalculate dynamic-sized recipe layers. Expected to be called
14+
* any time components or base recipe dimensions change.
15+
*/
16+
void recalculateRequirements();
17+
}

src/main/java/com/robotgryphon/compactcrafting/recipes/layers/IRecipeLayer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.robotgryphon.compactcrafting.recipes.layers;
22

3-
import net.minecraft.util.math.AxisAlignedBB;
43
import net.minecraft.util.math.BlockPos;
54

65
import java.util.Collection;
76
import java.util.Map;
87

98
public interface IRecipeLayer {
109

11-
Map<String, Integer> getComponentTotals(AxisAlignedBB recipeDims);
10+
Map<String, Integer> getComponentTotals();
1211

1312
/**
1413
* Gets a component key for the given (normalized) position.
@@ -24,9 +23,9 @@ public interface IRecipeLayer {
2423
*
2524
* @return
2625
*/
27-
Collection<BlockPos> getNonAirPositions(AxisAlignedBB recipeDims);
26+
Collection<BlockPos> getNonAirPositions();
2827

2928
boolean isPositionRequired(BlockPos pos);
3029

31-
int getNumberFilledPositions(AxisAlignedBB recipeDims);
30+
int getNumberFilledPositions();
3231
}

src/main/java/com/robotgryphon/compactcrafting/recipes/layers/MixedComponentRecipeLayer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public AxisAlignedBB getDimensions() {
4242
}
4343

4444
@Override
45-
public Map<String, Integer> getComponentTotals(AxisAlignedBB recipeDims) {
45+
public Map<String, Integer> getComponentTotals() {
4646
if(this.totalCache != null)
4747
return this.totalCache;
4848

@@ -71,7 +71,7 @@ public String getRequiredComponentKeyForPosition(BlockPos pos) {
7171
}
7272

7373
@Override
74-
public Collection<BlockPos> getNonAirPositions(AxisAlignedBB recipeDims) {
74+
public Collection<BlockPos> getNonAirPositions() {
7575
return componentLookup.keySet();
7676
}
7777

@@ -81,8 +81,8 @@ public boolean isPositionRequired(BlockPos pos) {
8181
}
8282

8383
@Override
84-
public int getNumberFilledPositions(AxisAlignedBB recipeDims) {
85-
return getComponentTotals(recipeDims)
84+
public int getNumberFilledPositions() {
85+
return getComponentTotals()
8686
.values()
8787
.stream()
8888
.reduce(0, Integer::sum);

src/main/java/com/robotgryphon/compactcrafting/recipes/layers/SingleComponentRecipeLayer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public AxisAlignedBB getDimensions() {
4242
}
4343

4444
@Override
45-
public Map<String, Integer> getComponentTotals(AxisAlignedBB recipeDims) {
45+
public Map<String, Integer> getComponentTotals() {
4646
double volume = dimensions.getXSize() * dimensions.getYSize() * dimensions.getZSize();
4747
return Collections.singletonMap(componentKey, (int) Math.ceil(volume));
4848
}
@@ -53,7 +53,7 @@ public String getRequiredComponentKeyForPosition(BlockPos pos) {
5353
}
5454

5555
@Override
56-
public Collection<BlockPos> getNonAirPositions(AxisAlignedBB recipeDims) {
56+
public Collection<BlockPos> getNonAirPositions() {
5757
return filledPositions;
5858
}
5959

@@ -63,7 +63,7 @@ public boolean isPositionRequired(BlockPos pos) {
6363
}
6464

6565
@Override
66-
public int getNumberFilledPositions(AxisAlignedBB recipeDims) {
66+
public int getNumberFilledPositions() {
6767
return filledPositions.size();
6868
}
6969
}

0 commit comments

Comments
 (0)