| 1 | package net.minecraft.world.level.levelgen.feature.foliageplacers;
|
| 2 |
|
| 3 | import com.mojang.datafixers.Products.P2;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder.Instance;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder.Mu;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.util.valueproviders.IntProvider;
|
| 12 | import net.minecraft.util.valueproviders.IntProviders;
|
| 13 | import net.minecraft.world.level.WorldGenLevel;
|
| 14 | import net.minecraft.world.level.block.state.BlockState;
|
| 15 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 16 | import net.minecraft.world.level.levelgen.feature.TreeFeature;
|
| 17 | import net.minecraft.world.level.material.Fluids;
|
| 18 |
|
| 19 | public abstract class FoliagePlacer {
|
| 20 | public static final Codec<FoliagePlacer> CODEC = BuiltInRegistries.FOLIAGE_PLACER_TYPE.byNameCodec().dispatch(FoliagePlacer::type, FoliagePlacerType::codec);
|
| 21 | protected final IntProvider radius;
|
| 22 | protected final IntProvider offset;
|
| 23 |
|
| 24 | protected static <P extends FoliagePlacer> P2<Mu<P>, IntProvider, IntProvider> foliagePlacerParts(final Instance<P> instance) {
|
| 25 | return instance.group(
|
| 26 | IntProviders.codec(0, 16).fieldOf("radius").forGetter(p -> p.radius), IntProviders.codec(0, 16).fieldOf("offset").forGetter(p -> p.offset)
|
| 27 | );
|
| 28 | }
|
| 29 |
|
| 30 | public FoliagePlacer(final IntProvider radius, final IntProvider offset) {
|
| 31 | this.radius = radius;
|
| 32 | this.offset = offset;
|
| 33 | }
|
| 34 |
|
| 35 | protected abstract FoliagePlacerType<?> type();
|
| 36 |
|
| 37 | public void createFoliage(
|
| 38 | final WorldGenLevel level,
|
| 39 | final FoliagePlacer.FoliageSetter foliageSetter,
|
| 40 | final RandomSource random,
|
| 41 | final TreeFeature tree,
|
| 42 | final int treeHeight,
|
| 43 | final FoliagePlacer.FoliageAttachment foliageAttachment,
|
| 44 | final int foliageHeight,
|
| 45 | final int leafRadius
|
| 46 | ) {
|
| 47 | this.createFoliage(level, foliageSetter, random, tree, treeHeight, foliageAttachment, foliageHeight, leafRadius, this.offset(random));
|
| 48 | }
|
| 49 |
|
| 50 | protected abstract void createFoliage(
|
| 51 | final WorldGenLevel level,
|
| 52 | final FoliagePlacer.FoliageSetter foliageSetter,
|
| 53 | final RandomSource random,
|
| 54 | final TreeFeature tree,
|
| 55 | final int treeHeight,
|
| 56 | final FoliagePlacer.FoliageAttachment foliageAttachment,
|
| 57 | final int foliageHeight,
|
| 58 | final int leafRadius,
|
| 59 | final int offset
|
| 60 | );
|
| 61 |
|
| 62 | public abstract int foliageHeight(final RandomSource random, final int treeHeight, final TreeFeature tree);
|
| 63 |
|
| 64 | public int foliageRadius(final RandomSource random, final int trunkHeight) {
|
| 65 | return this.radius.sample(random);
|
| 66 | }
|
| 67 |
|
| 68 | private int offset(final RandomSource random) {
|
| 69 | return this.offset.sample(random);
|
| 70 | }
|
| 71 |
|
| 72 | protected abstract boolean shouldSkipLocation(
|
| 73 | final RandomSource random, final int dx, final int y, final int dz, final int currentRadius, final boolean doubleTrunk
|
| 74 | );
|
| 75 |
|
| 76 | protected boolean shouldSkipLocationSigned(
|
| 77 | final RandomSource random, final int dx, final int y, final int dz, final int currentRadius, final boolean doubleTrunk
|
| 78 | ) {
|
| 79 | int minDx;
|
| 80 | int minDz;
|
| 81 | if (doubleTrunk) {
|
| 82 | minDx = Math.min(Math.abs(dx), Math.abs(dx - 1));
|
| 83 | minDz = Math.min(Math.abs(dz), Math.abs(dz - 1));
|
| 84 | } else {
|
| 85 | minDx = Math.abs(dx);
|
| 86 | minDz = Math.abs(dz);
|
| 87 | }
|
| 88 |
|
| 89 | return this.shouldSkipLocation(random, minDx, y, minDz, currentRadius, doubleTrunk);
|
| 90 | }
|
| 91 |
|
| 92 | protected void placeLeavesRow(
|
| 93 | final WorldGenLevel level,
|
| 94 | final FoliagePlacer.FoliageSetter foliageSetter,
|
| 95 | final RandomSource random,
|
| 96 | final TreeFeature tree,
|
| 97 | final BlockPos origin,
|
| 98 | final int currentRadius,
|
| 99 | final int y,
|
| 100 | final boolean doubleTrunk
|
| 101 | ) {
|
| 102 | int offset = doubleTrunk ? 1 : 0;
|
| 103 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 104 |
|
| 105 | for (int dx = -currentRadius; dx <= currentRadius + offset; dx++) {
|
| 106 | for (int dz = -currentRadius; dz <= currentRadius + offset; dz++) {
|
| 107 | if (!this.shouldSkipLocationSigned(random, dx, y, dz, currentRadius, doubleTrunk)) {
|
| 108 | pos.setWithOffset(origin, dx, y, dz);
|
| 109 | tryPlaceLeaf(level, foliageSetter, random, tree, pos);
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | protected final void placeLeavesRowWithHangingLeavesBelow(
|
| 116 | final WorldGenLevel level,
|
| 117 | final FoliagePlacer.FoliageSetter foliageSetter,
|
| 118 | final RandomSource random,
|
| 119 | final TreeFeature tree,
|
| 120 | final BlockPos origin,
|
| 121 | final int currentRadius,
|
| 122 | final int y,
|
| 123 | final boolean doubleTrunk,
|
| 124 | final float hangingLeavesChance,
|
| 125 | final float hangingLeavesExtensionChance
|
| 126 | ) {
|
| 127 | this.placeLeavesRow(level, foliageSetter, random, tree, origin, currentRadius, y, doubleTrunk);
|
| 128 | int offset = doubleTrunk ? 1 : 0;
|
| 129 | BlockPos logPos = origin.below();
|
| 130 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 131 |
|
| 132 | for (Direction alongEdge : Direction.Plane.HORIZONTAL) {
|
| 133 | Direction toEdge = alongEdge.getClockWise();
|
| 134 | int offsetToEdge = toEdge.getAxisDirection() == Direction.AxisDirection.POSITIVE ? currentRadius + offset : currentRadius;
|
| 135 | pos.setWithOffset(origin, 0, y - 1, 0).move(toEdge, offsetToEdge).move(alongEdge, -currentRadius);
|
| 136 | int offsetAlongEdge = -currentRadius;
|
| 137 |
|
| 138 | while (offsetAlongEdge < currentRadius + offset) {
|
| 139 | boolean leavesAbove = foliageSetter.isSet(pos.move(Direction.UP));
|
| 140 | pos.move(Direction.DOWN);
|
| 141 | if (leavesAbove && tryPlaceExtension(level, foliageSetter, random, tree, hangingLeavesChance, logPos, pos)) {
|
| 142 | pos.move(Direction.DOWN);
|
| 143 | tryPlaceExtension(level, foliageSetter, random, tree, hangingLeavesExtensionChance, logPos, pos);
|
| 144 | pos.move(Direction.UP);
|
| 145 | }
|
| 146 |
|
| 147 | offsetAlongEdge++;
|
| 148 | pos.move(alongEdge);
|
| 149 | }
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | private static boolean tryPlaceExtension(
|
| 154 | final WorldGenLevel level,
|
| 155 | final FoliagePlacer.FoliageSetter foliageSetter,
|
| 156 | final RandomSource random,
|
| 157 | final TreeFeature tree,
|
| 158 | final float chance,
|
| 159 | final BlockPos logPos,
|
| 160 | final BlockPos.MutableBlockPos pos
|
| 161 | ) {
|
| 162 | if (pos.distManhattan(logPos) >= 7) {
|
| 163 | return false;
|
| 164 | } else {
|
| 165 | return random.nextFloat() > chance ? false : tryPlaceLeaf(level, foliageSetter, random, tree, pos);
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | protected static boolean tryPlaceLeaf(
|
| 170 | final WorldGenLevel level, final FoliagePlacer.FoliageSetter foliageSetter, final RandomSource random, final TreeFeature tree, final BlockPos pos
|
| 171 | ) {
|
| 172 | boolean isPersistent = level.isStateAtPosition(pos, state -> state.getValueOrElse(BlockStateProperties.PERSISTENT, false));
|
| 173 | if (!isPersistent && TreeFeature.validTreePos(level, pos)) {
|
| 174 | BlockState foliageState = tree.foliageProvider().value().getState(level, random, pos);
|
| 175 | if (foliageState.hasProperty(BlockStateProperties.WATERLOGGED)) {
|
| 176 | foliageState = foliageState.setValue(BlockStateProperties.WATERLOGGED, level.isFluidAtPosition(pos, fluidState -> fluidState.isSourceOfType(Fluids.WATER)));
|
| 177 | }
|
| 178 |
|
| 179 | foliageSetter.set(pos, foliageState);
|
| 180 | return true;
|
| 181 | } else {
|
| 182 | return false;
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | public record FoliageAttachment(BlockPos pos, int radiusOffsetXZ, int foliageHeightOffset, int sizeX, int sizeZ) {
|
| 187 | public FoliageAttachment(final BlockPos pos, final int radiusOffsetXZ, final boolean doubleTrunk) {
|
| 188 | int sizeXZ = doubleTrunk ? 2 : 1;
|
| 189 | this(pos, radiusOffsetXZ, sizeXZ, sizeXZ);
|
| 190 | }
|
| 191 |
|
| 192 | public FoliageAttachment(final BlockPos pos, final int radiusOffsetXZ, final int sizeX, final int sizeZ) {
|
| 193 | this(pos, radiusOffsetXZ, 0, sizeX, sizeZ);
|
| 194 | }
|
| 195 |
|
| 196 | public boolean doubleTrunk() {
|
| 197 | return this.sizeX == 2 && this.sizeZ == 2;
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | public interface FoliageSetter {
|
| 202 | void set(final BlockPos pos, final BlockState state);
|
| 203 |
|
| 204 | boolean isSet(final BlockPos pos);
|
| 205 | }
|
| 206 | }
|
| 207 |
|