package net.minecraft.world.level.block.piston; import com.mojang.serialization.MapCodec; import java.util.Collections; import java.util.List; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.block.BaseEntityBlock; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Mirror; import net.minecraft.world.level.block.RenderShape; import net.minecraft.world.level.block.Rotation; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityTicker; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition.Builder; import net.minecraft.world.level.block.state.properties.EnumProperty; import net.minecraft.world.level.block.state.properties.PistonType; import net.minecraft.world.level.pathfinder.PathComputationType; import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; import org.jetbrains.annotations.Nullable; public class MovingPistonBlock extends BaseEntityBlock { public static final MapCodec CODEC = simpleCodec(MovingPistonBlock::new); public static final EnumProperty FACING = PistonHeadBlock.FACING; public static final EnumProperty TYPE = PistonHeadBlock.TYPE; @Override public MapCodec codec() { return CODEC; } public MovingPistonBlock(BlockBehaviour.Properties properties) { super(properties); this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(TYPE, PistonType.DEFAULT)); } @Nullable @Override public BlockEntity newBlockEntity(BlockPos pos, BlockState state) { return null; } public static BlockEntity newMovingBlockEntity( BlockPos pos, BlockState blockState, BlockState movedState, Direction direction, boolean extending, boolean isSourcePiston ) { return new PistonMovingBlockEntity(pos, blockState, movedState, direction, extending, isSourcePiston); } @Nullable @Override public BlockEntityTicker getTicker(Level level, BlockState state, BlockEntityType blockEntityType) { return createTickerHelper(blockEntityType, BlockEntityType.PISTON, PistonMovingBlockEntity::tick); } @Override public void destroy(LevelAccessor level, BlockPos pos, BlockState state) { BlockPos blockPos = pos.relative(((Direction)state.getValue(FACING)).getOpposite()); BlockState blockState = level.getBlockState(blockPos); if (blockState.getBlock() instanceof PistonBaseBlock && (Boolean)blockState.getValue(PistonBaseBlock.EXTENDED)) { level.removeBlock(blockPos, false); } } @Override protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) { if (!level.isClientSide && level.getBlockEntity(pos) == null) { level.removeBlock(pos, false); return InteractionResult.CONSUME; } else { return InteractionResult.PASS; } } @Override protected List getDrops(BlockState state, net.minecraft.world.level.storage.loot.LootParams.Builder params) { PistonMovingBlockEntity pistonMovingBlockEntity = this.getBlockEntity(params.getLevel(), BlockPos.containing(params.getParameter(LootContextParams.ORIGIN))); return pistonMovingBlockEntity == null ? Collections.emptyList() : pistonMovingBlockEntity.getMovedState().getDrops(params); } @Override protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { return Shapes.empty(); } @Override protected VoxelShape getCollisionShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { PistonMovingBlockEntity pistonMovingBlockEntity = this.getBlockEntity(level, pos); return pistonMovingBlockEntity != null ? pistonMovingBlockEntity.getCollisionShape(level, pos) : Shapes.empty(); } @Nullable private PistonMovingBlockEntity getBlockEntity(BlockGetter blockReader, BlockPos pos) { BlockEntity blockEntity = blockReader.getBlockEntity(pos); return blockEntity instanceof PistonMovingBlockEntity ? (PistonMovingBlockEntity)blockEntity : null; } @Override protected RenderShape getRenderShape(BlockState state) { return RenderShape.INVISIBLE; } @Override protected ItemStack getCloneItemStack(LevelReader level, BlockPos pos, BlockState state, boolean includeData) { return ItemStack.EMPTY; } @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) { return state.rotate(mirror.getRotation(state.getValue(FACING))); } @Override protected void createBlockStateDefinition(Builder builder) { builder.add(FACING, TYPE); } @Override protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) { return false; } }