| 1 | package net.minecraft.world.level.block.piston;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.entity.player.Player;
|
| 9 | import net.minecraft.world.item.ItemStack;
|
| 10 | import net.minecraft.world.level.BlockGetter;
|
| 11 | import net.minecraft.world.level.Level;
|
| 12 | import net.minecraft.world.level.LevelReader;
|
| 13 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 14 | import net.minecraft.world.level.block.Block;
|
| 15 | import net.minecraft.world.level.block.Blocks;
|
| 16 | import net.minecraft.world.level.block.DirectionalBlock;
|
| 17 | import net.minecraft.world.level.block.Mirror;
|
| 18 | import net.minecraft.world.level.block.Rotation;
|
| 19 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 22 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 23 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 24 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 25 | import net.minecraft.world.level.block.state.properties.PistonType;
|
| 26 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 27 | import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
| 28 | import net.minecraft.world.level.redstone.Orientation;
|
| 29 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 30 | import net.minecraft.world.phys.shapes.Shapes;
|
| 31 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 32 | import org.jspecify.annotations.Nullable;
|
| 33 |
|
| 34 | public class PistonHeadBlock extends DirectionalBlock {
|
| 35 | public static final EnumProperty<PistonType> TYPE = BlockStateProperties.PISTON_TYPE;
|
| 36 | public static final BooleanProperty SHORT = BlockStateProperties.SHORT;
|
| 37 | public static final int PLATFORM_THICKNESS = 4;
|
| 38 | private static final VoxelShape SHAPE_PLATFORM = Block.boxZ(16.0, 0.0, 4.0);
|
| 39 | private static final Map<Direction, VoxelShape> SHAPES_SHORT = Shapes.rotateAll(Shapes.or(SHAPE_PLATFORM, Block.boxZ(4.0, 4.0, 16.0)));
|
| 40 | private static final Map<Direction, VoxelShape> SHAPES = Shapes.rotateAll(Shapes.or(SHAPE_PLATFORM, Block.boxZ(4.0, 4.0, 20.0)));
|
| 41 |
|
| 42 | public PistonHeadBlock(final BlockBehaviour.Properties properties) {
|
| 43 | super(properties);
|
| 44 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(TYPE, PistonType.DEFAULT).setValue(SHORT, false));
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected boolean useShapeForLightOcclusion(final BlockState state) {
|
| 49 | return true;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 54 | return (state.getValue(SHORT) ? SHAPES_SHORT : SHAPES).get(state.getValue(FACING));
|
| 55 | }
|
| 56 |
|
| 57 | private boolean isFittingBase(final BlockState armState, final BlockState potentialBase) {
|
| 58 | Block baseBlock = armState.getValue(TYPE) == PistonType.DEFAULT ? Blocks.PISTON : Blocks.STICKY_PISTON;
|
| 59 | return potentialBase.is(baseBlock) && potentialBase.getValue(PistonBaseBlock.EXTENDED) && potentialBase.getValue(FACING) == armState.getValue(FACING);
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
|
| 64 | if (!level.isClientSide() && player.preventsBlockDrops()) {
|
| 65 | BlockPos basePos = pos.relative(state.getValue(FACING).getOpposite());
|
| 66 | if (this.isFittingBase(state, level.getBlockState(basePos))) {
|
| 67 | level.destroyBlock(basePos, false);
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | return super.playerWillDestroy(level, pos, state, player);
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
|
| 76 | BlockPos basePos = pos.relative(state.getValue(FACING).getOpposite());
|
| 77 | if (this.isFittingBase(state, level.getBlockState(basePos))) {
|
| 78 | level.destroyBlock(basePos, true);
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | protected BlockState updateShape(
|
| 84 | final BlockState state,
|
| 85 | final LevelReader level,
|
| 86 | final ScheduledTickAccess ticks,
|
| 87 | final BlockPos pos,
|
| 88 | final Direction directionToNeighbour,
|
| 89 | final BlockPos neighbourPos,
|
| 90 | final BlockState neighbourState,
|
| 91 | final RandomSource random
|
| 92 | ) {
|
| 93 | return directionToNeighbour.getOpposite() == state.getValue(FACING) && !state.canSurvive(level, pos)
|
| 94 | ? Blocks.AIR.defaultBlockState()
|
| 95 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 100 | BlockState base = level.getBlockState(pos.relative(state.getValue(FACING).getOpposite()));
|
| 101 | return this.isFittingBase(state, base) || base.is(Blocks.MOVING_PISTON) && base.getValue(FACING) == state.getValue(FACING);
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | protected void neighborChanged(
|
| 106 | final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 107 | ) {
|
| 108 | if (state.canSurvive(level, pos)) {
|
| 109 | level.neighborChanged(
|
| 110 | pos.relative(state.getValue(FACING).getOpposite()), block, ExperimentalRedstoneUtils.withFront(orientation, state.getValue(FACING).getOpposite())
|
| 111 | );
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 117 | return new ItemStack(state.getValue(TYPE) == PistonType.STICKY ? Blocks.STICKY_PISTON : Blocks.PISTON);
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 122 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 127 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 132 | builder.add(FACING, TYPE, SHORT);
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 137 | return false;
|
| 138 | }
|
| 139 | }
|
| 140 |
|