From e8c06a7fb3eeb9cdd863203a6f15ad590aaceaaa Mon Sep 17 00:00:00 2001 From: Adrien1106 Date: Mon, 1 Jul 2024 22:31:52 +0200 Subject: [PATCH] feat: better additivity support --- .../block/ReFramedHalfStairBlock.java | 8 ++ .../reframed/block/ReFramedSlabBlock.java | 71 +++++++++++++- .../block/ReFramedSmallCubeBlock.java | 92 ++++++++++--------- .../reframed/block/ReFramedStepBlock.java | 8 +- .../reframed/util/blocks/Corner.java | 4 + 5 files changed, 135 insertions(+), 48 deletions(-) diff --git a/src/main/java/fr/adrien1106/reframed/block/ReFramedHalfStairBlock.java b/src/main/java/fr/adrien1106/reframed/block/ReFramedHalfStairBlock.java index 2873cb3..1075731 100644 --- a/src/main/java/fr/adrien1106/reframed/block/ReFramedHalfStairBlock.java +++ b/src/main/java/fr/adrien1106/reframed/block/ReFramedHalfStairBlock.java @@ -73,6 +73,13 @@ public class ReFramedHalfStairBlock extends WaterloggableReFramedBlock { ReFramed.SMALL_CUBE.getDefaultState().with(CORNER, corner.getOpposite(dir)) ); + if (block == ReFramed.SLAB) + return ReFramed.SLAB.matchesShape( + context.getHitPos(), + context.getBlockPos(), + ReFramed.SLAB.getDefaultState().with(FACING, dir.getOpposite()) + ); + return false; } @@ -135,6 +142,7 @@ public class ReFramedHalfStairBlock extends WaterloggableReFramedBlock { || new_state.isOf(ReFramed.HALF_STAIRS_CUBE_STAIR) || new_state.isOf(ReFramed.HALF_STAIRS_STEP_STAIR) ) return Map.of(1, 1); + if (new_state.isOf(ReFramed.SLABS_INNER_STAIR)) return Map.of(1, 2); if (new_state.isOf(ReFramed.HALF_STAIRS_STAIR)) return Map.of( 1, diff --git a/src/main/java/fr/adrien1106/reframed/block/ReFramedSlabBlock.java b/src/main/java/fr/adrien1106/reframed/block/ReFramedSlabBlock.java index 67bcf20..579b42b 100644 --- a/src/main/java/fr/adrien1106/reframed/block/ReFramedSlabBlock.java +++ b/src/main/java/fr/adrien1106/reframed/block/ReFramedSlabBlock.java @@ -1,6 +1,8 @@ package fr.adrien1106.reframed.block; import fr.adrien1106.reframed.ReFramed; +import fr.adrien1106.reframed.util.blocks.BlockHelper; +import fr.adrien1106.reframed.util.blocks.Corner; import fr.adrien1106.reframed.util.blocks.Edge; import net.minecraft.block.Block; import net.minecraft.block.BlockState; @@ -19,8 +21,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Map; -import static fr.adrien1106.reframed.util.blocks.BlockProperties.EDGE; -import static fr.adrien1106.reframed.util.blocks.BlockProperties.EDGE_FACE; +import static fr.adrien1106.reframed.util.blocks.BlockProperties.*; import static net.minecraft.state.property.Properties.*; public class ReFramedSlabBlock extends WaterloggableReFramedBlock { @@ -89,6 +90,72 @@ public class ReFramedSlabBlock extends WaterloggableReFramedBlock { .with(WATERLOGGED, current_state.get(WATERLOGGED)); } + if (current_state.isOf(ReFramed.HALF_STAIR)) { + Corner corner = current_state.get(CORNER); + Direction face = corner.getDirection(current_state.get(CORNER_FACE)); + corner = corner.change(face); + return ReFramed.SLABS_INNER_STAIR.getDefaultState() + .with(CORNER, corner) + .with(CORNER_FACE, corner.getDirectionIndex(face.getOpposite())) + .with(WATERLOGGED, current_state.get(WATERLOGGED)); + } + + if (current_state.isOf(ReFramed.STEP)) { + Edge edge = current_state.get(EDGE), + placed = BlockHelper.getPlacementEdge(ctx), + new_edge; + Direction face = edge.getFirstDirection(), + other = edge.getSecondDirection().getOpposite(); + + if (!ReFramed.STEP.matchesShape( + ctx.getHitPos(), + ctx.getBlockPos(), + current_state.with(EDGE, new_edge = edge.getOpposite(face)) + ) + && ctx.getSide() != other.getOpposite() + && (placed.getAxis() == edge.getAxis() + || ctx.getSide() == other + || !placed.hasDirection(other)) + ) { + new_edge = edge.getOpposite(edge.getSecondDirection()); + other = edge.getFirstDirection().getOpposite(); + } + + return ReFramed.SLABS_STAIR.getDefaultState() + .with(EDGE, new_edge) + .with(EDGE_FACE, new_edge.getDirectionIndex(other)) + .with(WATERLOGGED, current_state.get(WATERLOGGED)); + } + + if (current_state.isOf(ReFramed.SMALL_CUBE)) { + Corner corner = current_state.get(CORNER); + Edge placed = BlockHelper.getPlacementEdge(ctx); + Direction face; + Corner new_corner; + + if (!corner.hasDirection(face = ctx.getSide())) { + int i = 0; + do { + face = corner.getDirection(i); + new_corner = corner.change(face); + } while (!ReFramed.SMALL_CUBE.matchesShape( + ctx.getHitPos(), + ctx.getBlockPos(), + current_state.with(CORNER, new_corner) + ) && ++i < 3); + + if (i == 3) { + face = placed.getOtherDirection(corner.getMatchingDirection(placed)).getOpposite(); + new_corner = corner.change(face); + } + } else new_corner = corner.change(face); + + return ReFramed.SLABS_OUTER_STAIR.getDefaultState() + .with(CORNER, new_corner) + .with(CORNER_FACE, new_corner.getDirectionIndex(face.getOpposite())) + .with(WATERLOGGED, current_state.get(WATERLOGGED)); + } + return super.getPlacementState(ctx).with(FACING, ctx.getSide().getOpposite()); } diff --git a/src/main/java/fr/adrien1106/reframed/block/ReFramedSmallCubeBlock.java b/src/main/java/fr/adrien1106/reframed/block/ReFramedSmallCubeBlock.java index f711770..0b2571c 100644 --- a/src/main/java/fr/adrien1106/reframed/block/ReFramedSmallCubeBlock.java +++ b/src/main/java/fr/adrien1106/reframed/block/ReFramedSmallCubeBlock.java @@ -4,6 +4,8 @@ import fr.adrien1106.reframed.ReFramed; import fr.adrien1106.reframed.util.VoxelHelper; import fr.adrien1106.reframed.util.blocks.BlockHelper; import fr.adrien1106.reframed.util.blocks.Corner; +import fr.adrien1106.reframed.util.blocks.Edge; +import fr.adrien1106.reframed.util.blocks.StairShape; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.ShapeContext; @@ -45,51 +47,51 @@ public class ReFramedSmallCubeBlock extends WaterloggableReFramedBlock { @Override @SuppressWarnings("deprecation") public boolean canReplace(BlockState state, ItemPlacementContext context) { - if (context.getPlayer() == null) return false; + if (context.getPlayer() == null + || context.getPlayer().isSneaking() + || !(context.getStack().getItem() instanceof BlockItem block_item) + ) return false; + + Block block = block_item.getBlock(); Corner corner = state.get(CORNER); - return !( - context.getPlayer().isSneaking() - || !(context.getStack().getItem() instanceof BlockItem block_item) - || ( - !( - block_item.getBlock() == ReFramed.HALF_STAIR - && !(corner.hasDirection(context.getSide()) - || (corner.hasDirection(context.getSide().getOpposite()) - && BlockHelper.cursorMatchesFace( - getOutlineShape(state, context.getWorld(), context.getBlockPos(), null), - BlockHelper.getRelativePos(context.getHitPos(), context.getBlockPos()) - ) - ) - ) + if (block == this) + return matchesShape( + context.getHitPos(), + context.getBlockPos(), + getDefaultState().with(CORNER, corner.change(corner.getFirstDirection())) + ) || matchesShape( + context.getHitPos(), + context.getBlockPos(), + getDefaultState().with(CORNER, corner.change(corner.getSecondDirection())) + ) || matchesShape( + context.getHitPos(), + context.getBlockPos(), + getDefaultState().with(CORNER, corner.change(corner.getThirdDirection())) + ); + + if (block == ReFramed.HALF_STAIR || block == ReFramed.SLAB) { + corner = corner.getOpposite(); + Direction face = corner.getFirstDirection(); + Edge edge = corner.getEdge(face); + if (ReFramed.STAIR.matchesShape( + context.getHitPos(), + context.getBlockPos(), + ReFramed.STAIR.getDefaultState() + .with(EDGE, edge) + .with( + STAIR_SHAPE, + face.getDirection() == Direction.AxisDirection.POSITIVE + ? StairShape.INNER_LEFT + : StairShape.INNER_RIGHT ) - && !( - block_item.getBlock() == this - && ( - ReFramed.SMALL_CUBES_STEP - .matchesShape( - context.getHitPos(), - context.getBlockPos(), - ReFramed.SMALL_CUBES_STEP.getDefaultState().with(EDGE, corner.getEdge(corner.getFirstDirection())), - corner.getFirstDirection().getDirection() == Direction.AxisDirection.POSITIVE ? 1 : 2 - ) - || ReFramed.SMALL_CUBES_STEP - .matchesShape( - context.getHitPos(), - context.getBlockPos(), - ReFramed.SMALL_CUBES_STEP.getDefaultState().with(EDGE, corner.getEdge(corner.getSecondDirection())), - corner.getSecondDirection().getDirection() == Direction.AxisDirection.POSITIVE ? 1 : 2 - ) - || ReFramed.SMALL_CUBES_STEP - .matchesShape( - context.getHitPos(), - context.getBlockPos(), - ReFramed.SMALL_CUBES_STEP.getDefaultState().with(EDGE, corner.getEdge(corner.getThirdDirection())), - corner.getThirdDirection().getDirection() == Direction.AxisDirection.POSITIVE ? 1 : 2 - ) - ) - ) - ) - ); + )) return block == ReFramed.SLAB || !matchesShape( + context.getHitPos(), + context.getBlockPos(), + getDefaultState().with(CORNER, corner) + ); + } + + return false; } @Override @@ -163,7 +165,9 @@ public class ReFramedSmallCubeBlock extends WaterloggableReFramedBlock { @Override public Map getThemeMap(BlockState state, BlockState new_state) { - if (new_state.isOf(ReFramed.HALF_STAIRS_SLAB)) return Map.of(1, 2); + if (new_state.isOf(ReFramed.HALF_STAIRS_SLAB) + || new_state.isOf(ReFramed.SLABS_OUTER_STAIR) + ) return Map.of(1, 2); if (new_state.isOf(ReFramed.SMALL_CUBES_STEP)) return Map.of( 1, diff --git a/src/main/java/fr/adrien1106/reframed/block/ReFramedStepBlock.java b/src/main/java/fr/adrien1106/reframed/block/ReFramedStepBlock.java index 4d700cf..f6451e7 100644 --- a/src/main/java/fr/adrien1106/reframed/block/ReFramedStepBlock.java +++ b/src/main/java/fr/adrien1106/reframed/block/ReFramedStepBlock.java @@ -63,7 +63,10 @@ public class ReFramedStepBlock extends WaterloggableReFramedBlock { ); // allow replacing with stair - if (block != this && block != ReFramed.STAIR) return false; + if (block != this + && block != ReFramed.STAIR + && block != ReFramed.SLAB + ) return false; return ReFramed.STAIR .matchesShape( @@ -186,7 +189,8 @@ public class ReFramedStepBlock extends WaterloggableReFramedBlock { if (new_state.isOf(ReFramed.STEPS_CROSS) || new_state.isOf(ReFramed.STEPS_HALF_LAYER) ) return Map.of(1, 1); - if (new_state.isOf(ReFramed.STAIRS_CUBE)) return Map.of(1, 2); + if (new_state.isOf(ReFramed.STAIRS_CUBE) + || new_state.isOf(ReFramed.SLABS_STAIR)) return Map.of(1, 2); if (new_state.isOf(ReFramed.STEPS_SLAB)) return Map.of( 1, diff --git a/src/main/java/fr/adrien1106/reframed/util/blocks/Corner.java b/src/main/java/fr/adrien1106/reframed/util/blocks/Corner.java index 4e44632..799f627 100644 --- a/src/main/java/fr/adrien1106/reframed/util/blocks/Corner.java +++ b/src/main/java/fr/adrien1106/reframed/util/blocks/Corner.java @@ -154,4 +154,8 @@ public enum Corner implements StringIdentifiable { third_direction == face ? opposite : third_direction ); } + + public Direction getMatchingDirection(Edge edge) { + return hasDirection(edge.getSecondDirection()) ? edge.getSecondDirection() : edge.getFirstDirection(); + } }