| 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.level.Level;
|
| 8 | import net.minecraft.world.level.LevelReader;
|
| 9 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 10 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 13 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 14 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 15 | import net.minecraft.world.level.material.Fluids;
|
| 16 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 17 |
|
| 18 | public abstract class GrowingPlantHeadBlock extends GrowingPlantBlock implements BonemealableBlock {
|
| 19 | public static final IntegerProperty AGE = BlockStateProperties.AGE_25;
|
| 20 | public static final int MAX_AGE = 25;
|
| 21 | private final double growPerTickProbability;
|
| 22 |
|
| 23 | protected GrowingPlantHeadBlock(
|
| 24 | final BlockBehaviour.Properties properties,
|
| 25 | final Direction growthDirection,
|
| 26 | final VoxelShape shape,
|
| 27 | final boolean scheduleFluidTicks,
|
| 28 | final double growPerTickProbability
|
| 29 | ) {
|
| 30 | super(properties, growthDirection, shape, scheduleFluidTicks);
|
| 31 | this.growPerTickProbability = growPerTickProbability;
|
| 32 | this.registerDefaultState(this.stateDefinition.any().setValue(AGE, 0));
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public BlockState getStateForPlacement(final RandomSource random) {
|
| 37 | return this.defaultBlockState().setValue(AGE, random.nextInt(25));
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | protected boolean isRandomlyTicking(final BlockState state) {
|
| 42 | return state.getValue(AGE) < 25;
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 47 | if (state.getValue(AGE) < 25 && random.nextDouble() < this.growPerTickProbability) {
|
| 48 | BlockPos growthPos = pos.relative(this.growthDirection);
|
| 49 | if (this.canGrowInto(level.getBlockState(growthPos))) {
|
| 50 | level.setBlockAndUpdate(growthPos, this.getGrowIntoState(state, level.getRandom()));
|
| 51 | }
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | protected BlockState getGrowIntoState(final BlockState growFromState, final RandomSource random) {
|
| 56 | return growFromState.cycle(AGE);
|
| 57 | }
|
| 58 |
|
| 59 | public BlockState getMaxAgeState(final BlockState fromState) {
|
| 60 | return fromState.setValue(AGE, 25);
|
| 61 | }
|
| 62 |
|
| 63 | public boolean isMaxAge(final BlockState state) {
|
| 64 | return state.getValue(AGE) == 25;
|
| 65 | }
|
| 66 |
|
| 67 | protected BlockState updateBodyAfterConvertedFromHead(final BlockState headState, final BlockState bodyState) {
|
| 68 | return bodyState;
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected BlockState updateShape(
|
| 73 | final BlockState state,
|
| 74 | final LevelReader level,
|
| 75 | final ScheduledTickAccess ticks,
|
| 76 | final BlockPos pos,
|
| 77 | final Direction directionToNeighbour,
|
| 78 | final BlockPos neighbourPos,
|
| 79 | final BlockState neighbourState,
|
| 80 | final RandomSource random
|
| 81 | ) {
|
| 82 | if (directionToNeighbour == this.growthDirection.getOpposite()) {
|
| 83 | if (!state.canSurvive(level, pos)) {
|
| 84 | ticks.scheduleTick(pos, this, 1);
|
| 85 | } else {
|
| 86 | BlockState neighborInGrowthDirection = level.getBlockState(pos.relative(this.growthDirection));
|
| 87 | if (neighborInGrowthDirection.is(this) || neighborInGrowthDirection.is(this.getBodyBlock())) {
|
| 88 | return this.updateBodyAfterConvertedFromHead(state, this.getBodyBlock().defaultBlockState());
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | if (directionToNeighbour != this.growthDirection || !neighbourState.is(this) && !neighbourState.is(this.getBodyBlock())) {
|
| 94 | if (this.scheduleFluidTicks) {
|
| 95 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 96 | }
|
| 97 |
|
| 98 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 99 | } else {
|
| 100 | return this.updateBodyAfterConvertedFromHead(state, this.getBodyBlock().defaultBlockState());
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 106 | builder.add(AGE);
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 111 | BlockPos growthPos = pos.relative(this.growthDirection);
|
| 112 | return this.canGrowInto(level.getBlockState(growthPos)) && level.isInsideBuildHeight(growthPos);
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 117 | return true;
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 122 | BlockPos forwardPos = pos.relative(this.growthDirection);
|
| 123 | int nextAge = Math.min(state.getValue(AGE) + 1, 25);
|
| 124 | int blocksToGrow = this.getBlocksToGrowWhenBonemealed(random);
|
| 125 |
|
| 126 | for (int i = 0; i < blocksToGrow && this.canGrowInto(level.getBlockState(forwardPos)) && !level.isOutsideBuildHeight(forwardPos); i++) {
|
| 127 | level.setBlockAndUpdate(forwardPos, state.setValue(AGE, nextAge));
|
| 128 | forwardPos = forwardPos.relative(this.growthDirection);
|
| 129 | nextAge = Math.min(nextAge + 1, 25);
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | protected abstract int getBlocksToGrowWhenBonemealed(final RandomSource random);
|
| 134 |
|
| 135 | protected abstract boolean canGrowInto(final BlockState state);
|
| 136 |
|
| 137 | @Override
|
| 138 | protected GrowingPlantHeadBlock getHeadBlock() {
|
| 139 | return this;
|
| 140 | }
|
| 141 | }
|
| 142 |
|