| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.level.WorldGenLevel;
|
| 11 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 12 | import net.minecraft.world.level.levelgen.placement.PlacedFeature;
|
| 13 |
|
| 14 | public record CoralTreeFeature(Holder<PlacedFeature> feature) implements Feature {
|
| 15 | public static final MapCodec<CoralTreeFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 16 | i -> i.group(PlacedFeature.CODEC.fieldOf("feature").forGetter(CoralTreeFeature::feature)).apply(i, CoralTreeFeature::new)
|
| 17 | );
|
| 18 |
|
| 19 | @Override
|
| 20 | public MapCodec<CoralTreeFeature> codec() {
|
| 21 | return CODEC;
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 26 | BlockPos.MutableBlockPos mutPos = origin.mutable();
|
| 27 | int trunckHeight = random.nextInt(3) + 1;
|
| 28 |
|
| 29 | for (int i = 0; i < trunckHeight; i++) {
|
| 30 | if (!this.feature.value().place(level, chunkGenerator, random, mutPos)) {
|
| 31 | return true;
|
| 32 | }
|
| 33 |
|
| 34 | mutPos.move(Direction.UP);
|
| 35 | }
|
| 36 |
|
| 37 | BlockPos trunckTopPos = mutPos.immutable();
|
| 38 | int nBranches = random.nextInt(3) + 2;
|
| 39 | List<Direction> directions = Direction.Plane.HORIZONTAL.shuffledCopy(random);
|
| 40 |
|
| 41 | for (Direction branchDirection : directions.subList(0, nBranches)) {
|
| 42 | mutPos.set(trunckTopPos);
|
| 43 | mutPos.move(branchDirection);
|
| 44 | int branchHeight = random.nextInt(5) + 2;
|
| 45 | int segmentLength = 0;
|
| 46 |
|
| 47 | for (int j = 0; j < branchHeight && this.feature.value().place(level, chunkGenerator, random, mutPos); j++) {
|
| 48 | segmentLength++;
|
| 49 | mutPos.move(Direction.UP);
|
| 50 | if (j == 0 || segmentLength >= 2 && random.nextFloat() < 0.25F) {
|
| 51 | mutPos.move(branchDirection);
|
| 52 | segmentLength = 0;
|
| 53 | }
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | return true;
|
| 58 | }
|
| 59 | }
|
| 60 |
|