Skip to content

Commit f363622

Browse files
committed
Basic tests of block rotation code (NYI)
1 parent 2d2fe95 commit f363622

File tree

9 files changed

+170
-68
lines changed

9 files changed

+170
-68
lines changed

src/main/java/com/robotgryphon/compactcrafting/blocks/FieldProjectorBlock.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static Optional<Direction> getDirection(IWorldReader world, BlockPos posi
5555
}
5656

5757
@Override
58+
@SuppressWarnings("deprecation")
5859
public void tick(BlockState state, ServerWorld worldIn, BlockPos pos, Random rand) {
5960
FieldProjectorTile tile = (FieldProjectorTile) worldIn.getTileEntity(pos);
6061
if(tile == null)
@@ -65,6 +66,7 @@ public void tick(BlockState state, ServerWorld worldIn, BlockPos pos, Random ran
6566

6667

6768
@Override
69+
@SuppressWarnings("deprecation")
6870
public VoxelShape getRenderShape(BlockState state, IBlockReader worldIn, BlockPos pos) {
6971
return VoxelShapes.empty();
7072
}
@@ -87,6 +89,7 @@ public TileEntity createTileEntity(BlockState state, IBlockReader world) {
8789
}
8890

8991
@Override
92+
@SuppressWarnings("deprecation")
9093
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
9194
if(world.isRemote)
9295
return ActionResultType.SUCCESS;

src/main/java/com/robotgryphon/compactcrafting/blocks/FieldProjectorTile.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.robotgryphon.compactcrafting.blocks;
22

3+
import com.robotgryphon.compactcrafting.CompactCrafting;
34
import com.robotgryphon.compactcrafting.core.Registration;
45
import com.robotgryphon.compactcrafting.crafting.CraftingHelper;
56
import com.robotgryphon.compactcrafting.field.FieldProjection;
67
import com.robotgryphon.compactcrafting.field.FieldProjectionSize;
78
import com.robotgryphon.compactcrafting.field.ProjectorHelper;
89
import com.robotgryphon.compactcrafting.recipes.MiniaturizationRecipe;
10+
import com.robotgryphon.compactcrafting.util.BlockSpaceUtil;
911
import com.robotgryphon.compactcrafting.world.ProjectionFieldSavedData;
1012
import com.robotgryphon.compactcrafting.world.ProjectorFieldData;
1113
import net.minecraft.block.BlockState;
@@ -17,14 +19,15 @@
1719
import net.minecraft.util.Direction;
1820
import net.minecraft.util.math.AxisAlignedBB;
1921
import net.minecraft.util.math.BlockPos;
22+
import net.minecraft.util.math.vector.Vector3i;
2023
import net.minecraft.world.server.ServerWorld;
2124
import net.minecraftforge.fml.RegistryObject;
2225

2326
import java.util.Collection;
2427
import java.util.List;
2528
import java.util.Optional;
29+
import java.util.Set;
2630
import java.util.stream.Collectors;
27-
import java.util.stream.Stream;
2831

2932
public class FieldProjectorTile extends TileEntity implements ITickableTileEntity {
3033

@@ -165,37 +168,65 @@ public void doRecipeScan() {
165168
if(!this.field.isPresent())
166169
return;
167170

171+
if(this.world == null)
172+
return;
173+
168174
FieldProjection fp = this.field.get();
169175
FieldProjectionSize size = fp.getFieldSize();
170176

171177
// Only the primary projector needs to worry about the recipe scan
172178
if(!isMainProjector())
173179
{
174180
Optional<BlockPos> center = ProjectorHelper.getCenterForSize(world, pos, size);
181+
if(!center.isPresent())
182+
return;
183+
175184
BlockPos masterPos = ProjectorHelper.getProjectorLocationForDirection(world, center.get(), Direction.NORTH, size);
176185

177186
FieldProjectorTile masterTile = (FieldProjectorTile) world.getTileEntity(masterPos);
187+
if(masterTile == null)
188+
return;
189+
178190
masterTile.doRecipeScan();
179191
return;
180192
}
181193

182194
AxisAlignedBB fieldBounds = fp.getBounds();
195+
BlockPos[] nonAirPositions = BlockPos.getAllInBox(fieldBounds)
196+
.filter(p -> !world.isAirBlock(p))
197+
.map(BlockPos::toImmutable)
198+
.toArray(BlockPos[]::new);
199+
200+
AxisAlignedBB filledBounds = BlockSpaceUtil.getBoundsForBlocks(nonAirPositions);
201+
202+
// ===========================================================================================================
203+
// RECIPE BEGIN
204+
// ===========================================================================================================
183205

184206
Collection<RegistryObject<MiniaturizationRecipe>> entries = Registration.MINIATURIZATION_RECIPES.getEntries();
185207
if (entries.isEmpty())
186208
return;
187209

188-
FieldProjection field = this.field.get();
189-
190-
Stream<MiniaturizationRecipe> matchedRecipes = entries
210+
Set<MiniaturizationRecipe> matchedRecipes = entries
191211
.stream()
192212
.map(RegistryObject::get)
193213
.filter(recipe -> recipe.fitsInFieldSize(size))
194-
.filter(recipe -> recipe.matches(world, size, fieldBounds));
214+
.filter(recipe -> BlockSpaceUtil.boundsFitsInside(filledBounds, recipe.getDimensions()))
215+
.collect(Collectors.toSet());
216+
217+
BlockPos[] testLocations = new BlockPos[] {
218+
new BlockPos(5, 0, 5),
219+
new BlockPos(7, 0, 5),
220+
new BlockPos(5, 0, 7)
221+
};
222+
223+
BlockPos[] rotatedCW = BlockSpaceUtil.rotateLayerPositions(testLocations, new Vector3i(5, 0, 5));
195224

196-
Optional<MiniaturizationRecipe> matched = matchedRecipes.findFirst();
225+
CompactCrafting.LOGGER.debug(".");
197226

198-
this.currentRecipe = matched.orElse(null);
227+
// Optional<MiniaturizationRecipe> matched = matchedRecipes.findFirst();
228+
//
229+
// this.currentRecipe = matched.orElse(null);
199230
}
200231

201232
private void tickCrafting() {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public class Registration {
8484
MiniaturizationRecipe rec = new MiniaturizationRecipe();
8585

8686
Set<BlockPos> template = new HashSet<>();
87+
88+
// cross pattern, see docs
8789
BlockPos[] layerBlocks = new BlockPos[]{
8890
new BlockPos(1, 0, 0),
8991
new BlockPos(0, 0, 1),
@@ -93,13 +95,9 @@ public class Registration {
9395

9496
Collections.addAll(template, layerBlocks);
9597

96-
97-
// Example: One obsidian block anywhere in the field can turn into crying obsidian
98-
rec.layers = new IRecipeLayer[]{
98+
rec.setLayers(new IRecipeLayer[]{
9999
new SingleComponentRecipeLayer("O", template)
100-
};
101-
102-
rec.recalculateDimensions();
100+
});
103101

104102
rec.catalyst = Items.ANVIL;
105103
rec.outputs = new ItemStack[]{

src/main/java/com/robotgryphon/compactcrafting/field/FieldHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void checkBlockPlacement(IWorld world, BlockPos pos, BlockUpdateTy
4747
for (BlockPos p : projectors) {
4848
FieldProjectorTile tile = (FieldProjectorTile) world.getTileEntity(p);
4949

50-
CompactCrafting.LOGGER.debug("Got a block placed near a projector: " + p.getCoordinatesAsString());
50+
// CompactCrafting.LOGGER.debug("Got a block placed near a projector: " + p.getCoordinatesAsString());
5151

5252
// Not a field projector tile. Somehow.
5353
if (tile == null) {
@@ -124,4 +124,6 @@ public static Map<BlockPos, String> remapLayerToRecipe(IWorldReader world, Minia
124124

125125
return relativeMap;
126126
}
127+
128+
127129
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import net.minecraft.util.math.BlockPos;
66
import net.minecraft.world.IWorldReader;
77

8+
import java.util.Collection;
89
import java.util.Map;
9-
import java.util.Set;
1010

1111
public interface IRecipeLayer {
1212

@@ -56,5 +56,5 @@ public interface IRecipeLayer {
5656
*
5757
* @return
5858
*/
59-
Set<BlockPos> getNonAirPositions();
59+
Collection<BlockPos> getNonAirPositions();
6060
}

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

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import com.robotgryphon.compactcrafting.crafting.CraftingHelper;
44
import com.robotgryphon.compactcrafting.field.FieldHelper;
55
import com.robotgryphon.compactcrafting.field.FieldProjectionSize;
6+
import com.robotgryphon.compactcrafting.util.BlockSpaceUtil;
67
import net.minecraft.block.BlockState;
78
import net.minecraft.item.Item;
89
import net.minecraft.item.ItemStack;
910
import net.minecraft.util.math.AxisAlignedBB;
10-
import net.minecraft.util.math.BlockPos;
11+
import net.minecraft.util.math.vector.Vector3d;
1112
import net.minecraft.world.IWorldReader;
1213
import net.minecraftforge.registries.ForgeRegistryEntry;
1314

@@ -20,7 +21,7 @@
2021

2122
public class MiniaturizationRecipe extends ForgeRegistryEntry<MiniaturizationRecipe> {
2223

23-
public IRecipeLayer[] layers;
24+
private IRecipeLayer[] layers;
2425
public Item catalyst;
2526
public ItemStack[] outputs;
2627
private AxisAlignedBB dimensions;
@@ -39,8 +40,26 @@ public MiniaturizationRecipe() {
3940
recalculateDimensions();
4041
}
4142

42-
public void recalculateDimensions() {
43-
this.dimensions = new AxisAlignedBB(BlockPos.ZERO);
43+
public void setLayers(IRecipeLayer[] layers) {
44+
this.layers = layers;
45+
this.recalculateDimensions();
46+
}
47+
48+
private void recalculateDimensions() {
49+
int height = this.layers.length;
50+
int x = 0;
51+
int z = 0;
52+
53+
for(IRecipeLayer layer : this.layers) {
54+
AxisAlignedBB dimensions = layer.getDimensions();
55+
if(dimensions.getXSize() > x)
56+
x = (int) Math.ceil(dimensions.getXSize());
57+
58+
if(dimensions.getZSize() > z)
59+
z = (int) Math.ceil(dimensions.getZSize());
60+
}
61+
62+
this.dimensions = new AxisAlignedBB(Vector3d.ZERO, new Vector3d(x, height, z));
4463
}
4564

4665
public boolean addComponent(String key, BlockState block) {
@@ -76,6 +95,10 @@ public boolean matches(IWorldReader world, FieldProjectionSize fieldSize, AxisAl
7695
if (!fitsInFieldSize(fieldSize))
7796
return false;
7897

98+
// We know that the recipe will at least fit inside the current projection field
99+
100+
// Check filled dimensions vs. recipe dimensions here?
101+
79102
Stream<AxisAlignedBB> fieldLayers = FieldHelper.splitIntoLayers(fieldSize, field);
80103
double recipeHeight = dimensions.getYSize();
81104
int maxRecipeBottomLayer = (int) Math.floor(field.maxY - recipeHeight);
@@ -88,9 +111,11 @@ public boolean matches(IWorldReader world, FieldProjectionSize fieldSize, AxisAl
88111
.filter(layer -> CraftingHelper.hasBlocksInField(world, layer))
89112
.collect(Collectors.toList());
90113

114+
return false;
115+
91116
// Check each layer bottom to see if it matches the bottom layer of this recipe
92-
return possibleRecipeBottomLayers.stream()
93-
.anyMatch(fieldLayer -> layers[0].matchesFieldLayer(world, this, fieldSize, fieldLayer));
117+
// return possibleRecipeBottomLayers.stream()
118+
// .anyMatch(fieldLayer -> layers[0].matchesFieldLayer(world, this, fieldSize, fieldLayer));
94119
}
95120

96121
public ItemStack[] getOutputs() {
@@ -114,4 +139,12 @@ public Optional<String> getRecipeComponentKey(BlockState state) {
114139

115140
return Optional.empty();
116141
}
142+
143+
public boolean fitsInDimensions(AxisAlignedBB bounds) {
144+
return BlockSpaceUtil.boundsFitsInside(this.dimensions, bounds);
145+
}
146+
147+
public AxisAlignedBB getDimensions() {
148+
return this.dimensions;
149+
}
117150
}

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

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.robotgryphon.compactcrafting.recipes;
22

3+
import com.robotgryphon.compactcrafting.util.BlockSpaceUtil;
34
import net.minecraft.block.BlockState;
45
import net.minecraft.util.math.AxisAlignedBB;
56
import net.minecraft.util.math.BlockPos;
6-
import net.minecraft.util.math.MutableBoundingBox;
77
import net.minecraft.util.math.vector.Vector3i;
88
import net.minecraft.world.IWorldReader;
99

1010
import java.util.Arrays;
11-
import java.util.Collection;
1211
import java.util.Optional;
13-
import java.util.Set;
1412
import java.util.stream.Stream;
1513

1614
public abstract class RecipeHelper {
@@ -45,43 +43,6 @@ public static BlockPos[] normalizeLayerPositions(AxisAlignedBB layerBounds, Bloc
4543
.toArray(BlockPos[]::new);
4644
}
4745

48-
public static AxisAlignedBB getBoundsForBlocks(Collection<BlockPos> filled) {
49-
if(filled.size() == 0)
50-
return AxisAlignedBB.withSizeAtOrigin(0, 0, 0);
51-
52-
MutableBoundingBox trimmedBounds = null;
53-
for(BlockPos filledPos : filled) {
54-
if(trimmedBounds == null) {
55-
trimmedBounds = new MutableBoundingBox(filledPos, filledPos);
56-
continue;
57-
}
58-
59-
MutableBoundingBox checkPos = new MutableBoundingBox(filledPos, filledPos);
60-
if(!trimmedBounds.intersectsWith(checkPos))
61-
trimmedBounds.expandTo(checkPos);
62-
}
63-
64-
return AxisAlignedBB.toImmutable(trimmedBounds);
65-
}
66-
67-
public static String[][] getTrimmedTemplateForLayer(IRecipeLayer layer, AxisAlignedBB fieldBounds) {
68-
Set<BlockPos> nonAir = layer.getNonAirPositions();
69-
AxisAlignedBB bounds = getBoundsForBlocks(nonAir);
70-
71-
int dim = (int) fieldBounds.getXSize();
72-
73-
String[][] components = new String[dim][dim];
74-
75-
for(BlockPos filledPos : nonAir) {
76-
String layerRequirement = layer.getRequiredComponentKeyForPosition(filledPos);
77-
78-
if(layerRequirement != null)
79-
components[filledPos.getX()][filledPos.getZ()] = layerRequirement;
80-
}
81-
82-
return components;
83-
}
84-
8546
/**
8647
* Checks if a layer matches a template by extracting information about components
8748
* from the various filled positions.
@@ -92,7 +53,7 @@ public static String[][] getTrimmedTemplateForLayer(IRecipeLayer layer, AxisAlig
9253
* @return
9354
*/
9455
public static boolean layerMatchesTemplate(IWorldReader world, MiniaturizationRecipe recipe, IRecipeLayer layer, AxisAlignedBB fieldBounds, BlockPos[] filledLocations) {
95-
AxisAlignedBB trimmedBounds = getBoundsForBlocks(Arrays.asList(filledLocations));
56+
AxisAlignedBB trimmedBounds = BlockSpaceUtil.getBoundsForBlocks(Arrays.asList(filledLocations));
9657
BlockPos[] normalizedLocations = normalizeLayerPositions(trimmedBounds, filledLocations);
9758

9859
// Finally, simply check the normalized template

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.robotgryphon.compactcrafting.CompactCrafting;
44
import com.robotgryphon.compactcrafting.field.FieldHelper;
55
import com.robotgryphon.compactcrafting.field.FieldProjectionSize;
6+
import com.robotgryphon.compactcrafting.util.BlockSpaceUtil;
67
import net.minecraft.block.BlockState;
78
import net.minecraft.util.math.AxisAlignedBB;
89
import net.minecraft.util.math.BlockPos;
@@ -27,12 +28,12 @@ public class SingleComponentRecipeLayer implements IRecipeLayer {
2728
*
2829
* = [0, 0], [2, 0], [1, 1], [0, 2], [2, 2]
2930
*/
30-
private Set<BlockPos> filledPositions;
31+
private Collection<BlockPos> filledPositions;
3132

32-
public SingleComponentRecipeLayer(String key, Set<BlockPos> filledPositions) {
33+
public SingleComponentRecipeLayer(String key, Collection<BlockPos> filledPositions) {
3334
this.componentKey = key;
3435
this.filledPositions = filledPositions;
35-
this.dimensions = AxisAlignedBB.withSizeAtOrigin(0, 0, 0);
36+
this.dimensions = BlockSpaceUtil.getBoundsForBlocks(filledPositions);
3637
}
3738

3839
@Override
@@ -74,7 +75,7 @@ public boolean matchesFieldLayer(IWorldReader world, MiniaturizationRecipe recip
7475
BlockPos[] normalizedFilledPositions = RecipeHelper.normalizeLayerPositions(this.dimensions, fieldPositions);
7576

7677
// Create a minimum-filled bounds of blocks in the field
77-
AxisAlignedBB trimmedBounds = RecipeHelper.getBoundsForBlocks(Arrays.asList(normalizedFilledPositions));
78+
AxisAlignedBB trimmedBounds = BlockSpaceUtil.getBoundsForBlocks(Arrays.asList(normalizedFilledPositions));
7879

7980
// Whitespace trim done - no padding needed, min and max bounds are already correct
8081

@@ -101,7 +102,7 @@ public String getRequiredComponentKeyForPosition(BlockPos pos) {
101102
}
102103

103104
@Override
104-
public Set<BlockPos> getNonAirPositions() {
105+
public Collection<BlockPos> getNonAirPositions() {
105106
return filledPositions;
106107
}
107108
}

0 commit comments

Comments
 (0)