Untitled
unknown
java
a year ago
3.9 kB
12
Indexable
public class ReassemblyUnitBlock extends Block {
public static final EnumProperty<DoubleBlockHalf> HALF = BlockStateProperties.DOUBLE_BLOCK_HALF;
private static final VoxelShape LOWER_SHAPE = Shapes.or(
Block.box(2, 0, 2, 14, 14, 14),
Block.box(0, 11, 0, 16, 16, 16)
);
private static final VoxelShape UPPER_SHAPE = Block.box(0, 0, 0, 16, 16, 16);
public ReassemblyUnitBlock(Properties properties) {
super(properties);
registerDefaultState(this.stateDefinition.any().setValue(HALF, DoubleBlockHalf.LOWER));
}
@Override
public VoxelShape getShape(BlockState state, BlockGetter arg2, BlockPos arg3, CollisionContext arg4) {
return state.getValue(HALF) == DoubleBlockHalf.LOWER ? LOWER_SHAPE : UPPER_SHAPE;
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(HALF);
}
@Override
public BlockState updateShape(BlockState state, Direction direction, BlockState neighborState,
LevelAccessor level, BlockPos currentPos, BlockPos neighborPos) {
DoubleBlockHalf half = state.getValue(HALF);
if (direction.getAxis() == Direction.Axis.Y &&
((half == DoubleBlockHalf.LOWER && direction == Direction.UP) ||
(half == DoubleBlockHalf.UPPER && direction == Direction.DOWN))) {
if (neighborState.getBlock() != this || neighborState.getValue(HALF) == half) {
return Blocks.AIR.defaultBlockState();
}
}
return super.updateShape(state, direction, neighborState, level, currentPos, neighborPos);
}
@Override
public BlockState playerWillDestroy(Level level, BlockPos pos, BlockState state, Player player) {
if (!level.isClientSide) {
DoubleBlockHalf half = state.getValue(HALF);
BlockPos otherHalfPos = half == DoubleBlockHalf.LOWER ? pos.above() : pos.below();
BlockState otherHalfState = level.getBlockState(otherHalfPos);
if (otherHalfState.getBlock() == this && otherHalfState.getValue(HALF) != half) {
// Check if player should get drops - only if using correct tool and not in creative
boolean hasCorrectTool = !player.isCreative() &&
level.getBlockState(pos).canHarvestBlock(level, pos, player);
// Remove the other half but with flag 35 which prevents drops when hasCorrectTool is false
// 35 = Block.UPDATE_ALL | Block.UPDATE_SUPPRESS_DROPS
int flags = hasCorrectTool ? 3 : 35;
level.setBlock(otherHalfPos, Blocks.AIR.defaultBlockState(), flags);
level.levelEvent(player, 2001, otherHalfPos, Block.getId(otherHalfState));
}
}
return super.playerWillDestroy(level, pos, state, player);
}
@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
level.setBlock(pos.above(), state.setValue(HALF, DoubleBlockHalf.UPPER), 3);
}
@Nullable
public BlockState getStateForPlacement(BlockPlaceContext context) {
BlockPos pos = context.getClickedPos();
Level level = context.getLevel();
if (pos.getY() < level.getMaxBuildHeight() - 1 && level.getBlockState(pos.above()).canBeReplaced(context)) {
return this.defaultBlockState().setValue(HALF, DoubleBlockHalf.LOWER);
} else {
return null;
}
}
@Override
public boolean canDropFromExplosion(BlockState state, BlockGetter level, BlockPos pos, Explosion explosion) {
return state.getValue(HALF) == DoubleBlockHalf.LOWER;
}
}Editor is loading...
Leave a Comment