| 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.entity.projectile.Projectile;
|
| 9 | import net.minecraft.world.level.BlockGetter;
|
| 10 | import net.minecraft.world.level.Level;
|
| 11 | import net.minecraft.world.level.LevelAccessor;
|
| 12 | import net.minecraft.world.level.LevelReader;
|
| 13 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 14 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 17 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 18 | import net.minecraft.world.level.block.state.properties.IntegerProperty;
|
| 19 | import net.minecraft.world.phys.BlockHitResult;
|
| 20 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | /**
|
| 24 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 25 | */
|
| 26 | public class ChorusFlowerBlock extends Block {
|
| 27 | public static final int DEAD_AGE = 5;
|
| 28 | public static final IntegerProperty AGE = BlockStateProperties.AGE_5;
|
| 29 | private static final VoxelShape SHAPE_BLOCK_SUPPORT = Block.column(14.0, 0.0, 15.0);
|
| 30 | private final Block plant;
|
| 31 |
|
| 32 | /**
|
| 33 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 34 | */
|
| 35 | public ChorusFlowerBlock(final Block plant, final BlockBehaviour.Properties properties) {
|
| 36 | super(properties);
|
| 37 | this.plant = plant;
|
| 38 | this.registerDefaultState(this.stateDefinition.any().setValue(AGE, 0));
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 43 | if (!state.canSurvive(level, pos)) {
|
| 44 | level.destroyBlock(pos, true);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | protected boolean isRandomlyTicking(final BlockState state) {
|
| 50 | return state.getValue(AGE) < 5;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public VoxelShape getBlockSupportShape(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 55 | return SHAPE_BLOCK_SUPPORT;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 60 | BlockPos above = pos.above();
|
| 61 | if (level.isEmptyBlock(above) && above.getY() <= level.getMaxY()) {
|
| 62 | int currentAge = state.getValue(AGE);
|
| 63 | if (currentAge < 5) {
|
| 64 | boolean growUpwards = false;
|
| 65 | boolean pillarOnSupportBlock = false;
|
| 66 | BlockState belowState = level.getBlockState(pos.below());
|
| 67 | if (belowState.is(BlockTags.SUPPORTS_CHORUS_FLOWER)) {
|
| 68 | growUpwards = true;
|
| 69 | } else if (belowState.is(this.plant)) {
|
| 70 | int height = 1;
|
| 71 |
|
| 72 | for (int i = 0; i < 4; i++) {
|
| 73 | BlockState testState = level.getBlockState(pos.below(height + 1));
|
| 74 | if (!testState.is(this.plant)) {
|
| 75 | if (testState.is(BlockTags.SUPPORTS_CHORUS_FLOWER)) {
|
| 76 | pillarOnSupportBlock = true;
|
| 77 | }
|
| 78 | break;
|
| 79 | }
|
| 80 |
|
| 81 | height++;
|
| 82 | }
|
| 83 |
|
| 84 | if (height < 2 || height <= random.nextInt(pillarOnSupportBlock ? 5 : 4)) {
|
| 85 | growUpwards = true;
|
| 86 | }
|
| 87 | } else if (belowState.isAir()) {
|
| 88 | growUpwards = true;
|
| 89 | }
|
| 90 |
|
| 91 | if (growUpwards && allNeighborsEmpty(level, above, null) && level.isEmptyBlock(pos.above(2))) {
|
| 92 | level.setBlock(pos, ChorusPlantBlock.getStateWithConnections(level, pos, this.plant.defaultBlockState()), 2);
|
| 93 | this.placeGrownFlower(level, above, currentAge);
|
| 94 | } else if (currentAge < 4) {
|
| 95 | int numBranchAttempts = random.nextInt(4);
|
| 96 | if (pillarOnSupportBlock) {
|
| 97 | numBranchAttempts++;
|
| 98 | }
|
| 99 |
|
| 100 | boolean createdBranch = false;
|
| 101 |
|
| 102 | for (int i = 0; i < numBranchAttempts; i++) {
|
| 103 | Direction direction = Direction.Plane.HORIZONTAL.getRandomDirection(random);
|
| 104 | BlockPos target = pos.relative(direction);
|
| 105 | if (level.isEmptyBlock(target) && level.isEmptyBlock(target.below()) && allNeighborsEmpty(level, target, direction.getOpposite())) {
|
| 106 | this.placeGrownFlower(level, target, currentAge + 1);
|
| 107 | createdBranch = true;
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | if (createdBranch) {
|
| 112 | level.setBlock(pos, ChorusPlantBlock.getStateWithConnections(level, pos, this.plant.defaultBlockState()), 2);
|
| 113 | } else {
|
| 114 | this.placeDeadFlower(level, pos);
|
| 115 | }
|
| 116 | } else {
|
| 117 | this.placeDeadFlower(level, pos);
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | private void placeGrownFlower(final Level level, final BlockPos pos, final int age) {
|
| 124 | level.setBlock(pos, this.defaultBlockState().setValue(AGE, age), 2);
|
| 125 | level.levelEvent(1033, pos, 0);
|
| 126 | }
|
| 127 |
|
| 128 | private void placeDeadFlower(final Level level, final BlockPos pos) {
|
| 129 | level.setBlock(pos, this.defaultBlockState().setValue(AGE, 5), 2);
|
| 130 | level.levelEvent(1034, pos, 0);
|
| 131 | }
|
| 132 |
|
| 133 | private static boolean allNeighborsEmpty(final LevelReader level, final BlockPos pos, final @Nullable Direction ignore) {
|
| 134 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 135 | if (direction != ignore && !level.isEmptyBlock(pos.relative(direction))) {
|
| 136 | return false;
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | return true;
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | protected BlockState updateShape(
|
| 145 | final BlockState state,
|
| 146 | final LevelReader level,
|
| 147 | final ScheduledTickAccess ticks,
|
| 148 | final BlockPos pos,
|
| 149 | final Direction directionToNeighbour,
|
| 150 | final BlockPos neighbourPos,
|
| 151 | final BlockState neighbourState,
|
| 152 | final RandomSource random
|
| 153 | ) {
|
| 154 | if (directionToNeighbour != Direction.UP && !state.canSurvive(level, pos)) {
|
| 155 | ticks.scheduleTick(pos, this, 1);
|
| 156 | }
|
| 157 |
|
| 158 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 163 | BlockState belowState = level.getBlockState(pos.below());
|
| 164 | if (!belowState.is(this.plant) && !belowState.is(BlockTags.SUPPORTS_CHORUS_FLOWER)) {
|
| 165 | if (!belowState.isAir()) {
|
| 166 | return false;
|
| 167 | }
|
| 168 |
|
| 169 | boolean oneNeighbor = false;
|
| 170 |
|
| 171 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 172 | BlockState neighbor = level.getBlockState(pos.relative(direction));
|
| 173 | if (neighbor.is(this.plant)) {
|
| 174 | if (oneNeighbor) {
|
| 175 | return false;
|
| 176 | }
|
| 177 |
|
| 178 | oneNeighbor = true;
|
| 179 | } else if (!neighbor.isAir()) {
|
| 180 | return false;
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | return oneNeighbor;
|
| 185 | } else {
|
| 186 | return true;
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 192 | builder.add(AGE);
|
| 193 | }
|
| 194 |
|
| 195 | public static void generatePlant(final LevelAccessor level, final BlockPos target, final RandomSource random, final int maxHorizontalSpread) {
|
| 196 | level.setBlock(target, ChorusPlantBlock.getStateWithConnections(level, target, Blocks.CHORUS_PLANT.defaultBlockState()), 2);
|
| 197 | growTreeRecursive(level, target, random, target, maxHorizontalSpread, 0);
|
| 198 | }
|
| 199 |
|
| 200 | private static void growTreeRecursive(
|
| 201 | final LevelAccessor level, final BlockPos current, final RandomSource random, final BlockPos startPos, final int maxHorizontalSpread, final int depth
|
| 202 | ) {
|
| 203 | Block chorus = Blocks.CHORUS_PLANT;
|
| 204 | int height = random.nextInt(4) + 1;
|
| 205 | if (depth == 0) {
|
| 206 | height++;
|
| 207 | }
|
| 208 |
|
| 209 | for (int i = 0; i < height; i++) {
|
| 210 | BlockPos target = current.above(i + 1);
|
| 211 | if (!allNeighborsEmpty(level, target, null)) {
|
| 212 | return;
|
| 213 | }
|
| 214 |
|
| 215 | level.setBlock(target, ChorusPlantBlock.getStateWithConnections(level, target, chorus.defaultBlockState()), 2);
|
| 216 | level.setBlock(target.below(), ChorusPlantBlock.getStateWithConnections(level, target.below(), chorus.defaultBlockState()), 2);
|
| 217 | }
|
| 218 |
|
| 219 | boolean placedStem = false;
|
| 220 | if (depth < 4) {
|
| 221 | int stems = random.nextInt(4);
|
| 222 | if (depth == 0) {
|
| 223 | stems++;
|
| 224 | }
|
| 225 |
|
| 226 | for (int i = 0; i < stems; i++) {
|
| 227 | Direction direction = Direction.Plane.HORIZONTAL.getRandomDirection(random);
|
| 228 | BlockPos target = current.above(height).relative(direction);
|
| 229 | if (Math.abs(target.getX() - startPos.getX()) < maxHorizontalSpread
|
| 230 | && Math.abs(target.getZ() - startPos.getZ()) < maxHorizontalSpread
|
| 231 | && level.isEmptyBlock(target)
|
| 232 | && level.isEmptyBlock(target.below())
|
| 233 | && allNeighborsEmpty(level, target, direction.getOpposite())) {
|
| 234 | placedStem = true;
|
| 235 | level.setBlock(target, ChorusPlantBlock.getStateWithConnections(level, target, chorus.defaultBlockState()), 2);
|
| 236 | level.setBlock(
|
| 237 | target.relative(direction.getOpposite()),
|
| 238 | ChorusPlantBlock.getStateWithConnections(level, target.relative(direction.getOpposite()), chorus.defaultBlockState()),
|
| 239 | 2
|
| 240 | );
|
| 241 | growTreeRecursive(level, target, random, startPos, maxHorizontalSpread, depth + 1);
|
| 242 | }
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | if (!placedStem) {
|
| 247 | level.setBlock(current.above(height), Blocks.CHORUS_FLOWER.defaultBlockState().setValue(AGE, 5), 2);
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | @Override
|
| 252 | protected void onProjectileHit(final Level level, final BlockState state, final BlockHitResult blockHit, final Projectile projectile) {
|
| 253 | BlockPos pos = blockHit.getBlockPos();
|
| 254 | if (level instanceof ServerLevel serverLevel && projectile.mayInteract(serverLevel, pos) && projectile.mayBreak(serverLevel, pos)) {
|
| 255 | level.destroyBlock(pos, true, projectile);
|
| 256 | }
|
| 257 | }
|
| 258 | }
|
| 259 |
|