| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.level.LevelAccessor;
|
| 11 | import net.minecraft.world.level.WorldGenLevel;
|
| 12 | import net.minecraft.world.level.block.Block;
|
| 13 | import net.minecraft.world.level.block.Blocks;
|
| 14 | import net.minecraft.world.level.block.GrowingPlantHeadBlock;
|
| 15 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 18 | import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
|
| 19 |
|
| 20 | public record HugeFungusFeature(
|
| 21 | BlockState validBaseState, BlockState stemState, BlockState hatState, BlockState decorState, BlockPredicate replaceableBlocks, boolean planted
|
| 22 | ) implements Feature {
|
| 23 | private static final float HUGE_PROBABILITY = 0.06F;
|
| 24 | public static final MapCodec<HugeFungusFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 25 | i -> i.group(
|
| 26 | BlockState.CODEC.fieldOf("valid_base_block").forGetter(HugeFungusFeature::validBaseState),
|
| 27 | BlockState.CODEC.fieldOf("stem_state").forGetter(HugeFungusFeature::stemState),
|
| 28 | BlockState.CODEC.fieldOf("hat_state").forGetter(HugeFungusFeature::hatState),
|
| 29 | BlockState.CODEC.fieldOf("decor_state").forGetter(HugeFungusFeature::decorState),
|
| 30 | BlockPredicate.CODEC.fieldOf("replaceable_blocks").forGetter(HugeFungusFeature::replaceableBlocks),
|
| 31 | Codec.BOOL.optionalFieldOf("planted", false).forGetter(HugeFungusFeature::planted)
|
| 32 | )
|
| 33 | .apply(i, HugeFungusFeature::new)
|
| 34 | );
|
| 35 |
|
| 36 | @Override
|
| 37 | public MapCodec<HugeFungusFeature> codec() {
|
| 38 | return CODEC;
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 43 | Block allowedBaseBlock = this.validBaseState.getBlock();
|
| 44 | BlockPos newOrigin = null;
|
| 45 | BlockState belowState = level.getBlockState(origin.below());
|
| 46 | if (belowState.is(allowedBaseBlock)) {
|
| 47 | newOrigin = origin;
|
| 48 | }
|
| 49 |
|
| 50 | if (newOrigin == null) {
|
| 51 | return false;
|
| 52 | }
|
| 53 |
|
| 54 | int totalHeight = Mth.nextInt(random, 4, 13);
|
| 55 | if (random.nextInt(12) == 0) {
|
| 56 | totalHeight *= 2;
|
| 57 | }
|
| 58 |
|
| 59 | if (!this.planted()) {
|
| 60 | int maxHeight = chunkGenerator.getGenDepth();
|
| 61 | if (newOrigin.getY() + totalHeight + 1 >= maxHeight) {
|
| 62 | return false;
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | boolean isHuge = !this.planted() && random.nextFloat() < 0.06F;
|
| 67 | level.setBlock(origin, Blocks.AIR.defaultBlockState(), 260);
|
| 68 | this.placeStem(level, random, newOrigin, totalHeight, isHuge);
|
| 69 | this.placeHat(level, random, newOrigin, totalHeight, isHuge);
|
| 70 | return true;
|
| 71 | }
|
| 72 |
|
| 73 | private boolean isReplaceable(final WorldGenLevel level, final BlockPos pos, final boolean checkNonReplaceablePlants) {
|
| 74 | if (level.isStateAtPosition(pos, BlockBehaviour.BlockStateBase::canBeReplaced)) {
|
| 75 | return true;
|
| 76 | } else {
|
| 77 | return checkNonReplaceablePlants ? this.replaceableBlocks.test(level, pos) : false;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | private void placeStem(final WorldGenLevel level, final RandomSource random, final BlockPos surfaceOrigin, final int totalHeight, final boolean isHuge) {
|
| 82 | BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
|
| 83 | int stemRadius = isHuge ? 1 : 0;
|
| 84 |
|
| 85 | for (int dx = -stemRadius; dx <= stemRadius; dx++) {
|
| 86 | for (int dz = -stemRadius; dz <= stemRadius; dz++) {
|
| 87 | boolean cornerOfHugeStem = isHuge && Mth.abs(dx) == stemRadius && Mth.abs(dz) == stemRadius;
|
| 88 |
|
| 89 | for (int dy = 0; dy < totalHeight; dy++) {
|
| 90 | blockPos.setWithOffset(surfaceOrigin, dx, dy, dz);
|
| 91 | if (this.isReplaceable(level, blockPos, true)) {
|
| 92 | if (this.planted) {
|
| 93 | if (!level.getBlockState(blockPos.below()).isAir()) {
|
| 94 | level.destroyBlock(blockPos, true);
|
| 95 | }
|
| 96 |
|
| 97 | level.setBlockAndUpdate(blockPos, this.stemState);
|
| 98 | } else if (cornerOfHugeStem) {
|
| 99 | if (random.nextFloat() < 0.1F) {
|
| 100 | this.setBlock(level, blockPos, this.stemState);
|
| 101 | }
|
| 102 | } else {
|
| 103 | this.setBlock(level, blockPos, this.stemState);
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | private void placeHat(final WorldGenLevel level, final RandomSource random, final BlockPos surfaceOrigin, final int totalHeight, final boolean isHuge) {
|
| 112 | BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
|
| 113 | boolean placeVines = this.hatState.is(Blocks.NETHER_WART_BLOCK);
|
| 114 | int hatHeight = Math.min(random.nextInt(1 + totalHeight / 3) + 5, totalHeight);
|
| 115 | int hatStartY = totalHeight - hatHeight;
|
| 116 |
|
| 117 | for (int dy = hatStartY; dy <= totalHeight; dy++) {
|
| 118 | int radius = dy < totalHeight - random.nextInt(3) ? 2 : 1;
|
| 119 | if (hatHeight > 8 && dy < hatStartY + 4) {
|
| 120 | radius = 3;
|
| 121 | }
|
| 122 |
|
| 123 | if (isHuge) {
|
| 124 | radius++;
|
| 125 | }
|
| 126 |
|
| 127 | for (int dx = -radius; dx <= radius; dx++) {
|
| 128 | for (int dz = -radius; dz <= radius; dz++) {
|
| 129 | boolean isEdgeX = dx == -radius || dx == radius;
|
| 130 | boolean isEdgeZ = dz == -radius || dz == radius;
|
| 131 | boolean inside = !isEdgeX && !isEdgeZ && dy != totalHeight;
|
| 132 | boolean corner = isEdgeX && isEdgeZ;
|
| 133 | boolean isHatBottom = dy < hatStartY + 3;
|
| 134 | blockPos.setWithOffset(surfaceOrigin, dx, dy, dz);
|
| 135 | if (this.isReplaceable(level, blockPos, false)) {
|
| 136 | if (this.planted && !level.getBlockState(blockPos.below()).isAir()) {
|
| 137 | level.destroyBlock(blockPos, true);
|
| 138 | }
|
| 139 |
|
| 140 | if (isHatBottom) {
|
| 141 | if (!inside) {
|
| 142 | this.placeHatDropBlock(level, random, blockPos, placeVines);
|
| 143 | }
|
| 144 | } else if (inside) {
|
| 145 | this.placeHatBlock(level, random, blockPos, 0.1F, 0.2F, placeVines ? 0.1F : 0.0F);
|
| 146 | } else if (corner) {
|
| 147 | this.placeHatBlock(level, random, blockPos, 0.01F, 0.7F, placeVines ? 0.083F : 0.0F);
|
| 148 | } else {
|
| 149 | this.placeHatBlock(level, random, blockPos, 5.0E-4F, 0.98F, placeVines ? 0.07F : 0.0F);
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | private void placeHatBlock(
|
| 158 | final LevelAccessor level,
|
| 159 | final RandomSource random,
|
| 160 | final BlockPos.MutableBlockPos blockPos,
|
| 161 | final float decorBlockProbability,
|
| 162 | final float hatBlockProbability,
|
| 163 | final float vinesProbability
|
| 164 | ) {
|
| 165 | if (random.nextFloat() < decorBlockProbability) {
|
| 166 | this.setBlock(level, blockPos, this.decorState);
|
| 167 | } else if (random.nextFloat() < hatBlockProbability) {
|
| 168 | this.setBlock(level, blockPos, this.hatState);
|
| 169 | if (random.nextFloat() < vinesProbability) {
|
| 170 | tryPlaceWeepingVines(blockPos, level, random);
|
| 171 | }
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | private void placeHatDropBlock(final LevelAccessor level, final RandomSource random, final BlockPos blockPos, final boolean placeVines) {
|
| 176 | if (level.getBlockState(blockPos.below()).is(this.hatState.getBlock())) {
|
| 177 | this.setBlock(level, blockPos, this.hatState);
|
| 178 | } else if (random.nextFloat() < 0.15) {
|
| 179 | this.setBlock(level, blockPos, this.hatState);
|
| 180 | if (placeVines && random.nextInt(11) == 0) {
|
| 181 | tryPlaceWeepingVines(blockPos, level, random);
|
| 182 | }
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | private static void tryPlaceWeepingVines(final BlockPos hatBlockPos, final LevelAccessor level, final RandomSource random) {
|
| 187 | BlockPos.MutableBlockPos placePos = hatBlockPos.mutable().move(Direction.DOWN);
|
| 188 | if (level.isEmptyBlock(placePos)) {
|
| 189 | int goalVineHeight = Mth.nextInt(random, 1, 5);
|
| 190 | if (random.nextInt(7) == 0) {
|
| 191 | goalVineHeight *= 2;
|
| 192 | }
|
| 193 |
|
| 194 | int minVineAge = 23;
|
| 195 | int maxVineAge = 25;
|
| 196 | placeWeepingVinesColumn(level, random, placePos, goalVineHeight, 23, 25);
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | private static void placeWeepingVinesColumn(
|
| 201 | final LevelAccessor level, final RandomSource random, final BlockPos origin, final int totalHeight, final int minAge, final int naxAge
|
| 202 | ) {
|
| 203 | BlockPos.MutableBlockPos placePos = origin.mutable();
|
| 204 |
|
| 205 | for (int height = 0; height <= totalHeight; height++) {
|
| 206 | if (level.isEmptyBlock(placePos)) {
|
| 207 | if (height == totalHeight || !level.isEmptyBlock(placePos.below())) {
|
| 208 | level.setBlock(placePos, Blocks.WEEPING_VINES.defaultBlockState().setValue(GrowingPlantHeadBlock.AGE, Mth.nextInt(random, minAge, naxAge)), 2);
|
| 209 | break;
|
| 210 | }
|
| 211 |
|
| 212 | level.setBlock(placePos, Blocks.WEEPING_VINES_PLANT.defaultBlockState(), 2);
|
| 213 | }
|
| 214 |
|
| 215 | placePos.move(Direction.DOWN);
|
| 216 | }
|
| 217 | }
|
| 218 | }
|
| 219 |
|