minecraft-src/net/minecraft/world/level/block/StairBlock.java
2025-07-04 01:41:11 +03:00

241 lines
10 KiB
Java

package net.minecraft.world.level.block;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import java.util.stream.IntStream;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.Half;
import net.minecraft.world.level.block.state.properties.StairsShape;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
public class StairBlock extends Block implements SimpleWaterloggedBlock {
public static final MapCodec<StairBlock> CODEC = RecordCodecBuilder.mapCodec(
instance -> instance.group(BlockState.CODEC.fieldOf("base_state").forGetter(stairBlock -> stairBlock.baseState), propertiesCodec())
.apply(instance, StairBlock::new)
);
public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
public static final EnumProperty<Half> HALF = BlockStateProperties.HALF;
public static final EnumProperty<StairsShape> SHAPE = BlockStateProperties.STAIRS_SHAPE;
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
protected static final VoxelShape TOP_AABB = SlabBlock.TOP_AABB;
protected static final VoxelShape BOTTOM_AABB = SlabBlock.BOTTOM_AABB;
protected static final VoxelShape OCTET_NNN = Block.box(0.0, 0.0, 0.0, 8.0, 8.0, 8.0);
protected static final VoxelShape OCTET_NNP = Block.box(0.0, 0.0, 8.0, 8.0, 8.0, 16.0);
protected static final VoxelShape OCTET_NPN = Block.box(0.0, 8.0, 0.0, 8.0, 16.0, 8.0);
protected static final VoxelShape OCTET_NPP = Block.box(0.0, 8.0, 8.0, 8.0, 16.0, 16.0);
protected static final VoxelShape OCTET_PNN = Block.box(8.0, 0.0, 0.0, 16.0, 8.0, 8.0);
protected static final VoxelShape OCTET_PNP = Block.box(8.0, 0.0, 8.0, 16.0, 8.0, 16.0);
protected static final VoxelShape OCTET_PPN = Block.box(8.0, 8.0, 0.0, 16.0, 16.0, 8.0);
protected static final VoxelShape OCTET_PPP = Block.box(8.0, 8.0, 8.0, 16.0, 16.0, 16.0);
protected static final VoxelShape[] TOP_SHAPES = makeShapes(TOP_AABB, OCTET_NNN, OCTET_PNN, OCTET_NNP, OCTET_PNP);
protected static final VoxelShape[] BOTTOM_SHAPES = makeShapes(BOTTOM_AABB, OCTET_NPN, OCTET_PPN, OCTET_NPP, OCTET_PPP);
private static final int[] SHAPE_BY_STATE = new int[]{12, 5, 3, 10, 14, 13, 7, 11, 13, 7, 11, 14, 8, 4, 1, 2, 4, 1, 2, 8};
private final Block base;
protected final BlockState baseState;
@Override
public MapCodec<? extends StairBlock> codec() {
return CODEC;
}
private static VoxelShape[] makeShapes(VoxelShape slabShape, VoxelShape nwCorner, VoxelShape neCorner, VoxelShape swCorner, VoxelShape seCorner) {
return (VoxelShape[])IntStream.range(0, 16).mapToObj(i -> makeStairShape(i, slabShape, nwCorner, neCorner, swCorner, seCorner)).toArray(VoxelShape[]::new);
}
/**
* Combines the shapes according to the mode set in the bitfield
*/
private static VoxelShape makeStairShape(
int bitfield, VoxelShape slabShape, VoxelShape nwCorner, VoxelShape neCorner, VoxelShape swCorner, VoxelShape seCorner
) {
VoxelShape voxelShape = slabShape;
if ((bitfield & 1) != 0) {
voxelShape = Shapes.or(slabShape, nwCorner);
}
if ((bitfield & 2) != 0) {
voxelShape = Shapes.or(voxelShape, neCorner);
}
if ((bitfield & 4) != 0) {
voxelShape = Shapes.or(voxelShape, swCorner);
}
if ((bitfield & 8) != 0) {
voxelShape = Shapes.or(voxelShape, seCorner);
}
return voxelShape;
}
protected StairBlock(BlockState baseState, BlockBehaviour.Properties properties) {
super(properties);
this.registerDefaultState(
this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(HALF, Half.BOTTOM).setValue(SHAPE, StairsShape.STRAIGHT).setValue(WATERLOGGED, false)
);
this.base = baseState.getBlock();
this.baseState = baseState;
}
@Override
protected boolean useShapeForLightOcclusion(BlockState state) {
return true;
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return (state.getValue(HALF) == Half.TOP ? TOP_SHAPES : BOTTOM_SHAPES)[SHAPE_BY_STATE[this.getShapeIndex(state)]];
}
private int getShapeIndex(BlockState state) {
return ((StairsShape)state.getValue(SHAPE)).ordinal() * 4 + ((Direction)state.getValue(FACING)).get2DDataValue();
}
@Override
public float getExplosionResistance() {
return this.base.getExplosionResistance();
}
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
Direction direction = context.getClickedFace();
BlockPos blockPos = context.getClickedPos();
FluidState fluidState = context.getLevel().getFluidState(blockPos);
BlockState blockState = this.defaultBlockState()
.setValue(FACING, context.getHorizontalDirection())
.setValue(
HALF, direction != Direction.DOWN && (direction == Direction.UP || !(context.getClickLocation().y - blockPos.getY() > 0.5)) ? Half.BOTTOM : Half.TOP
)
.setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
return blockState.setValue(SHAPE, getStairsShape(blockState, context.getLevel(), blockPos));
}
@Override
protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
if ((Boolean)state.getValue(WATERLOGGED)) {
level.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
}
return direction.getAxis().isHorizontal()
? state.setValue(SHAPE, getStairsShape(state, level, pos))
: super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}
/**
* Returns a stair shape property based on the surrounding stairs from the given blockstate and position
*/
private static StairsShape getStairsShape(BlockState state, BlockGetter level, BlockPos pos) {
Direction direction = state.getValue(FACING);
BlockState blockState = level.getBlockState(pos.relative(direction));
if (isStairs(blockState) && state.getValue(HALF) == blockState.getValue(HALF)) {
Direction direction2 = blockState.getValue(FACING);
if (direction2.getAxis() != ((Direction)state.getValue(FACING)).getAxis() && canTakeShape(state, level, pos, direction2.getOpposite())) {
if (direction2 == direction.getCounterClockWise()) {
return StairsShape.OUTER_LEFT;
}
return StairsShape.OUTER_RIGHT;
}
}
BlockState blockState2 = level.getBlockState(pos.relative(direction.getOpposite()));
if (isStairs(blockState2) && state.getValue(HALF) == blockState2.getValue(HALF)) {
Direction direction3 = blockState2.getValue(FACING);
if (direction3.getAxis() != ((Direction)state.getValue(FACING)).getAxis() && canTakeShape(state, level, pos, direction3)) {
if (direction3 == direction.getCounterClockWise()) {
return StairsShape.INNER_LEFT;
}
return StairsShape.INNER_RIGHT;
}
}
return StairsShape.STRAIGHT;
}
private static boolean canTakeShape(BlockState state, BlockGetter level, BlockPos pos, Direction face) {
BlockState blockState = level.getBlockState(pos.relative(face));
return !isStairs(blockState) || blockState.getValue(FACING) != state.getValue(FACING) || blockState.getValue(HALF) != state.getValue(HALF);
}
public static boolean isStairs(BlockState state) {
return state.getBlock() instanceof StairBlock;
}
@Override
protected BlockState rotate(BlockState state, Rotation rotation) {
return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
}
@Override
protected BlockState mirror(BlockState state, Mirror mirror) {
Direction direction = state.getValue(FACING);
StairsShape stairsShape = state.getValue(SHAPE);
switch (mirror) {
case LEFT_RIGHT:
if (direction.getAxis() == Direction.Axis.Z) {
switch (stairsShape) {
case INNER_LEFT:
return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.INNER_RIGHT);
case INNER_RIGHT:
return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.INNER_LEFT);
case OUTER_LEFT:
return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.OUTER_RIGHT);
case OUTER_RIGHT:
return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.OUTER_LEFT);
default:
return state.rotate(Rotation.CLOCKWISE_180);
}
}
break;
case FRONT_BACK:
if (direction.getAxis() == Direction.Axis.X) {
switch (stairsShape) {
case INNER_LEFT:
return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.INNER_LEFT);
case INNER_RIGHT:
return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.INNER_RIGHT);
case OUTER_LEFT:
return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.OUTER_RIGHT);
case OUTER_RIGHT:
return state.rotate(Rotation.CLOCKWISE_180).setValue(SHAPE, StairsShape.OUTER_LEFT);
case STRAIGHT:
return state.rotate(Rotation.CLOCKWISE_180);
}
}
}
return super.mirror(state, mirror);
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING, HALF, SHAPE, WATERLOGGED);
}
@Override
protected FluidState getFluidState(BlockState state) {
return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
}
@Override
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
return false;
}
}