| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.server.level.ServerPlayer;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.entity.LivingEntity;
|
| 10 | import net.minecraft.world.entity.player.Player;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.LevelAccessor;
|
| 15 | import net.minecraft.world.level.LevelReader;
|
| 16 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 17 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 18 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 21 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 22 | import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
|
| 23 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 24 | import net.minecraft.world.level.material.Fluids;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class DoublePlantBlock extends VegetationBlock {
|
| 28 | public static final EnumProperty<DoubleBlockHalf> HALF = BlockStateProperties.DOUBLE_BLOCK_HALF;
|
| 29 |
|
| 30 | public DoublePlantBlock(final BlockBehaviour.Properties properties) {
|
| 31 | super(properties);
|
| 32 | this.registerDefaultState(this.stateDefinition.any().setValue(HALF, DoubleBlockHalf.LOWER));
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | protected BlockState updateShape(
|
| 37 | final BlockState state,
|
| 38 | final LevelReader level,
|
| 39 | final ScheduledTickAccess ticks,
|
| 40 | final BlockPos pos,
|
| 41 | final Direction directionToNeighbour,
|
| 42 | final BlockPos neighbourPos,
|
| 43 | final BlockState neighbourState,
|
| 44 | final RandomSource random
|
| 45 | ) {
|
| 46 | DoubleBlockHalf half = state.getValue(HALF);
|
| 47 | if (directionToNeighbour.getAxis() != Direction.Axis.Y
|
| 48 | || half == DoubleBlockHalf.LOWER != (directionToNeighbour == Direction.UP)
|
| 49 | || neighbourState.is(this) && neighbourState.getValue(HALF) != half) {
|
| 50 | return half == DoubleBlockHalf.LOWER && directionToNeighbour == Direction.DOWN && !state.canSurvive(level, pos)
|
| 51 | ? Blocks.AIR.defaultBlockState()
|
| 52 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 53 | } else {
|
| 54 | return Blocks.AIR.defaultBlockState();
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 60 | BlockPos pos = context.getClickedPos();
|
| 61 | Level level = context.getLevel();
|
| 62 | return pos.getY() < level.getMaxY() && level.getBlockState(pos.above()).canBeReplaced(context) ? super.getStateForPlacement(context) : null;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
|
| 67 | BlockPos abovePos = pos.above();
|
| 68 | level.setBlockAndUpdate(abovePos, copyWaterloggedFrom(level, abovePos, this.defaultBlockState().setValue(HALF, DoubleBlockHalf.UPPER)));
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 73 | if (state.getValue(HALF) != DoubleBlockHalf.UPPER) {
|
| 74 | return super.canSurvive(state, level, pos);
|
| 75 | }
|
| 76 |
|
| 77 | BlockState belowState = level.getBlockState(pos.below());
|
| 78 | return belowState.is(this) && belowState.getValue(HALF) == DoubleBlockHalf.LOWER;
|
| 79 | }
|
| 80 |
|
| 81 | public static void placeAt(final LevelAccessor level, final BlockState state, final BlockPos lowerPos, final @Block.UpdateFlags int updateType) {
|
| 82 | BlockPos upperPos = lowerPos.above();
|
| 83 | level.setBlock(lowerPos, copyWaterloggedFrom(level, lowerPos, state.setValue(HALF, DoubleBlockHalf.LOWER)), updateType);
|
| 84 | level.setBlock(upperPos, copyWaterloggedFrom(level, upperPos, state.setValue(HALF, DoubleBlockHalf.UPPER)), updateType);
|
| 85 | }
|
| 86 |
|
| 87 | public static BlockState copyWaterloggedFrom(final LevelReader level, final BlockPos pos, final BlockState state) {
|
| 88 | return state.hasProperty(BlockStateProperties.WATERLOGGED) ? state.setValue(BlockStateProperties.WATERLOGGED, level.isWaterAt(pos)) : state;
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
|
| 93 | if (!level.isClientSide()) {
|
| 94 | if (player.preventsBlockDrops()) {
|
| 95 | preventDropFromBottomPart(level, pos, state, player);
|
| 96 | } else {
|
| 97 | dropResources(state, level, pos, null, player, player.getMainHandItem());
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | return super.playerWillDestroy(level, pos, state, player);
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public void playerDestroy(
|
| 106 | final ServerLevel level,
|
| 107 | final ServerPlayer player,
|
| 108 | final BlockPos pos,
|
| 109 | final BlockState state,
|
| 110 | final @Nullable BlockEntity blockEntity,
|
| 111 | final ItemStack destroyedWith
|
| 112 | ) {
|
| 113 | super.playerDestroy(level, player, pos, Blocks.AIR.defaultBlockState(), blockEntity, destroyedWith);
|
| 114 | }
|
| 115 |
|
| 116 | protected static void preventDropFromBottomPart(final Level level, final BlockPos pos, final BlockState state, final Player player) {
|
| 117 | DoubleBlockHalf part = state.getValue(HALF);
|
| 118 | if (part == DoubleBlockHalf.UPPER) {
|
| 119 | BlockPos bottomPos = pos.below();
|
| 120 | BlockState bottomState = level.getBlockState(bottomPos);
|
| 121 | if (bottomState.is(state.getBlock()) && bottomState.getValue(HALF) == DoubleBlockHalf.LOWER) {
|
| 122 | BlockState blockState = bottomState.getFluidState().is(Fluids.WATER) ? Blocks.WATER.defaultBlockState() : Blocks.AIR.defaultBlockState();
|
| 123 | level.setBlock(bottomPos, blockState, 35);
|
| 124 | level.levelEvent(player, 2001, bottomPos, Block.getId(bottomState));
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 131 | builder.add(HALF);
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | protected long getSeed(final BlockState state, final BlockPos pos) {
|
| 136 | return Mth.getSeed(pos.getX(), pos.below(state.getValue(HALF) == DoubleBlockHalf.LOWER ? 0 : 1).getY(), pos.getZ());
|
| 137 | }
|
| 138 | }
|
| 139 |
|