| 1 | package net.minecraft.world.level.levelgen.feature.trunkplacers;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.BiConsumer;
|
| 7 | import java.util.function.Function;
|
| 8 | import java.util.stream.Collectors;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 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.RotatedPillarBlock;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.levelgen.feature.TreeFeature;
|
| 18 | import net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacer;
|
| 19 |
|
| 20 | public class PoplarTrunkPlacer extends TrunkPlacer {
|
| 21 | public static final MapCodec<PoplarTrunkPlacer> CODEC = RecordCodecBuilder.mapCodec(
|
| 22 | i -> trunkPlacerParts(i)
|
| 23 | .and(
|
| 24 | i.group(
|
| 25 | IntProviders.codec(0, 8).fieldOf("trunk_height_above_branches").forGetter(t -> t.trunkHeightAboveBranches),
|
| 26 | IntProviders.codec(1, 4).fieldOf("branch_amount").forGetter(t -> t.branchAmount)
|
| 27 | )
|
| 28 | )
|
| 29 | .apply(i, PoplarTrunkPlacer::new)
|
| 30 | );
|
| 31 | private final IntProvider trunkHeightAboveBranches;
|
| 32 | private final IntProvider branchAmount;
|
| 33 |
|
| 34 | public PoplarTrunkPlacer(
|
| 35 | final int baseHeight, final int heightRandA, final int heightRandB, final IntProvider trunkHeightAboveBranches, final IntProvider branchAmount
|
| 36 | ) {
|
| 37 | super(baseHeight, heightRandA, heightRandB);
|
| 38 | this.trunkHeightAboveBranches = trunkHeightAboveBranches;
|
| 39 | this.branchAmount = branchAmount;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | protected TrunkPlacerType<?> type() {
|
| 44 | return TrunkPlacerType.POPLAR_TRUNK_PLACER;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public List<FoliagePlacer.FoliageAttachment> placeTrunk(
|
| 49 | final WorldGenLevel level,
|
| 50 | final BiConsumer<BlockPos, BlockState> trunkSetter,
|
| 51 | final RandomSource random,
|
| 52 | final int treeHeight,
|
| 53 | final BlockPos origin,
|
| 54 | final TreeFeature tree
|
| 55 | ) {
|
| 56 | placeBelowTrunkBlock(level, trunkSetter, random, origin.below(), tree);
|
| 57 | int trunkHeightUpToFoliageBranches = treeHeight - this.trunkHeightAboveBranches.sample(random);
|
| 58 |
|
| 59 | for (int y = 0; y < treeHeight; y++) {
|
| 60 | this.placeLog(level, trunkSetter, random, origin.above(y), tree);
|
| 61 | List<Direction> directions = getShuffledBranchDirections(random);
|
| 62 | if (trunkHeightUpToFoliageBranches - 1 == y) {
|
| 63 | int branches = this.branchAmount.sample(random);
|
| 64 |
|
| 65 | for (int x = 0; x < branches; x++) {
|
| 66 | Direction branchDirection = directions.get(x);
|
| 67 | this.placeLog(level, trunkSetter, random, origin.above(y).relative(branchDirection, 1), tree, getSidewaysStateModifier(branchDirection));
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | return List.of(new FoliagePlacer.FoliageAttachment(origin.above(trunkHeightUpToFoliageBranches), 0, false));
|
| 73 | }
|
| 74 |
|
| 75 | private static Function<BlockState, BlockState> getSidewaysStateModifier(final Direction branchDirection) {
|
| 76 | return state -> state.trySetValue(RotatedPillarBlock.AXIS, branchDirection.getAxis());
|
| 77 | }
|
| 78 |
|
| 79 | private static List<Direction> getShuffledBranchDirections(final RandomSource random) {
|
| 80 | return Direction.allShuffled(random).stream().filter(direction -> !direction.getAxis().isVertical()).collect(Collectors.toList());
|
| 81 | }
|
| 82 | }
|
| 83 |
|