| 1 | package net.minecraft.world.level.levelgen.feature.foliageplacers;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.function.Function;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.util.Mth;
|
| 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.RotatedPillarBlock;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.levelgen.feature.TreeFeature;
|
| 17 |
|
| 18 | public class PoplarFoliagePlacer extends FoliagePlacer {
|
| 19 | public static final MapCodec<PoplarFoliagePlacer> CODEC = RecordCodecBuilder.mapCodec(
|
| 20 | i -> foliagePlacerParts(i)
|
| 21 | .and(
|
| 22 | i.group(
|
| 23 | IntProviders.codec(5, 16).fieldOf("height").forGetter(p -> p.height),
|
| 24 | Codec.floatRange(0.0F, 1.0F).fieldOf("side_hole_chance").forGetter(p -> p.sideHoleChance)
|
| 25 | )
|
| 26 | )
|
| 27 | .apply(i, PoplarFoliagePlacer::new)
|
| 28 | );
|
| 29 | private final IntProvider height;
|
| 30 | private final float sideHoleChance;
|
| 31 |
|
| 32 | public PoplarFoliagePlacer(final IntProvider radius, final IntProvider offset, final IntProvider height, final float sideHoleChance) {
|
| 33 | super(radius, offset);
|
| 34 | this.height = height;
|
| 35 | this.sideHoleChance = sideHoleChance;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | protected FoliagePlacerType<?> type() {
|
| 40 | return FoliagePlacerType.POPLAR_FOLIAGE_PLACER;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected void createFoliage(
|
| 45 | final WorldGenLevel level,
|
| 46 | final FoliagePlacer.FoliageSetter foliageSetter,
|
| 47 | final RandomSource random,
|
| 48 | final TreeFeature tree,
|
| 49 | final int treeHeight,
|
| 50 | final FoliagePlacer.FoliageAttachment foliageAttachment,
|
| 51 | final int foliageHeight,
|
| 52 | final int leafRadius,
|
| 53 | final int offset
|
| 54 | ) {
|
| 55 | boolean doubleTrunk = foliageAttachment.doubleTrunk();
|
| 56 | BlockPos foliagePos = foliageAttachment.pos().above(offset);
|
| 57 | int currentRadius = leafRadius + foliageAttachment.radiusOffsetXZ() - 1;
|
| 58 | boolean flipRhombusShape = random.nextBoolean();
|
| 59 | int foliageHeightWithOffset = foliageHeight + foliageAttachment.foliageHeightOffset();
|
| 60 | this.placeLeavesRow(
|
| 61 | level, foliageSetter, random, tree, foliagePos, currentRadius - 2, foliageHeightWithOffset - 1, doubleTrunk, foliageHeightWithOffset, flipRhombusShape
|
| 62 | );
|
| 63 | this.placeLeavesRow(
|
| 64 | level, foliageSetter, random, tree, foliagePos, currentRadius - 1, foliageHeightWithOffset - 2, doubleTrunk, foliageHeightWithOffset, flipRhombusShape
|
| 65 | );
|
| 66 | this.placeLeavesRow(
|
| 67 | level, foliageSetter, random, tree, foliagePos, currentRadius - 1, foliageHeightWithOffset - 3, doubleTrunk, foliageHeightWithOffset, flipRhombusShape
|
| 68 | );
|
| 69 |
|
| 70 | for (int y = foliageHeightWithOffset - 4; y >= 1; y--) {
|
| 71 | this.placeLeavesRow(level, foliageSetter, random, tree, foliagePos, currentRadius, y, doubleTrunk, foliageHeightWithOffset, flipRhombusShape);
|
| 72 | }
|
| 73 |
|
| 74 | this.replaceLeavesWithLog(
|
| 75 | level, foliageSetter, tree, random, foliagePos, currentRadius, foliageHeightWithOffset - 4, doubleTrunk, foliageHeightWithOffset, flipRhombusShape
|
| 76 | );
|
| 77 | this.placeLeavesRow(level, foliageSetter, random, tree, foliagePos, currentRadius - 1, 0, doubleTrunk, foliageHeightWithOffset, flipRhombusShape);
|
| 78 | this.placeLeavesRow(
|
| 79 | level, foliageSetter, random, tree, foliagePos, Mth.clamp(currentRadius - 2, 1, 2), -1, doubleTrunk, foliageHeightWithOffset, flipRhombusShape
|
| 80 | );
|
| 81 | }
|
| 82 |
|
| 83 | private void replaceLeavesWithLog(
|
| 84 | final WorldGenLevel level,
|
| 85 | final FoliagePlacer.FoliageSetter foliageSetter,
|
| 86 | final TreeFeature tree,
|
| 87 | final RandomSource random,
|
| 88 | final BlockPos origin,
|
| 89 | final int currentRadius,
|
| 90 | final int y,
|
| 91 | final boolean doubleTrunk,
|
| 92 | final int foliageHeight,
|
| 93 | final boolean flipRhombusShape
|
| 94 | ) {
|
| 95 | int offset = doubleTrunk ? 1 : 0;
|
| 96 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 97 |
|
| 98 | for (int dx = -currentRadius; dx <= currentRadius + offset; dx++) {
|
| 99 | for (int dz = -currentRadius; dz <= currentRadius + offset; dz++) {
|
| 100 | int absDz = Mth.abs(dz);
|
| 101 | int absDx = Mth.abs(dx);
|
| 102 | if (isWithinRhombusShape(
|
| 103 | currentRadius,
|
| 104 | absDx,
|
| 105 | absDz,
|
| 106 | this.getCornerBlocksToCutForRhombusShape(dx, dz, currentRadius, this.shouldRowBePartialRhombusShape(foliageHeight, y), flipRhombusShape),
|
| 107 | 2
|
| 108 | )
|
| 109 | && (absDz == 0 && currentRadius - absDx >= 4 || absDx == 0 && currentRadius - absDz >= 4)) {
|
| 110 | pos.setWithOffset(origin, dx, y, dz);
|
| 111 | tryPlaceLog(
|
| 112 | level,
|
| 113 | foliageSetter,
|
| 114 | random,
|
| 115 | tree,
|
| 116 | pos,
|
| 117 | getSidewaysStateModifier(Direction.fromAxisAndDirection(absDz == 0 ? Direction.Axis.X : Direction.Axis.Z, Direction.AxisDirection.POSITIVE))
|
| 118 | );
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | private static void tryPlaceLog(
|
| 125 | final WorldGenLevel level,
|
| 126 | final FoliagePlacer.FoliageSetter foliageSetter,
|
| 127 | final RandomSource random,
|
| 128 | final TreeFeature tree,
|
| 129 | final BlockPos pos,
|
| 130 | final Function<BlockState, BlockState> stateModifier
|
| 131 | ) {
|
| 132 | if (level.isStateAtPosition(pos, state -> state.equals(tree.foliageProvider().value().getState(level, random, pos)))) {
|
| 133 | foliageSetter.set(pos, stateModifier.apply(tree.trunkProvider().value().getState(level, random, pos)));
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | private static Function<BlockState, BlockState> getSidewaysStateModifier(final Direction branchDirection) {
|
| 138 | return state -> state.trySetValue(RotatedPillarBlock.AXIS, branchDirection.getAxis());
|
| 139 | }
|
| 140 |
|
| 141 | @Override
|
| 142 | public int foliageHeight(final RandomSource random, final int treeHeight, final TreeFeature tree) {
|
| 143 | return this.height.sample(random);
|
| 144 | }
|
| 145 |
|
| 146 | private void placeLeavesRow(
|
| 147 | final WorldGenLevel level,
|
| 148 | final FoliagePlacer.FoliageSetter foliageSetter,
|
| 149 | final RandomSource random,
|
| 150 | final TreeFeature tree,
|
| 151 | final BlockPos origin,
|
| 152 | final int currentRadius,
|
| 153 | final int y,
|
| 154 | final boolean doubleTrunk,
|
| 155 | final int foliageHeight,
|
| 156 | final boolean flipRhombusShape
|
| 157 | ) {
|
| 158 | int offset = doubleTrunk ? 1 : 0;
|
| 159 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 160 |
|
| 161 | for (int dx = -currentRadius; dx <= currentRadius + offset; dx++) {
|
| 162 | for (int dz = -currentRadius; dz <= currentRadius + offset; dz++) {
|
| 163 | if (!this.shouldSkipLocation(random, dx, y, dz, currentRadius, doubleTrunk, foliageHeight, flipRhombusShape)) {
|
| 164 | pos.setWithOffset(origin, dx, y, dz);
|
| 165 | tryPlaceLeaf(level, foliageSetter, random, tree, pos);
|
| 166 | }
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | private boolean shouldSkipLocation(
|
| 172 | final RandomSource random,
|
| 173 | final int dx,
|
| 174 | final int y,
|
| 175 | final int dz,
|
| 176 | final int currentRadius,
|
| 177 | final boolean doubleTrunk,
|
| 178 | final int foliageHeight,
|
| 179 | final boolean flipRhombusShape
|
| 180 | ) {
|
| 181 | boolean shouldRowBePartialRhombusShape = this.shouldRowBePartialRhombusShape(foliageHeight, y);
|
| 182 | int cornerBlocksToCutForRhombusShape = this.getCornerBlocksToCutForRhombusShape(dx, dz, currentRadius, shouldRowBePartialRhombusShape, flipRhombusShape);
|
| 183 | int absDx = Mth.abs(dx);
|
| 184 | int absDz = Mth.abs(dz);
|
| 185 | boolean isRhombusEdgeBlock = absDx == currentRadius || absDz == currentRadius;
|
| 186 | if (shouldRowBePartialRhombusShape && isRhombusEdgeBlock) {
|
| 187 | return true;
|
| 188 | }
|
| 189 |
|
| 190 | int additionalSideRemoval = random.nextFloat() <= this.sideHoleChance ? 1 : 0;
|
| 191 | return !isWithinRhombusShape(currentRadius, absDx, absDz, cornerBlocksToCutForRhombusShape, additionalSideRemoval);
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | protected boolean shouldSkipLocationSigned(
|
| 196 | final RandomSource random, final int dx, final int y, final int dz, final int currentRadius, final boolean doubleTrunk
|
| 197 | ) {
|
| 198 | throw new IllegalStateException("Overridden method needs more context");
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | protected boolean shouldSkipLocation(final RandomSource random, final int dx, final int y, final int dz, final int currentRadius, final boolean doubleTrunk) {
|
| 203 | throw new IllegalStateException("Overridden method needs more context");
|
| 204 | }
|
| 205 |
|
| 206 | private int getCornerBlocksToCutForRhombusShape(
|
| 207 | final int dx, final int dz, final int currentRadius, final boolean shouldRowBePartialRhombusShape, final boolean flipRhombusShape
|
| 208 | ) {
|
| 209 | boolean isSmallCornerOfShape = flipRhombusShape ? isLeftTopCornerOrRightLowerCorner(dx, dz) : isLeftLowerCornerOrRightTopCorner(dx, dz);
|
| 210 | return isSmallCornerOfShape ? currentRadius - 1 : (shouldRowBePartialRhombusShape ? currentRadius + 1 : currentRadius);
|
| 211 | }
|
| 212 |
|
| 213 | private static boolean isWithinRhombusShape(
|
| 214 | final int currentRadius, final int absDx, final int absDz, final int cornerBlocksToCutForRhombusShape, final int additionalSideRemoval
|
| 215 | ) {
|
| 216 | return absDx + absDz <= currentRadius * 2 - (cornerBlocksToCutForRhombusShape + additionalSideRemoval);
|
| 217 | }
|
| 218 |
|
| 219 | private static boolean isLeftLowerCornerOrRightTopCorner(final int dx, final int dz) {
|
| 220 | return dx > 0 && dz < 0 || dz > 0 && dx < 0;
|
| 221 | }
|
| 222 |
|
| 223 | private static boolean isLeftTopCornerOrRightLowerCorner(final int dx, final int dz) {
|
| 224 | return dx > 0 && dz > 0 || dz < 0 && dx < 0;
|
| 225 | }
|
| 226 |
|
| 227 | private boolean shouldRowBePartialRhombusShape(final int foliageHeight, final int y) {
|
| 228 | return foliageHeight - 1 == y || foliageHeight - 2 == y;
|
| 229 | }
|
| 230 | }
|
| 231 |
|