From d7ad6ed0c1d61b8f1b57e30f06d74f525f6b3214 Mon Sep 17 00:00:00 2001 From: Adrien1106 Date: Mon, 17 Jun 2024 10:52:21 +0200 Subject: [PATCH] feat: added and improved logic to merge step with slabs --- .../java/fr/adrien1106/reframed/ReFramed.java | 2 +- .../reframed/block/ReFramedBlock.java | 14 +++ .../reframed/block/ReFramedDoubleBlock.java | 8 -- .../reframed/block/ReFramedSlabBlock.java | 32 ++--- .../reframed/block/ReFramedStepBlock.java | 113 +++++++++--------- 5 files changed, 87 insertions(+), 82 deletions(-) diff --git a/src/main/java/fr/adrien1106/reframed/ReFramed.java b/src/main/java/fr/adrien1106/reframed/ReFramed.java index 098f8dd..40139c5 100644 --- a/src/main/java/fr/adrien1106/reframed/ReFramed.java +++ b/src/main/java/fr/adrien1106/reframed/ReFramed.java @@ -37,7 +37,7 @@ public class ReFramed implements ModInitializer { public static final String MODID = "reframed"; public static final ArrayList BLOCKS = new ArrayList<>(); - public static Block + public static ReFramedBlock CUBE, SMALL_CUBE, SMALL_CUBES_STEP, STAIR, STAIRS_CUBE, diff --git a/src/main/java/fr/adrien1106/reframed/block/ReFramedBlock.java b/src/main/java/fr/adrien1106/reframed/block/ReFramedBlock.java index ceac63c..c2cd457 100644 --- a/src/main/java/fr/adrien1106/reframed/block/ReFramedBlock.java +++ b/src/main/java/fr/adrien1106/reframed/block/ReFramedBlock.java @@ -20,6 +20,7 @@ import net.minecraft.util.collection.DefaultedList; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; +import net.minecraft.util.math.Vec3d; import net.minecraft.util.shape.VoxelShape; import net.minecraft.util.shape.VoxelShapes; import net.minecraft.world.BlockView; @@ -135,6 +136,19 @@ public class ReFramedBlock extends Block implements BlockEntityProvider { } onPlaced(world, pos, state, placer, stack); } + + public boolean matchesShape(Vec3d hit, BlockPos pos, BlockState state) { + return matchesShape(hit, pos, state, 0); + } + + public boolean matchesShape(Vec3d hit, BlockPos pos, BlockState state, int i) { + Vec3d rel = BlockHelper.getRelativePos(hit, pos); + return matchesShape(rel, getShape(state, i)); + } + + public boolean matchesShape(Vec3d rel_hit, VoxelShape shape) { + return BlockHelper.cursorMatchesFace(shape, rel_hit); + } @Override public VoxelShape getCollisionShape(BlockState state, BlockView view, BlockPos pos, ShapeContext ctx) { diff --git a/src/main/java/fr/adrien1106/reframed/block/ReFramedDoubleBlock.java b/src/main/java/fr/adrien1106/reframed/block/ReFramedDoubleBlock.java index 7f67064..459c5b0 100644 --- a/src/main/java/fr/adrien1106/reframed/block/ReFramedDoubleBlock.java +++ b/src/main/java/fr/adrien1106/reframed/block/ReFramedDoubleBlock.java @@ -49,14 +49,6 @@ public abstract class ReFramedDoubleBlock extends ReFramedBlock { return 0; } - public boolean matchesShape(Vec3d hit, BlockPos pos, BlockState state, int i) { - Vec3d rel = BlockHelper.getRelativePos(hit, pos); - return BlockHelper.cursorMatchesFace( - getShape(state, i), - rel - ); - } - @Override public boolean isTransparent(BlockState state, BlockView world, BlockPos pos) { return world.getBlockEntity(pos) instanceof ThemeableBlockEntity framed_entity diff --git a/src/main/java/fr/adrien1106/reframed/block/ReFramedSlabBlock.java b/src/main/java/fr/adrien1106/reframed/block/ReFramedSlabBlock.java index 39a813e..0c8252e 100644 --- a/src/main/java/fr/adrien1106/reframed/block/ReFramedSlabBlock.java +++ b/src/main/java/fr/adrien1106/reframed/block/ReFramedSlabBlock.java @@ -42,21 +42,23 @@ public class ReFramedSlabBlock extends WaterloggableReFramedBlock { @Override public boolean canReplace(BlockState state, ItemPlacementContext context) { - if (context.getPlayer() == null) return false; - return !( - context.getPlayer().isSneaking() - || !(context.getStack().getItem() instanceof BlockItem block_item) - || !( - block_item.getBlock() == this - && ((ReFramedSlabsCubeBlock) ReFramed.SLABS_CUBE) - .matchesShape( - context.getHitPos(), - context.getBlockPos(), - ReFramed.SLABS_CUBE.getDefaultState().with(AXIS, state.get(FACING).getAxis()), - state.get(FACING).getDirection() == Direction.AxisDirection.POSITIVE ? 1 : 2 - ) - ) - ); + if (context.getPlayer() == null + || context.getPlayer().isSneaking() + || !(context.getStack().getItem() instanceof BlockItem block_item) + ) return false; + + // allow replacing with slab and step + if (block_item.getBlock() != this && block_item.getBlock() != ReFramed.STEP) + return false; + + // check if the player is clicking on the inner part of the block + return ReFramed.SLABS_CUBE + .matchesShape( + context.getHitPos(), + context.getBlockPos(), + ReFramed.SLABS_CUBE.getDefaultState().with(AXIS, state.get(FACING).getAxis()), + state.get(FACING).getDirection() == Direction.AxisDirection.POSITIVE ? 1 : 2 + ); } @Nullable diff --git a/src/main/java/fr/adrien1106/reframed/block/ReFramedStepBlock.java b/src/main/java/fr/adrien1106/reframed/block/ReFramedStepBlock.java index 121cace..f344db9 100644 --- a/src/main/java/fr/adrien1106/reframed/block/ReFramedStepBlock.java +++ b/src/main/java/fr/adrien1106/reframed/block/ReFramedStepBlock.java @@ -22,10 +22,9 @@ import org.jetbrains.annotations.Nullable; import java.util.Map; import static fr.adrien1106.reframed.util.VoxelHelper.VoxelListBuilder; -import static fr.adrien1106.reframed.util.blocks.BlockProperties.EDGE; +import static fr.adrien1106.reframed.util.blocks.BlockProperties.*; import static fr.adrien1106.reframed.util.blocks.BlockProperties.STAIR_SHAPE; import static net.minecraft.state.property.Properties.*; -import static net.minecraft.state.property.Properties.WATERLOGGED; public class ReFramedStepBlock extends WaterloggableReFramedBlock { @@ -43,48 +42,32 @@ public class ReFramedStepBlock extends WaterloggableReFramedBlock { @Override public boolean canReplace(BlockState state, ItemPlacementContext context) { - if (context.getPlayer() == null) return false; - Edge edge = state.get(EDGE); - return !( - context.getPlayer().isSneaking() + if (context.getPlayer() == null + || context.getPlayer().isSneaking() || !(context.getStack().getItem() instanceof BlockItem block_item) - || ( - !( - block_item.getBlock() == ReFramed.STAIR - && ((ReFramedStairsCubeBlock) ReFramed.STAIRS_CUBE) - .matchesShape( - context.getHitPos(), - context.getBlockPos(), - ReFramed.STAIRS_CUBE.getDefaultState().with(EDGE, edge.opposite()), - 1 - ) + ) return false; - ) - && !( - block_item.getBlock() == this - && ( - ((ReFramedStepsSlabBlock) ReFramed.STEPS_SLAB) - .matchesShape( - context.getHitPos(), - context.getBlockPos(), - ReFramed.STEPS_SLAB.getDefaultState() - .with(FACING, edge.getFirstDirection()) - .with(AXIS, edge.getSecondDirection().getAxis()), - edge.getSecondDirection().getDirection() == Direction.AxisDirection.POSITIVE ? 1 : 2 - ) - || ((ReFramedStepsSlabBlock) ReFramed.STEPS_SLAB) - .matchesShape( - context.getHitPos(), - context.getBlockPos(), - ReFramed.STEPS_SLAB.getDefaultState() - .with(FACING, edge.getSecondDirection()) - .with(AXIS, edge.getFirstDirection().getAxis()), - edge.getFirstDirection().getDirection() == Direction.AxisDirection.POSITIVE ? 1 : 2 - ) - ) - ) - ) - ); + Edge edge = state.get(EDGE); + // allow replacing with stair + if (block_item.getBlock() == ReFramed.STAIR) + return ReFramed.STAIRS_CUBE + .matchesShape( + context.getHitPos(), + context.getBlockPos(), + ReFramed.STAIRS_CUBE.getDefaultState().with(EDGE, edge.opposite()), + 1 + ); + + if (block_item.getBlock() == this) + return ReFramed.STAIR + .matchesShape( + context.getHitPos(), + context.getBlockPos(), + ReFramed.STAIR.getDefaultState() + .with(EDGE, edge.opposite()) + ); + + return false; } @Nullable @@ -101,23 +84,37 @@ public class ReFramedStepBlock extends WaterloggableReFramedBlock { if (current_state.isOf(this)) { Vec3d hit = ctx.getHitPos(); Edge edge = current_state.get(EDGE); - Direction dir = edge.getFirstDirection(); - ReFramedStepsSlabBlock block = ((ReFramedStepsSlabBlock) ReFramed.STEPS_SLAB); - BlockState state = block.getDefaultState() - .with(FACING, dir) - .with(AXIS, edge.getOtherDirection(dir).getAxis()) + + // Steps Slab + if (matchesShape(hit, pos, + current_state.with(EDGE, edge.getOpposite(edge.getFirstDirection())) + )) return ReFramed.STEPS_SLAB.getDefaultState() + .with(FACING, edge.getFirstDirection()) + .with(AXIS, edge.getSecondDirection().getAxis()) .with(WATERLOGGED, current_state.get(WATERLOGGED)); - if (!block.matchesShape( - hit, pos, - state, - edge.getOtherDirection(dir).getDirection() == Direction.AxisDirection.POSITIVE ? 1 : 2 - )) { - dir = edge.getSecondDirection(); - state = state - .with(FACING, dir) - .with(AXIS, edge.getOtherDirection(dir).getAxis()); - } - return state; + + else if (matchesShape(hit, pos, + current_state.with(EDGE, edge.getOpposite(edge.getSecondDirection())) + )) return ReFramed.STEPS_SLAB.getDefaultState() + .with(FACING, edge.getFirstDirection()) + .with(AXIS, edge.getSecondDirection().getAxis()) + .with(WATERLOGGED, current_state.get(WATERLOGGED)); + + // Steps Cross + return null; // TODO STEP cross + } else if (current_state.isOf(ReFramed.SLAB)) { + Direction facing = current_state.get(FACING); + Edge edge; + + // Slabs Stair + if (ctx.getSide() == facing || ctx.getSide() == facing.getOpposite()) + edge = BlockHelper.getPlacementEdge(ctx); + else + edge = Edge.getByDirections(facing, ctx.getSide().getOpposite()); + return ReFramed.SLABS_STAIR.getDefaultState() + .with(EDGE, edge) + .with(EDGE_FACE, edge.getDirectionIndex(facing)); + } return super.getPlacementState(ctx).with(EDGE, BlockHelper.getPlacementEdge(ctx));