| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.tags.FluidTags;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 7 | import net.minecraft.world.entity.LivingEntity;
|
| 8 | import net.minecraft.world.item.ItemStack;
|
| 9 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 10 | import net.minecraft.world.level.BlockGetter;
|
| 11 | import net.minecraft.world.level.LevelAccessor;
|
| 12 | import net.minecraft.world.level.LevelReader;
|
| 13 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 14 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 17 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 18 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 19 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 20 | import net.minecraft.world.level.block.state.properties.SlabType;
|
| 21 | import net.minecraft.world.level.material.Fluid;
|
| 22 | import net.minecraft.world.level.material.FluidState;
|
| 23 | import net.minecraft.world.level.material.Fluids;
|
| 24 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 25 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 26 | import net.minecraft.world.phys.shapes.Shapes;
|
| 27 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | public class SlabBlock extends Block implements SimpleWaterloggedBlock {
|
| 31 | public static final EnumProperty<SlabType> TYPE = BlockStateProperties.SLAB_TYPE;
|
| 32 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 33 | private static final VoxelShape SHAPE_BOTTOM = Block.column(16.0, 0.0, 8.0);
|
| 34 | private static final VoxelShape SHAPE_TOP = Block.column(16.0, 8.0, 16.0);
|
| 35 |
|
| 36 | public SlabBlock(final BlockBehaviour.Properties properties) {
|
| 37 | super(properties);
|
| 38 | this.registerDefaultState(this.defaultBlockState().setValue(TYPE, SlabType.BOTTOM).setValue(WATERLOGGED, false));
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected boolean useShapeForLightOcclusion(final BlockState state) {
|
| 43 | return state.getValue(TYPE) != SlabType.DOUBLE;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 48 | builder.add(TYPE, WATERLOGGED);
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 53 | return switch ((SlabType)state.getValue(TYPE)) {
|
| 54 | case TOP -> SHAPE_TOP;
|
| 55 | case BOTTOM -> SHAPE_BOTTOM;
|
| 56 | case DOUBLE -> Shapes.block();
|
| 57 | };
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 62 | BlockPos pos = context.getClickedPos();
|
| 63 | BlockState replacedBlockState = context.getLevel().getBlockState(pos);
|
| 64 | if (replacedBlockState.is(this)) {
|
| 65 | return replacedBlockState.setValue(TYPE, SlabType.DOUBLE).setValue(WATERLOGGED, false);
|
| 66 | }
|
| 67 |
|
| 68 | FluidState replacedFluidState = context.getLevel().getFluidState(pos);
|
| 69 | BlockState result = this.defaultBlockState().setValue(TYPE, SlabType.BOTTOM).setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
|
| 70 | Direction clickedFace = context.getClickedFace();
|
| 71 | return clickedFace != Direction.DOWN && (clickedFace == Direction.UP || !(context.getClickLocation().y - pos.getY() > 0.5))
|
| 72 | ? result
|
| 73 | : result.setValue(TYPE, SlabType.TOP);
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | protected boolean canBeReplaced(final BlockState state, final BlockPlaceContext context) {
|
| 78 | ItemStack itemStack = context.getItemInHand();
|
| 79 | SlabType type = state.getValue(TYPE);
|
| 80 | if (type == SlabType.DOUBLE || !itemStack.is(this.asItem())) {
|
| 81 | return false;
|
| 82 | } else if (context.replacingClickedOnBlock()) {
|
| 83 | boolean above = context.getClickLocation().y - context.getClickedPos().getY() > 0.5;
|
| 84 | Direction clickedFace = context.getClickedFace();
|
| 85 | return type == SlabType.BOTTOM
|
| 86 | ? clickedFace == Direction.UP || above && clickedFace.getAxis().isHorizontal()
|
| 87 | : clickedFace == Direction.DOWN || !above && clickedFace.getAxis().isHorizontal();
|
| 88 | } else {
|
| 89 | return true;
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected FluidState getFluidState(final BlockState state) {
|
| 95 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public boolean placeLiquid(final LevelAccessor level, final BlockPos pos, final BlockState state, final FluidState fluidState) {
|
| 100 | return state.getValue(TYPE) != SlabType.DOUBLE ? SimpleWaterloggedBlock.super.placeLiquid(level, pos, state, fluidState) : false;
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public boolean canPlaceLiquid(final @Nullable LivingEntity user, final BlockGetter level, final BlockPos pos, final BlockState state, final Fluid type) {
|
| 105 | return state.getValue(TYPE) != SlabType.DOUBLE ? SimpleWaterloggedBlock.super.canPlaceLiquid(user, level, pos, state, type) : false;
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | protected BlockState updateShape(
|
| 110 | final BlockState state,
|
| 111 | final LevelReader level,
|
| 112 | final ScheduledTickAccess ticks,
|
| 113 | final BlockPos pos,
|
| 114 | final Direction directionToNeighbour,
|
| 115 | final BlockPos neighbourPos,
|
| 116 | final BlockState neighbourState,
|
| 117 | final RandomSource random
|
| 118 | ) {
|
| 119 | if (state.getValue(WATERLOGGED)) {
|
| 120 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 121 | }
|
| 122 |
|
| 123 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 128 | return switch (type) {
|
| 129 | case LAND -> false;
|
| 130 | case WATER -> state.getFluidState().is(FluidTags.WATER);
|
| 131 | case AIR -> false;
|
| 132 | default -> false;
|
| 133 | };
|
| 134 | }
|
| 135 | }
|
| 136 |
|