| 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.tags.BlockTags;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 9 | import net.minecraft.world.level.BlockGetter;
|
| 10 | import net.minecraft.world.level.Level;
|
| 11 | import net.minecraft.world.level.LevelReader;
|
| 12 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 13 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 16 | import net.minecraft.world.level.block.state.properties.BambooLeaves;
|
| 17 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 18 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 19 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 20 | import net.minecraft.world.level.material.FluidState;
|
| 21 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 22 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 23 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | public class BambooStalkBlock extends Block implements BonemealableBlock {
|
| 27 | private static final VoxelShape SHAPE_SMALL = Block.column(6.0, 0.0, 16.0);
|
| 28 | private static final VoxelShape SHAPE_LARGE = Block.column(10.0, 0.0, 16.0);
|
| 29 | private static final VoxelShape SHAPE_COLLISION = Block.column(3.0, 0.0, 16.0);
|
| 30 | public static final IntegerProperty AGE = BlockStateProperties.AGE_1;
|
| 31 | public static final EnumProperty<BambooLeaves> LEAVES = BlockStateProperties.BAMBOO_LEAVES;
|
| 32 | public static final IntegerProperty STAGE = BlockStateProperties.STAGE;
|
| 33 | public static final int MAX_HEIGHT = 16;
|
| 34 | public static final int STAGE_GROWING = 0;
|
| 35 | public static final int STAGE_DONE_GROWING = 1;
|
| 36 | public static final int AGE_THIN_BAMBOO = 0;
|
| 37 | public static final int AGE_THICK_BAMBOO = 1;
|
| 38 |
|
| 39 | public BambooStalkBlock(final BlockBehaviour.Properties properties) {
|
| 40 | super(properties);
|
| 41 | this.registerDefaultState(this.stateDefinition.any().setValue(AGE, 0).setValue(LEAVES, BambooLeaves.NONE).setValue(STAGE, 0));
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 46 | builder.add(AGE, LEAVES, STAGE);
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | protected boolean propagatesSkylightDown(final BlockState state) {
|
| 51 | return true;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 56 | VoxelShape shape = state.getValue(LEAVES) == BambooLeaves.LARGE ? SHAPE_LARGE : SHAPE_SMALL;
|
| 57 | return shape.move(state.getOffset(pos));
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 62 | return false;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 67 | return SHAPE_COLLISION.move(state.getOffset(pos));
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected boolean isCollisionShapeFullBlock(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 72 | return false;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 77 | FluidState fluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 78 | if (!fluidState.isEmpty()) {
|
| 79 | return null;
|
| 80 | }
|
| 81 |
|
| 82 | BlockState belowState = context.getLevel().getBlockState(context.getClickedPos().below());
|
| 83 | if (belowState.is(BlockTags.SUPPORTS_BAMBOO)) {
|
| 84 | if (belowState.is(Blocks.BAMBOO_SAPLING)) {
|
| 85 | return this.defaultBlockState().setValue(AGE, 0);
|
| 86 | } else if (belowState.is(Blocks.BAMBOO)) {
|
| 87 | int age = belowState.getValue(AGE) > 0 ? 1 : 0;
|
| 88 | return this.defaultBlockState().setValue(AGE, age);
|
| 89 | } else {
|
| 90 | BlockState aboveState = context.getLevel().getBlockState(context.getClickedPos().above());
|
| 91 | return aboveState.is(Blocks.BAMBOO) ? this.defaultBlockState().setValue(AGE, aboveState.getValue(AGE)) : Blocks.BAMBOO_SAPLING.defaultBlockState();
|
| 92 | }
|
| 93 | } else {
|
| 94 | return null;
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 100 | if (!state.canSurvive(level, pos)) {
|
| 101 | level.destroyBlock(pos, true);
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | protected boolean isRandomlyTicking(final BlockState state) {
|
| 107 | return state.getValue(STAGE) == 0;
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 112 | if (state.getValue(STAGE) == 0) {
|
| 113 | if (random.nextInt(3) == 0 && level.isEmptyBlock(pos.above()) && level.getRawBrightness(pos.above(), 0) >= 9) {
|
| 114 | int height = this.getHeightBelowUpToMax(level, pos) + 1;
|
| 115 | if (height < 16) {
|
| 116 | this.growBamboo(state, level, pos, random, height);
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 124 | return level.getBlockState(pos.below()).is(BlockTags.SUPPORTS_BAMBOO);
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | protected BlockState updateShape(
|
| 129 | final BlockState state,
|
| 130 | final LevelReader level,
|
| 131 | final ScheduledTickAccess ticks,
|
| 132 | final BlockPos pos,
|
| 133 | final Direction directionToNeighbour,
|
| 134 | final BlockPos neighbourPos,
|
| 135 | final BlockState neighbourState,
|
| 136 | final RandomSource random
|
| 137 | ) {
|
| 138 | if (!state.canSurvive(level, pos)) {
|
| 139 | ticks.scheduleTick(pos, this, 1);
|
| 140 | }
|
| 141 |
|
| 142 | return directionToNeighbour == Direction.UP && neighbourState.is(Blocks.BAMBOO) && neighbourState.getValue(AGE) > state.getValue(AGE)
|
| 143 | ? state.cycle(AGE)
|
| 144 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 149 | int heightAbove = this.getHeightAboveUpToMax(level, pos);
|
| 150 | int heightBelow = this.getHeightBelowUpToMax(level, pos);
|
| 151 | BlockPos growthPos = pos.above(heightAbove + 1);
|
| 152 | return heightAbove + heightBelow + 1 < 16
|
| 153 | && level.getBlockState(pos.above(heightAbove)).getValue(STAGE) != 1
|
| 154 | && level.isInsideBuildHeight(growthPos)
|
| 155 | && level.isEmptyBlock(growthPos);
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 160 | return true;
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
|
| 165 | int heightAbove = this.getHeightAboveUpToMax(level, pos);
|
| 166 | int heightBelow = this.getHeightBelowUpToMax(level, pos);
|
| 167 | int totalHeight = heightAbove + heightBelow + 1;
|
| 168 | int newBamboo = 1 + random.nextInt(2);
|
| 169 |
|
| 170 | for (int i = 0; i < newBamboo; i++) {
|
| 171 | BlockPos topPos = pos.above(heightAbove);
|
| 172 | BlockState topState = level.getBlockState(topPos);
|
| 173 | BlockPos growthPos = topPos.above();
|
| 174 | if (totalHeight >= 16 || topState.getValue(STAGE) == 1 || !level.isEmptyBlock(growthPos) || level.isOutsideBuildHeight(growthPos)) {
|
| 175 | return;
|
| 176 | }
|
| 177 |
|
| 178 | this.growBamboo(topState, level, topPos, random, totalHeight);
|
| 179 | heightAbove++;
|
| 180 | totalHeight++;
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | protected void growBamboo(final BlockState state, final Level level, final BlockPos pos, final RandomSource random, final int height) {
|
| 185 | BlockState belowState = level.getBlockState(pos.below());
|
| 186 | BlockPos twoBelowPos = pos.below(2);
|
| 187 | BlockState twoBelowState = level.getBlockState(twoBelowPos);
|
| 188 | BambooLeaves leaves = BambooLeaves.NONE;
|
| 189 | if (height >= 1) {
|
| 190 | if (!belowState.is(Blocks.BAMBOO) || belowState.getValue(LEAVES) == BambooLeaves.NONE) {
|
| 191 | leaves = BambooLeaves.SMALL;
|
| 192 | } else if (belowState.is(Blocks.BAMBOO) && belowState.getValue(LEAVES) != BambooLeaves.NONE) {
|
| 193 | leaves = BambooLeaves.LARGE;
|
| 194 | if (twoBelowState.is(Blocks.BAMBOO)) {
|
| 195 | level.setBlockAndUpdate(pos.below(), belowState.setValue(LEAVES, BambooLeaves.SMALL));
|
| 196 | level.setBlockAndUpdate(twoBelowPos, twoBelowState.setValue(LEAVES, BambooLeaves.NONE));
|
| 197 | }
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | int age = state.getValue(AGE) != 1 && !twoBelowState.is(Blocks.BAMBOO) ? 0 : 1;
|
| 202 | int stage = (height < 11 || !(random.nextFloat() < 0.25F)) && height != 15 ? 0 : 1;
|
| 203 | level.setBlockAndUpdate(pos.above(), this.defaultBlockState().setValue(AGE, age).setValue(LEAVES, leaves).setValue(STAGE, stage));
|
| 204 | }
|
| 205 |
|
| 206 | protected int getHeightAboveUpToMax(final BlockGetter level, final BlockPos pos) {
|
| 207 | int height = 0;
|
| 208 |
|
| 209 | while (height < 16 && level.getBlockState(pos.above(height + 1)).is(Blocks.BAMBOO)) {
|
| 210 | height++;
|
| 211 | }
|
| 212 |
|
| 213 | return height;
|
| 214 | }
|
| 215 |
|
| 216 | protected int getHeightBelowUpToMax(final BlockGetter level, final BlockPos pos) {
|
| 217 | int height = 0;
|
| 218 |
|
| 219 | while (height < 16 && level.getBlockState(pos.below(height + 1)).is(Blocks.BAMBOO)) {
|
| 220 | height++;
|
| 221 | }
|
| 222 |
|
| 223 | return height;
|
| 224 | }
|
| 225 | }
|
| 226 |
|