| 1 | package net.minecraft.world.level.levelgen.feature.trunkplacers;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.List;
|
| 7 | import java.util.function.BiConsumer;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Direction;
|
| 10 | import net.minecraft.util.ExtraCodecs;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.util.valueproviders.IntProvider;
|
| 13 | import net.minecraft.util.valueproviders.IntProviders;
|
| 14 | import net.minecraft.world.level.WorldGenLevel;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.levelgen.feature.TreeFeature;
|
| 17 | import net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacer;
|
| 18 |
|
| 19 | public class BendingTrunkPlacer extends TrunkPlacer {
|
| 20 | public static final MapCodec<BendingTrunkPlacer> CODEC = RecordCodecBuilder.mapCodec(
|
| 21 | i -> trunkPlacerParts(i)
|
| 22 | .and(
|
| 23 | i.group(
|
| 24 | ExtraCodecs.POSITIVE_INT.optionalFieldOf("min_height_for_leaves", 1).forGetter(c -> c.minHeightForLeaves),
|
| 25 | IntProviders.codec(1, 64).fieldOf("bend_length").forGetter(c -> c.bendLength)
|
| 26 | )
|
| 27 | )
|
| 28 | .apply(i, BendingTrunkPlacer::new)
|
| 29 | );
|
| 30 | private final int minHeightForLeaves;
|
| 31 | private final IntProvider bendLength;
|
| 32 |
|
| 33 | public BendingTrunkPlacer(final int baseHeight, final int heightRandA, final int heightRandB, final int minHeightForLeaves, final IntProvider bendLength) {
|
| 34 | super(baseHeight, heightRandA, heightRandB);
|
| 35 | this.minHeightForLeaves = minHeightForLeaves;
|
| 36 | this.bendLength = bendLength;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected TrunkPlacerType<?> type() {
|
| 41 | return TrunkPlacerType.BENDING_TRUNK_PLACER;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public List<FoliagePlacer.FoliageAttachment> placeTrunk(
|
| 46 | final WorldGenLevel level,
|
| 47 | final BiConsumer<BlockPos, BlockState> trunkSetter,
|
| 48 | final RandomSource random,
|
| 49 | final int treeHeight,
|
| 50 | final BlockPos origin,
|
| 51 | final TreeFeature tree
|
| 52 | ) {
|
| 53 | Direction direction = Direction.Plane.HORIZONTAL.getRandomDirection(random);
|
| 54 | int logHeight = treeHeight - 1;
|
| 55 | BlockPos.MutableBlockPos pos = origin.mutable();
|
| 56 | BlockPos belowPos = pos.below();
|
| 57 | placeBelowTrunkBlock(level, trunkSetter, random, belowPos, tree);
|
| 58 | List<FoliagePlacer.FoliageAttachment> foliagePoints = Lists.newArrayList();
|
| 59 |
|
| 60 | for (int i = 0; i <= logHeight; i++) {
|
| 61 | if (i + 1 >= logHeight + random.nextInt(2)) {
|
| 62 | pos.move(direction);
|
| 63 | }
|
| 64 |
|
| 65 | if (TreeFeature.validTreePos(level, pos)) {
|
| 66 | this.placeLog(level, trunkSetter, random, pos, tree);
|
| 67 | }
|
| 68 |
|
| 69 | if (i >= this.minHeightForLeaves) {
|
| 70 | foliagePoints.add(new FoliagePlacer.FoliageAttachment(pos.immutable(), 0, false));
|
| 71 | }
|
| 72 |
|
| 73 | pos.move(Direction.UP);
|
| 74 | }
|
| 75 |
|
| 76 | int dirLength = this.bendLength.sample(random);
|
| 77 |
|
| 78 | for (int i = 0; i <= dirLength; i++) {
|
| 79 | if (TreeFeature.validTreePos(level, pos)) {
|
| 80 | this.placeLog(level, trunkSetter, random, pos, tree);
|
| 81 | }
|
| 82 |
|
| 83 | foliagePoints.add(new FoliagePlacer.FoliageAttachment(pos.immutable(), 0, false));
|
| 84 | pos.move(direction);
|
| 85 | }
|
| 86 |
|
| 87 | return foliagePoints;
|
| 88 | }
|
| 89 | }
|
| 90 |
|