| 1 | package net.minecraft.world.level.levelgen.feature.rootplacers;
|
| 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.Optional;
|
| 8 | import java.util.function.BiConsumer;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.util.RandomSource;
|
| 13 | import net.minecraft.util.valueproviders.IntProvider;
|
| 14 | import net.minecraft.world.level.LevelSimulatedReader;
|
| 15 | import net.minecraft.world.level.WorldGenLevel;
|
| 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.stateproviders.BlockStateProvider;
|
| 19 |
|
| 20 | public class MangroveRootPlacer extends RootPlacer {
|
| 21 | public static final int ROOT_WIDTH_LIMIT = 8;
|
| 22 | public static final int ROOT_LENGTH_LIMIT = 15;
|
| 23 | public static final MapCodec<MangroveRootPlacer> CODEC = RecordCodecBuilder.mapCodec(
|
| 24 | i -> rootPlacerParts(i)
|
| 25 | .and(MangroveRootPlacement.CODEC.fieldOf("mangrove_root_placement").forGetter(c -> c.mangroveRootPlacement))
|
| 26 | .apply(i, MangroveRootPlacer::new)
|
| 27 | );
|
| 28 | private final MangroveRootPlacement mangroveRootPlacement;
|
| 29 |
|
| 30 | public MangroveRootPlacer(
|
| 31 | final IntProvider trunkOffsetY,
|
| 32 | final Holder<BlockStateProvider> rootProvider,
|
| 33 | final Optional<AboveRootPlacement> aboveRootPlacement,
|
| 34 | final MangroveRootPlacement mangroveRootPlacement
|
| 35 | ) {
|
| 36 | super(trunkOffsetY, rootProvider, aboveRootPlacement);
|
| 37 | this.mangroveRootPlacement = mangroveRootPlacement;
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public boolean placeRoots(
|
| 42 | final WorldGenLevel level,
|
| 43 | final BiConsumer<BlockPos, BlockState> rootSetter,
|
| 44 | final RandomSource random,
|
| 45 | final BlockPos origin,
|
| 46 | final BlockPos trunkOrigin,
|
| 47 | final TreeFeature tree
|
| 48 | ) {
|
| 49 | List<BlockPos> rootPositions = Lists.newArrayList();
|
| 50 | BlockPos.MutableBlockPos columnPos = origin.mutable();
|
| 51 |
|
| 52 | while (columnPos.getY() < trunkOrigin.getY()) {
|
| 53 | if (!this.canPlaceRoot(level, columnPos)) {
|
| 54 | return false;
|
| 55 | }
|
| 56 |
|
| 57 | columnPos.move(Direction.UP);
|
| 58 | }
|
| 59 |
|
| 60 | rootPositions.add(trunkOrigin.below());
|
| 61 |
|
| 62 | for (Direction dir : Direction.Plane.HORIZONTAL) {
|
| 63 | BlockPos pos = trunkOrigin.relative(dir);
|
| 64 | List<BlockPos> positionsInDirection = Lists.newArrayList();
|
| 65 | if (!this.simulateRoots(level, random, pos, dir, trunkOrigin, positionsInDirection, 0)) {
|
| 66 | return false;
|
| 67 | }
|
| 68 |
|
| 69 | rootPositions.addAll(positionsInDirection);
|
| 70 | rootPositions.add(trunkOrigin.relative(dir));
|
| 71 | }
|
| 72 |
|
| 73 | for (BlockPos rootPos : rootPositions) {
|
| 74 | this.placeRoot(level, rootSetter, random, rootPos, tree);
|
| 75 | }
|
| 76 |
|
| 77 | return true;
|
| 78 | }
|
| 79 |
|
| 80 | private boolean simulateRoots(
|
| 81 | final LevelSimulatedReader level,
|
| 82 | final RandomSource random,
|
| 83 | final BlockPos rootPos,
|
| 84 | final Direction dir,
|
| 85 | final BlockPos rootOrigin,
|
| 86 | final List<BlockPos> rootPositions,
|
| 87 | final int layer
|
| 88 | ) {
|
| 89 | int maxRootLength = this.mangroveRootPlacement.maxRootLength();
|
| 90 | if (layer != maxRootLength && rootPositions.size() <= maxRootLength) {
|
| 91 | for (BlockPos pos : this.potentialRootPositions(rootPos, dir, random, rootOrigin)) {
|
| 92 | if (this.canPlaceRoot(level, pos)) {
|
| 93 | rootPositions.add(pos);
|
| 94 | if (!this.simulateRoots(level, random, pos, dir, rootOrigin, rootPositions, layer + 1)) {
|
| 95 | return false;
|
| 96 | }
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | return true;
|
| 101 | } else {
|
| 102 | return false;
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | protected List<BlockPos> potentialRootPositions(final BlockPos pos, final Direction prevDir, final RandomSource random, final BlockPos rootOrigin) {
|
| 107 | BlockPos below = pos.below();
|
| 108 | BlockPos nextTo = pos.relative(prevDir);
|
| 109 | int width = pos.distManhattan(rootOrigin);
|
| 110 | int maxRootWidth = this.mangroveRootPlacement.maxRootWidth();
|
| 111 | float randomSkewChance = this.mangroveRootPlacement.randomSkewChance();
|
| 112 | if (width > maxRootWidth - 3 && width <= maxRootWidth) {
|
| 113 | return random.nextFloat() < randomSkewChance ? List.of(below, nextTo.below()) : List.of(below);
|
| 114 | } else if (width > maxRootWidth) {
|
| 115 | return List.of(below);
|
| 116 | } else if (random.nextFloat() < randomSkewChance) {
|
| 117 | return List.of(below);
|
| 118 | } else {
|
| 119 | return random.nextBoolean() ? List.of(nextTo) : List.of(below);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | protected boolean canPlaceRoot(final LevelSimulatedReader level, final BlockPos pos) {
|
| 125 | return super.canPlaceRoot(level, pos) || level.isStateAtPosition(pos, state -> state.is(this.mangroveRootPlacement.canGrowThrough()));
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | protected void placeRoot(
|
| 130 | final WorldGenLevel level, final BiConsumer<BlockPos, BlockState> rootSetter, final RandomSource random, final BlockPos pos, final TreeFeature tree
|
| 131 | ) {
|
| 132 | if (level.isStateAtPosition(pos, s -> s.is(this.mangroveRootPlacement.muddyRootsIn()))) {
|
| 133 | BlockState muddyRoots = this.mangroveRootPlacement.muddyRootsProvider().value().getState(level, random, pos);
|
| 134 | rootSetter.accept(pos, this.getPotentiallyWaterloggedState(level, pos, muddyRoots));
|
| 135 | } else {
|
| 136 | super.placeRoot(level, rootSetter, random, pos, tree);
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | protected RootPlacerType<?> type() {
|
| 142 | return RootPlacerType.MANGROVE_ROOT_PLACER;
|
| 143 | }
|
| 144 | }
|
| 145 |
|