| 1 | package net.minecraft.world.level.levelgen.feature.trunkplacers;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.List;
|
| 8 | import java.util.function.BiConsumer;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.core.HolderSet;
|
| 12 | import net.minecraft.core.registries.Registries;
|
| 13 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.util.valueproviders.IntProvider;
|
| 16 | import net.minecraft.util.valueproviders.IntProviders;
|
| 17 | import net.minecraft.world.level.WorldGenLevel;
|
| 18 | import net.minecraft.world.level.block.Block;
|
| 19 | import net.minecraft.world.level.block.state.BlockState;
|
| 20 | import net.minecraft.world.level.levelgen.feature.TreeFeature;
|
| 21 | import net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacer;
|
| 22 |
|
| 23 | public class UpwardsBranchingTrunkPlacer extends TrunkPlacer {
|
| 24 | public static final MapCodec<UpwardsBranchingTrunkPlacer> CODEC = RecordCodecBuilder.mapCodec(
|
| 25 | i -> trunkPlacerParts(i)
|
| 26 | .and(
|
| 27 | i.group(
|
| 28 | IntProviders.POSITIVE_CODEC.fieldOf("extra_branch_steps").forGetter(p -> p.extraBranchSteps),
|
| 29 | Codec.floatRange(0.0F, 1.0F).fieldOf("place_branch_per_log_probability").forGetter(p -> p.placeBranchPerLogProbability),
|
| 30 | IntProviders.NON_NEGATIVE_CODEC.fieldOf("extra_branch_length").forGetter(c -> c.extraBranchLength),
|
| 31 | RegistryCodecs.holderSet(Registries.BLOCK).fieldOf("can_grow_through").forGetter(t -> t.canGrowThrough)
|
| 32 | )
|
| 33 | )
|
| 34 | .apply(i, UpwardsBranchingTrunkPlacer::new)
|
| 35 | );
|
| 36 | private final IntProvider extraBranchSteps;
|
| 37 | private final float placeBranchPerLogProbability;
|
| 38 | private final IntProvider extraBranchLength;
|
| 39 | private final HolderSet<Block> canGrowThrough;
|
| 40 |
|
| 41 | public UpwardsBranchingTrunkPlacer(
|
| 42 | final int baseHeight,
|
| 43 | final int heightRandA,
|
| 44 | final int heightRandB,
|
| 45 | final IntProvider extraBranchSteps,
|
| 46 | final float placeBranchPerLogProbability,
|
| 47 | final IntProvider extraBranchLength,
|
| 48 | final HolderSet<Block> canGrowThrough
|
| 49 | ) {
|
| 50 | super(baseHeight, heightRandA, heightRandB);
|
| 51 | this.extraBranchSteps = extraBranchSteps;
|
| 52 | this.placeBranchPerLogProbability = placeBranchPerLogProbability;
|
| 53 | this.extraBranchLength = extraBranchLength;
|
| 54 | this.canGrowThrough = canGrowThrough;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | protected TrunkPlacerType<?> type() {
|
| 59 | return TrunkPlacerType.UPWARDS_BRANCHING_TRUNK_PLACER;
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public List<FoliagePlacer.FoliageAttachment> placeTrunk(
|
| 64 | final WorldGenLevel level,
|
| 65 | final BiConsumer<BlockPos, BlockState> trunkSetter,
|
| 66 | final RandomSource random,
|
| 67 | final int treeHeight,
|
| 68 | final BlockPos origin,
|
| 69 | final TreeFeature tree
|
| 70 | ) {
|
| 71 | List<FoliagePlacer.FoliageAttachment> attachments = Lists.newArrayList();
|
| 72 | BlockPos.MutableBlockPos logPos = new BlockPos.MutableBlockPos();
|
| 73 |
|
| 74 | for (int heightPos = 0; heightPos < treeHeight; heightPos++) {
|
| 75 | int currentHeight = origin.getY() + heightPos;
|
| 76 | if (this.placeLog(level, trunkSetter, random, logPos.set(origin.getX(), currentHeight, origin.getZ()), tree)
|
| 77 | && heightPos < treeHeight - 1
|
| 78 | && random.nextFloat() < this.placeBranchPerLogProbability) {
|
| 79 | Direction branchDir = Direction.Plane.HORIZONTAL.getRandomDirection(random);
|
| 80 | int branchLen = this.extraBranchLength.sample(random);
|
| 81 | int branchPos = Math.max(0, branchLen - this.extraBranchLength.sample(random) - 1);
|
| 82 | int branchSteps = this.extraBranchSteps.sample(random);
|
| 83 | this.placeBranch(level, trunkSetter, random, treeHeight, tree, attachments, logPos, currentHeight, branchDir, branchPos, branchSteps);
|
| 84 | }
|
| 85 |
|
| 86 | if (heightPos == treeHeight - 1) {
|
| 87 | attachments.add(new FoliagePlacer.FoliageAttachment(logPos.set(origin.getX(), currentHeight + 1, origin.getZ()), 0, false));
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | return attachments;
|
| 92 | }
|
| 93 |
|
| 94 | private void placeBranch(
|
| 95 | final WorldGenLevel level,
|
| 96 | final BiConsumer<BlockPos, BlockState> trunkSetter,
|
| 97 | final RandomSource random,
|
| 98 | final int treeHeight,
|
| 99 | final TreeFeature tree,
|
| 100 | final List<FoliagePlacer.FoliageAttachment> attachments,
|
| 101 | final BlockPos.MutableBlockPos logPos,
|
| 102 | final int currentHeight,
|
| 103 | final Direction branchDir,
|
| 104 | final int branchPos,
|
| 105 | int branchSteps
|
| 106 | ) {
|
| 107 | int heightAlongBranch = currentHeight + branchPos;
|
| 108 | int logX = logPos.getX();
|
| 109 | int logZ = logPos.getZ();
|
| 110 | int branchPlacementIndex = branchPos;
|
| 111 |
|
| 112 | while (branchPlacementIndex < treeHeight && branchSteps > 0) {
|
| 113 | if (branchPlacementIndex >= 1) {
|
| 114 | int placementHeight = currentHeight + branchPlacementIndex;
|
| 115 | logX += branchDir.getStepX();
|
| 116 | logZ += branchDir.getStepZ();
|
| 117 | heightAlongBranch = placementHeight;
|
| 118 | if (this.placeLog(level, trunkSetter, random, logPos.set(logX, placementHeight, logZ), tree)) {
|
| 119 | heightAlongBranch++;
|
| 120 | }
|
| 121 |
|
| 122 | attachments.add(new FoliagePlacer.FoliageAttachment(logPos.immutable(), 0, false));
|
| 123 | }
|
| 124 |
|
| 125 | branchPlacementIndex++;
|
| 126 | branchSteps--;
|
| 127 | }
|
| 128 |
|
| 129 | if (heightAlongBranch - currentHeight > 1) {
|
| 130 | BlockPos foliagePos = new BlockPos(logX, heightAlongBranch, logZ);
|
| 131 | attachments.add(new FoliagePlacer.FoliageAttachment(foliagePos, 0, false));
|
| 132 | attachments.add(new FoliagePlacer.FoliageAttachment(foliagePos.below(2), 0, false));
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | protected boolean validTreePos(final WorldGenLevel level, final BlockPos pos) {
|
| 138 | return super.validTreePos(level, pos) || level.isStateAtPosition(pos, s -> s.is(this.canGrowThrough));
|
| 139 | }
|
| 140 | }
|
| 141 |
|