| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import com.mojang.math.OctahedralGroup;
|
| 4 | import java.util.Map;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 9 | import net.minecraft.world.level.BlockGetter;
|
| 10 | import net.minecraft.world.level.LevelReader;
|
| 11 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 12 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 13 | import net.minecraft.world.level.block.state.BlockState;
|
| 14 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 15 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 16 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 17 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 18 | import net.minecraft.world.level.block.state.properties.Half;
|
| 19 | import net.minecraft.world.level.block.state.properties.StairsShape;
|
| 20 | import net.minecraft.world.level.material.FluidState;
|
| 21 | import net.minecraft.world.level.material.Fluids;
|
| 22 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 23 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 24 | import net.minecraft.world.phys.shapes.Shapes;
|
| 25 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 26 |
|
| 27 | /**
|
| 28 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 29 | */
|
| 30 | public class StairBlock extends Block implements SimpleWaterloggedBlock {
|
| 31 | public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
|
| 32 | public static final EnumProperty<Half> HALF = BlockStateProperties.HALF;
|
| 33 | public static final EnumProperty<StairsShape> SHAPE = BlockStateProperties.STAIRS_SHAPE;
|
| 34 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 35 | private static final VoxelShape SHAPE_OUTER = Shapes.or(Block.column(16.0, 0.0, 8.0), Block.box(0.0, 8.0, 0.0, 8.0, 16.0, 8.0));
|
| 36 | private static final VoxelShape SHAPE_STRAIGHT = Shapes.or(SHAPE_OUTER, Shapes.rotate(SHAPE_OUTER, OctahedralGroup.BLOCK_ROT_Y_90));
|
| 37 | private static final VoxelShape SHAPE_INNER = Shapes.or(SHAPE_STRAIGHT, Shapes.rotate(SHAPE_STRAIGHT, OctahedralGroup.BLOCK_ROT_Y_90));
|
| 38 | private static final Map<Direction, VoxelShape> SHAPE_BOTTOM_OUTER = Shapes.rotateHorizontal(SHAPE_OUTER);
|
| 39 | private static final Map<Direction, VoxelShape> SHAPE_BOTTOM_STRAIGHT = Shapes.rotateHorizontal(SHAPE_STRAIGHT);
|
| 40 | private static final Map<Direction, VoxelShape> SHAPE_BOTTOM_INNER = Shapes.rotateHorizontal(SHAPE_INNER);
|
| 41 | private static final Map<Direction, VoxelShape> SHAPE_TOP_OUTER = Shapes.rotateHorizontal(SHAPE_OUTER, OctahedralGroup.INVERT_Y);
|
| 42 | private static final Map<Direction, VoxelShape> SHAPE_TOP_STRAIGHT = Shapes.rotateHorizontal(SHAPE_STRAIGHT, OctahedralGroup.INVERT_Y);
|
| 43 | private static final Map<Direction, VoxelShape> SHAPE_TOP_INNER = Shapes.rotateHorizontal(SHAPE_INNER, OctahedralGroup.INVERT_Y);
|
| 44 | private final Block base;
|
| 45 | protected final BlockState baseState;
|
| 46 |
|
| 47 | /**
|
| 48 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 49 | */
|
| 50 | public StairBlock(final BlockState baseState, final BlockBehaviour.Properties properties) {
|
| 51 | super(properties);
|
| 52 | this.registerDefaultState(
|
| 53 | this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(HALF, Half.BOTTOM).setValue(SHAPE, StairsShape.STRAIGHT).setValue(WATERLOGGED, false)
|
| 54 | );
|
| 55 | this.base = baseState.getBlock();
|
| 56 | this.baseState = baseState;
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected boolean useShapeForLightOcclusion(final BlockState state) {
|
| 61 | return true;
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 66 | boolean isBottom = state.getValue(HALF) == Half.BOTTOM;
|
| 67 | Direction facing = state.getValue(FACING);
|
| 68 |
|
| 69 | Map var10000 = switch ((StairsShape)state.getValue(SHAPE)) {
|
| 70 | case STRAIGHT -> isBottom ? SHAPE_BOTTOM_STRAIGHT : SHAPE_TOP_STRAIGHT;
|
| 71 | case OUTER_LEFT, OUTER_RIGHT -> isBottom ? SHAPE_BOTTOM_OUTER : SHAPE_TOP_OUTER;
|
| 72 | case INNER_RIGHT, INNER_LEFT -> isBottom ? SHAPE_BOTTOM_INNER : SHAPE_TOP_INNER;
|
| 73 | };
|
| 74 |
|
| 75 | return (VoxelShape)var10000.get(switch ((StairsShape)state.getValue(SHAPE)) {
|
| 76 | case STRAIGHT, OUTER_LEFT, INNER_RIGHT -> facing;
|
| 77 | case INNER_LEFT -> facing.getCounterClockWise();
|
| 78 | case OUTER_RIGHT -> facing.getClockWise();
|
| 79 | });
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public float getExplosionResistance() {
|
| 84 | return this.base.getExplosionResistance();
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 89 | Direction clickedFace = context.getClickedFace();
|
| 90 | BlockPos pos = context.getClickedPos();
|
| 91 | FluidState replacedFluidState = context.getLevel().getFluidState(pos);
|
| 92 | BlockState state = this.defaultBlockState()
|
| 93 | .setValue(FACING, context.getHorizontalDirection())
|
| 94 | .setValue(
|
| 95 | HALF, clickedFace != Direction.DOWN && (clickedFace == Direction.UP || !(context.getClickLocation().y - pos.getY() > 0.5)) ? Half.BOTTOM : Half.TOP
|
| 96 | )
|
| 97 | .setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
|
| 98 | return state.setValue(SHAPE, getStairsShape(state, context.getLevel(), pos));
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | protected BlockState updateShape(
|
| 103 | final BlockState state,
|
| 104 | final LevelReader level,
|
| 105 | final ScheduledTickAccess ticks,
|
| 106 | final BlockPos pos,
|
| 107 | final Direction directionToNeighbour,
|
| 108 | final BlockPos neighbourPos,
|
| 109 | final BlockState neighbourState,
|
| 110 | final RandomSource random
|
| 111 | ) {
|
| 112 | if (state.getValue(WATERLOGGED)) {
|
| 113 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 114 | }
|
| 115 |
|
| 116 | return directionToNeighbour.getAxis().isHorizontal()
|
| 117 | ? state.setValue(SHAPE, getStairsShape(state, level, pos))
|
| 118 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 119 | }
|
| 120 |
|
| 121 | private static StairsShape getStairsShape(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 122 | Direction facing = state.getValue(FACING);
|
| 123 | BlockState behindState = level.getBlockState(pos.relative(facing));
|
| 124 | if (isStairs(behindState) && state.getValue(HALF) == behindState.getValue(HALF)) {
|
| 125 | Direction behindFacing = behindState.getValue(FACING);
|
| 126 | if (behindFacing.getAxis() != state.getValue(FACING).getAxis() && canTakeShape(state, level, pos, behindFacing.getOpposite())) {
|
| 127 | if (behindFacing == facing.getCounterClockWise()) {
|
| 128 | return StairsShape.OUTER_LEFT;
|
| 129 | }
|
| 130 |
|
| 131 | return StairsShape.OUTER_RIGHT;
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | BlockState frontState = level.getBlockState(pos.relative(facing.getOpposite()));
|
| 136 | if (isStairs(frontState) && state.getValue(HALF) == frontState.getValue(HALF)) {
|
| 137 | Direction frontFacing = frontState.getValue(FACING);
|
| 138 | if (frontFacing.getAxis() != state.getValue(FACING).getAxis() && canTakeShape(state, level, pos, frontFacing)) {
|
| 139 | if (frontFacing == facing.getCounterClockWise()) {
|
| 140 | return StairsShape.INNER_LEFT;
|
| 141 | }
|
| 142 |
|
| 143 | return StairsShape.INNER_RIGHT;
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | return StairsShape.STRAIGHT;
|
| 148 | }
|
| 149 |
|
| 150 | private static boolean canTakeShape(final BlockState state, final BlockGetter level, final BlockPos pos, final Direction neighbour) {
|
| 151 | BlockState neighborState = level.getBlockState(pos.relative(neighbour));
|
| 152 | return !isStairs(neighborState) || neighborState.getValue(FACING) != state.getValue(FACING) || neighborState.getValue(HALF) != state.getValue(HALF);
|
| 153 | }
|
| 154 |
|
| 155 | public static boolean isStairs(final BlockState state) {
|
| 156 | return state.getBlock() instanceof StairBlock;
|
| 157 | }
|
| 158 |
|
| 159 | @Override
|
| 160 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 161 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 166 | Direction direction = state.getValue(FACING);
|
| 167 | StairsShape shape = state.getValue(SHAPE);
|
| 168 | switch (mirror) {
|
| 169 | case LEFT_RIGHT:
|
| 170 | if (direction.getAxis() == Direction.Axis.Z) {
|
| 171 | switch (shape) {
|
| 172 | case OUTER_LEFT:
|
| 173 | return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.OUTER_RIGHT);
|
| 174 | case INNER_RIGHT:
|
| 175 | return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.INNER_LEFT);
|
| 176 | case INNER_LEFT:
|
| 177 | return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.INNER_RIGHT);
|
| 178 | case OUTER_RIGHT:
|
| 179 | return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.OUTER_LEFT);
|
| 180 | default:
|
| 181 | return state.rotate(Rotation.CLOCKWISE_180);
|
| 182 | }
|
| 183 | }
|
| 184 | break;
|
| 185 | case FRONT_BACK:
|
| 186 | if (direction.getAxis() == Direction.Axis.X) {
|
| 187 | switch (shape) {
|
| 188 | case STRAIGHT:
|
| 189 | return state.rotate(Rotation.CLOCKWISE_180);
|
| 190 | case OUTER_LEFT:
|
| 191 | return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.OUTER_RIGHT);
|
| 192 | case INNER_RIGHT:
|
| 193 | return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.INNER_RIGHT);
|
| 194 | case INNER_LEFT:
|
| 195 | return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.INNER_LEFT);
|
| 196 | case OUTER_RIGHT:
|
| 197 | return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.OUTER_LEFT);
|
| 198 | }
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | return super.mirror(state, mirror);
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 207 | builder.add(FACING, HALF, SHAPE, WATERLOGGED);
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | protected FluidState getFluidState(final BlockState state) {
|
| 212 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 213 | }
|
| 214 |
|
| 215 | @Override
|
| 216 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 217 | return false;
|
| 218 | }
|
| 219 | }
|
| 220 |
|