| 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.util.RandomSource;
|
| 7 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 8 | import net.minecraft.world.level.BlockGetter;
|
| 9 | import net.minecraft.world.level.LevelReader;
|
| 10 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 13 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public abstract class GrowingPlantBlock extends Block {
|
| 17 | protected final Direction growthDirection;
|
| 18 | protected final boolean scheduleFluidTicks;
|
| 19 | protected final VoxelShape shape;
|
| 20 |
|
| 21 | protected GrowingPlantBlock(
|
| 22 | final BlockBehaviour.Properties properties, final Direction growthDirection, final VoxelShape shape, final boolean scheduleFluidTicks
|
| 23 | ) {
|
| 24 | super(properties);
|
| 25 | this.growthDirection = growthDirection;
|
| 26 | this.shape = shape;
|
| 27 | this.scheduleFluidTicks = scheduleFluidTicks;
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 32 | BlockState growthDirectionState = context.getLevel().getBlockState(context.getClickedPos().relative(this.growthDirection));
|
| 33 | return !growthDirectionState.is(this.getHeadBlock()) && !growthDirectionState.is(this.getBodyBlock())
|
| 34 | ? this.getStateForPlacement(context.getLevel().getRandom())
|
| 35 | : this.getBodyBlock().defaultBlockState();
|
| 36 | }
|
| 37 |
|
| 38 | public BlockState getStateForPlacement(final RandomSource random) {
|
| 39 | return this.defaultBlockState();
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 44 | BlockPos attachedToPos = pos.relative(this.growthDirection.getOpposite());
|
| 45 | BlockState attachedToState = level.getBlockState(attachedToPos);
|
| 46 | return !this.canAttachTo(attachedToState)
|
| 47 | ? false
|
| 48 | : attachedToState.is(this.getHeadBlock())
|
| 49 | || attachedToState.is(this.getBodyBlock())
|
| 50 | || attachedToState.isFaceSturdy(level, attachedToPos, this.growthDirection);
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 55 | if (!state.canSurvive(level, pos)) {
|
| 56 | level.destroyBlock(pos, true);
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | protected boolean canAttachTo(final BlockState state) {
|
| 61 | return true;
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 66 | return this.shape;
|
| 67 | }
|
| 68 |
|
| 69 | protected abstract GrowingPlantHeadBlock getHeadBlock();
|
| 70 |
|
| 71 | protected abstract Block getBodyBlock();
|
| 72 | }
|
| 73 |
|