| 1 | package net.minecraft.world.level.block.piston;
|
| 2 |
|
| 3 | import java.util.Collections;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.world.InteractionResult;
|
| 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.LevelAccessor;
|
| 13 | import net.minecraft.world.level.LevelReader;
|
| 14 | import net.minecraft.world.level.block.BaseEntityBlock;
|
| 15 | import net.minecraft.world.level.block.Block;
|
| 16 | import net.minecraft.world.level.block.Mirror;
|
| 17 | import net.minecraft.world.level.block.RenderShape;
|
| 18 | import net.minecraft.world.level.block.Rotation;
|
| 19 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 20 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 21 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 22 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 23 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 24 | import net.minecraft.world.level.block.state.BlockState;
|
| 25 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 26 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 27 | import net.minecraft.world.level.block.state.properties.PistonType;
|
| 28 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 29 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 30 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 31 | import net.minecraft.world.phys.BlockHitResult;
|
| 32 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 33 | import net.minecraft.world.phys.shapes.Shapes;
|
| 34 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 35 | import org.jspecify.annotations.Nullable;
|
| 36 |
|
| 37 | public class MovingPistonBlock extends BaseEntityBlock {
|
| 38 | public static final EnumProperty<Direction> FACING = PistonHeadBlock.FACING;
|
| 39 | public static final EnumProperty<PistonType> TYPE = PistonHeadBlock.TYPE;
|
| 40 |
|
| 41 | public MovingPistonBlock(final BlockBehaviour.Properties properties) {
|
| 42 | super(properties);
|
| 43 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(TYPE, PistonType.DEFAULT));
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public @Nullable BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 48 | return null;
|
| 49 | }
|
| 50 |
|
| 51 | public static BlockEntity newMovingBlockEntity(
|
| 52 | final BlockPos position,
|
| 53 | final BlockState blockState,
|
| 54 | final BlockState movedState,
|
| 55 | final Direction direction,
|
| 56 | final boolean extending,
|
| 57 | final boolean isSourcePiston
|
| 58 | ) {
|
| 59 | return new PistonMovingBlockEntity(position, blockState, movedState, direction, extending, isSourcePiston);
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 64 | return createTickerHelper(type, BlockEntityTypes.PISTON, PistonMovingBlockEntity::tick);
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public void destroy(final LevelAccessor level, final BlockPos pos, final BlockState state) {
|
| 69 | BlockPos relative = pos.relative(state.getValue(FACING).getOpposite());
|
| 70 | BlockState blockState = level.getBlockState(relative);
|
| 71 | if (blockState.getBlock() instanceof PistonBaseBlock && blockState.getValue(PistonBaseBlock.EXTENDED)) {
|
| 72 | level.removeBlock(relative, false);
|
| 73 | }
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 78 | if (!level.isClientSide() && level.getBlockEntity(pos) == null) {
|
| 79 | level.removeBlock(pos, false);
|
| 80 | return InteractionResult.CONSUME;
|
| 81 | } else {
|
| 82 | return InteractionResult.PASS;
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | protected List<ItemStack> getDrops(final BlockState state, final LootParams.Builder params) {
|
| 88 | PistonMovingBlockEntity entity = this.getBlockEntity(params.getLevel(), BlockPos.containing(params.getParameter(LootContextParams.ORIGIN)));
|
| 89 | return entity == null ? Collections.emptyList() : entity.getMovedState().getDrops(params);
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 94 | return Shapes.empty();
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 99 | PistonMovingBlockEntity blockEntity = this.getBlockEntity(level, pos);
|
| 100 | return blockEntity != null ? blockEntity.getCollisionShape(level, pos) : Shapes.empty();
|
| 101 | }
|
| 102 |
|
| 103 | private @Nullable PistonMovingBlockEntity getBlockEntity(final BlockGetter level, final BlockPos pos) {
|
| 104 | return level.getBlockEntity(pos) instanceof PistonMovingBlockEntity pistonMovingBlockEntity ? pistonMovingBlockEntity : null;
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected RenderShape getRenderShape(final BlockState state) {
|
| 109 | return RenderShape.INVISIBLE;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
|
| 114 | return ItemStack.EMPTY;
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 119 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 124 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 129 | builder.add(FACING, TYPE);
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 134 | return false;
|
| 135 | }
|
| 136 | }
|
| 137 |
|