| 1 | package net.minecraft.world.level.levelgen.feature.trunkplacers;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.ArrayList;
|
| 8 | import java.util.List;
|
| 9 | import java.util.function.BiConsumer;
|
| 10 | import java.util.function.Function;
|
| 11 | import net.minecraft.core.BlockPos;
|
| 12 | import net.minecraft.core.Direction;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.util.valueproviders.IntProvider;
|
| 15 | import net.minecraft.util.valueproviders.IntProviders;
|
| 16 | import net.minecraft.util.valueproviders.UniformInt;
|
| 17 | import net.minecraft.world.level.WorldGenLevel;
|
| 18 | import net.minecraft.world.level.block.RotatedPillarBlock;
|
| 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 CherryTrunkPlacer extends TrunkPlacer {
|
| 24 | private static final Codec<UniformInt> BRANCH_START_CODEC = UniformInt.MAP_CODEC
|
| 25 | .codec()
|
| 26 | .validate(
|
| 27 | u -> u.maxInclusive() - u.minInclusive() < 1
|
| 28 | ? DataResult.error(() -> "Need at least 2 blocks variation for the branch starts to fit both branches")
|
| 29 | : DataResult.success(u)
|
| 30 | );
|
| 31 | public static final MapCodec<CherryTrunkPlacer> CODEC = RecordCodecBuilder.mapCodec(
|
| 32 | i -> trunkPlacerParts(i)
|
| 33 | .and(
|
| 34 | i.group(
|
| 35 | IntProviders.codec(1, 3).fieldOf("branch_count").forGetter(t -> t.branchCount),
|
| 36 | IntProviders.codec(2, 16).fieldOf("branch_horizontal_length").forGetter(t -> t.branchHorizontalLength),
|
| 37 | IntProviders.validateCodec(-16, 0, BRANCH_START_CODEC).fieldOf("branch_start_offset_from_top").forGetter(t -> t.branchStartOffsetFromTop),
|
| 38 | IntProviders.codec(-16, 16).fieldOf("branch_end_offset_from_top").forGetter(t -> t.branchEndOffsetFromTop)
|
| 39 | )
|
| 40 | )
|
| 41 | .apply(i, CherryTrunkPlacer::new)
|
| 42 | );
|
| 43 | private final IntProvider branchCount;
|
| 44 | private final IntProvider branchHorizontalLength;
|
| 45 | private final UniformInt branchStartOffsetFromTop;
|
| 46 | private final UniformInt secondBranchStartOffsetFromTop;
|
| 47 | private final IntProvider branchEndOffsetFromTop;
|
| 48 |
|
| 49 | public CherryTrunkPlacer(
|
| 50 | final int baseHeight,
|
| 51 | final int heightRandA,
|
| 52 | final int heightRandB,
|
| 53 | final IntProvider branchCount,
|
| 54 | final IntProvider branchHorizontalLength,
|
| 55 | final UniformInt branchStartOffsetFromTop,
|
| 56 | final IntProvider branchEndOffsetFromTop
|
| 57 | ) {
|
| 58 | super(baseHeight, heightRandA, heightRandB);
|
| 59 | this.branchCount = branchCount;
|
| 60 | this.branchHorizontalLength = branchHorizontalLength;
|
| 61 | this.branchStartOffsetFromTop = branchStartOffsetFromTop;
|
| 62 | this.secondBranchStartOffsetFromTop = UniformInt.of(branchStartOffsetFromTop.minInclusive(), branchStartOffsetFromTop.maxInclusive() - 1);
|
| 63 | this.branchEndOffsetFromTop = branchEndOffsetFromTop;
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected TrunkPlacerType<?> type() {
|
| 68 | return TrunkPlacerType.CHERRY_TRUNK_PLACER;
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public List<FoliagePlacer.FoliageAttachment> placeTrunk(
|
| 73 | final WorldGenLevel level,
|
| 74 | final BiConsumer<BlockPos, BlockState> trunkSetter,
|
| 75 | final RandomSource random,
|
| 76 | final int treeHeight,
|
| 77 | final BlockPos origin,
|
| 78 | final TreeFeature tree
|
| 79 | ) {
|
| 80 | placeBelowTrunkBlock(level, trunkSetter, random, origin.below(), tree);
|
| 81 | int firstBranchOffsetFromOrigin = Math.max(0, treeHeight - 1 + this.branchStartOffsetFromTop.sample(random));
|
| 82 | int secondBranchOffsetFromOrigin = Math.max(0, treeHeight - 1 + this.secondBranchStartOffsetFromTop.sample(random));
|
| 83 | if (secondBranchOffsetFromOrigin >= firstBranchOffsetFromOrigin) {
|
| 84 | secondBranchOffsetFromOrigin++;
|
| 85 | }
|
| 86 |
|
| 87 | int branchCount = this.branchCount.sample(random);
|
| 88 | boolean hasMiddleBranch = branchCount == 3;
|
| 89 | boolean hasBothSideBranches = branchCount >= 2;
|
| 90 | int trunkHeight;
|
| 91 | if (hasMiddleBranch) {
|
| 92 | trunkHeight = treeHeight;
|
| 93 | } else if (hasBothSideBranches) {
|
| 94 | trunkHeight = Math.max(firstBranchOffsetFromOrigin, secondBranchOffsetFromOrigin) + 1;
|
| 95 | } else {
|
| 96 | trunkHeight = firstBranchOffsetFromOrigin + 1;
|
| 97 | }
|
| 98 |
|
| 99 | for (int y = 0; y < trunkHeight; y++) {
|
| 100 | this.placeLog(level, trunkSetter, random, origin.above(y), tree);
|
| 101 | }
|
| 102 |
|
| 103 | List<FoliagePlacer.FoliageAttachment> attachments = new ArrayList<>();
|
| 104 | if (hasMiddleBranch) {
|
| 105 | attachments.add(new FoliagePlacer.FoliageAttachment(origin.above(trunkHeight), 0, false));
|
| 106 | }
|
| 107 |
|
| 108 | BlockPos.MutableBlockPos logPos = new BlockPos.MutableBlockPos();
|
| 109 | Direction treeDirection = Direction.Plane.HORIZONTAL.getRandomDirection(random);
|
| 110 | Function<BlockState, BlockState> sidewaysStateModifier = state -> state.trySetValue(RotatedPillarBlock.AXIS, treeDirection.getAxis());
|
| 111 | attachments.add(
|
| 112 | this.generateBranch(
|
| 113 | level,
|
| 114 | trunkSetter,
|
| 115 | random,
|
| 116 | treeHeight,
|
| 117 | origin,
|
| 118 | tree,
|
| 119 | sidewaysStateModifier,
|
| 120 | treeDirection,
|
| 121 | firstBranchOffsetFromOrigin,
|
| 122 | firstBranchOffsetFromOrigin < trunkHeight - 1,
|
| 123 | logPos
|
| 124 | )
|
| 125 | );
|
| 126 | if (hasBothSideBranches) {
|
| 127 | attachments.add(
|
| 128 | this.generateBranch(
|
| 129 | level,
|
| 130 | trunkSetter,
|
| 131 | random,
|
| 132 | treeHeight,
|
| 133 | origin,
|
| 134 | tree,
|
| 135 | sidewaysStateModifier,
|
| 136 | treeDirection.getOpposite(),
|
| 137 | secondBranchOffsetFromOrigin,
|
| 138 | secondBranchOffsetFromOrigin < trunkHeight - 1,
|
| 139 | logPos
|
| 140 | )
|
| 141 | );
|
| 142 | }
|
| 143 |
|
| 144 | return attachments;
|
| 145 | }
|
| 146 |
|
| 147 | private FoliagePlacer.FoliageAttachment generateBranch(
|
| 148 | final WorldGenLevel level,
|
| 149 | final BiConsumer<BlockPos, BlockState> trunkSetter,
|
| 150 | final RandomSource random,
|
| 151 | final int treeHeight,
|
| 152 | final BlockPos origin,
|
| 153 | final TreeFeature tree,
|
| 154 | final Function<BlockState, BlockState> sidewaysStateModifier,
|
| 155 | final Direction branchDirection,
|
| 156 | final int offsetFromOrigin,
|
| 157 | final boolean middleContinuesUpwards,
|
| 158 | final BlockPos.MutableBlockPos logPos
|
| 159 | ) {
|
| 160 | logPos.set(origin).move(Direction.UP, offsetFromOrigin);
|
| 161 | int branchEndPosOffsetFromOrigin = treeHeight - 1 + this.branchEndOffsetFromTop.sample(random);
|
| 162 | boolean extendBranchAwayFromTrunk = middleContinuesUpwards || branchEndPosOffsetFromOrigin < offsetFromOrigin;
|
| 163 | int distanceToTrunk = this.branchHorizontalLength.sample(random) + (extendBranchAwayFromTrunk ? 1 : 0);
|
| 164 | BlockPos branchEndPos = origin.relative(branchDirection, distanceToTrunk).above(branchEndPosOffsetFromOrigin);
|
| 165 | int stepsHorizontally = extendBranchAwayFromTrunk ? 2 : 1;
|
| 166 |
|
| 167 | for (int i = 0; i < stepsHorizontally; i++) {
|
| 168 | this.placeLog(level, trunkSetter, random, logPos.move(branchDirection), tree, sidewaysStateModifier);
|
| 169 | }
|
| 170 |
|
| 171 | Direction verticalDirection = branchEndPos.getY() > logPos.getY() ? Direction.UP : Direction.DOWN;
|
| 172 |
|
| 173 | while (true) {
|
| 174 | int distance = logPos.distManhattan(branchEndPos);
|
| 175 | if (distance == 0) {
|
| 176 | return new FoliagePlacer.FoliageAttachment(branchEndPos.above(), 0, false);
|
| 177 | }
|
| 178 |
|
| 179 | float chanceToGrowVertically = (float)Math.abs(branchEndPos.getY() - logPos.getY()) / distance;
|
| 180 | boolean growVertically = random.nextFloat() < chanceToGrowVertically;
|
| 181 | logPos.move(growVertically ? verticalDirection : branchDirection);
|
| 182 | this.placeLog(level, trunkSetter, random, logPos, tree, growVertically ? Function.identity() : sidewaysStateModifier);
|
| 183 | }
|
| 184 | }
|
| 185 | }
|
| 186 |
|