package su.a71.new_soviet.blocks; import net.minecraft.block.*; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.enums.BlockHalf; import net.minecraft.block.enums.StairShape; import net.minecraft.block.piston.PistonBehavior; import net.minecraft.fluid.FluidState; import net.minecraft.fluid.Fluids; import net.minecraft.item.ItemPlacementContext; import net.minecraft.registry.tag.BlockTags; import net.minecraft.sound.BlockSoundGroup; import net.minecraft.state.StateManager; import net.minecraft.state.property.BooleanProperty; import net.minecraft.state.property.IntProperty; import net.minecraft.state.property.Properties; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.util.shape.VoxelShape; import net.minecraft.util.shape.VoxelShapes; import net.minecraft.world.BlockView; import net.minecraft.world.WorldAccess; import su.a71.new_soviet.entities.TVBlockEntity; import su.a71.new_soviet.util.Shapes; import java.util.List; import static net.minecraft.block.StairsBlock.HALF; import static net.minecraft.block.StairsBlock.SHAPE; // FIXME: 05.09.2023 This class can probably be improved further. // By the way, Blockstate not updating when removing stairs is a "creative decision" apparently // - Andrew71 public class HandrailBlock extends HorizontalFacingBlock implements BlockEntityProvider, Waterloggable { protected static final Shapes.HorizontalShape SHAPE1; protected static final Shapes.HorizontalShape SHAPE2; protected static final Shapes.HorizontalShape SHAPE3; public static final BooleanProperty WATERLOGGED; public static final IntProperty ROTATION; public HandrailBlock(Settings settings) { super(settings.sounds(BlockSoundGroup.METAL).pistonBehavior(PistonBehavior.BLOCK).strength(1f, 2f)); setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH)); } @Override protected void appendProperties(StateManager.Builder builder) { builder.add(Properties.HORIZONTAL_FACING, WATERLOGGED, ROTATION); } public FluidState getFluidState(BlockState state) { return state.get(WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(state); } @Override public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext ctx) { Direction dir = state.get(FACING); int rot = state.get(ROTATION); return switch (dir) { case NORTH -> switch (rot) { case 1 -> SHAPE1.north(); case 2 -> SHAPE2.north(); case 3 -> SHAPE3.north(); default -> VoxelShapes.fullCube(); }; case SOUTH -> switch (rot) { case 1 -> SHAPE1.south(); case 2 -> SHAPE2.south(); case 3 -> SHAPE3.south(); default -> VoxelShapes.fullCube(); }; case EAST -> switch (rot) { case 1 -> SHAPE1.east(); case 2 -> SHAPE2.east(); case 3 -> SHAPE3.east(); default -> VoxelShapes.fullCube(); }; case WEST -> switch (rot) { case 1 -> SHAPE1.west(); case 2 -> SHAPE2.west(); case 3 -> SHAPE3.west(); default -> VoxelShapes.fullCube(); }; default -> VoxelShapes.fullCube(); }; } @Override public BlockState getPlacementState(ItemPlacementContext ctx) { int rot = getRotation(ctx.getWorld().getBlockState(ctx.getBlockPos().down()), ctx); return this.getDefaultState() .with(WATERLOGGED, ctx.getWorld().getFluidState(ctx.getBlockPos()).getFluid() == Fluids.WATER) .with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite()) .with(ROTATION, rot); } private int getRotation(BlockState state, ItemPlacementContext ctx) { if (state.isIn(BlockTags.STAIRS) && ((state.get(SHAPE) == StairShape.STRAIGHT) && state.get(HALF) == BlockHalf.BOTTOM)) { if (returnFacing(state.get(FACING)) == ctx.getHorizontalPlayerFacing().getOpposite()) { return 1; } else if (state.get(FACING) == returnFacing(ctx.getHorizontalPlayerFacing().getOpposite())) { return 3; } } return 2; } private Direction returnFacing(Direction facing) { return switch (facing) { case NORTH -> Direction.WEST; case WEST -> Direction.SOUTH; case SOUTH -> Direction.EAST; default -> Direction.NORTH; }; } @Override public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { return new TVBlockEntity(pos, state); } public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) { if (state.get(WATERLOGGED)) { world.scheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world)); } return !state.canPlaceAt(world, pos) ? Blocks.AIR.getDefaultState() : super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos); } static { SHAPE2 = new Shapes.HorizontalShape(List.of(List.of(0.0, 0.0, 15.0, 16.0, 17.0, 16.0), List.of(0.0, 17.0, 14.5, 16.0, 19.0, 16.5))); SHAPE1 = new Shapes.HorizontalShape(List.of(List.of(0.0, 0.0, 15.0, 8.0, 18.0, 16.0), List.of(8.0, -8.0, 15.0, 16.0, 10.0, 16.0))); SHAPE3 = new Shapes.HorizontalShape(List.of(List.of(0.0, -8.0, 15.0, 8.0, 10.0, 16.0), List.of(8.0, 0.0, 15.0, 16.0, 18.0, 16.0))); ROTATION = IntProperty.of("rotate", 1, 3); WATERLOGGED = Properties.WATERLOGGED; } }