| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.HashSet;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Set;
|
| 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.core.Holder;
|
| 14 | import net.minecraft.util.RandomSource;
|
| 15 | import net.minecraft.util.valueproviders.IntProvider;
|
| 16 | import net.minecraft.util.valueproviders.IntProviders;
|
| 17 | import net.minecraft.world.level.LevelAccessor;
|
| 18 | import net.minecraft.world.level.WorldGenLevel;
|
| 19 | import net.minecraft.world.level.block.RotatedPillarBlock;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 22 | import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
| 23 | import net.minecraft.world.level.levelgen.feature.treedecorators.TreeDecorator;
|
| 24 |
|
| 25 | public record FallenTreeFeature(
|
| 26 | Holder<BlockStateProvider> trunkProvider, IntProvider logLength, List<TreeDecorator> stumpDecorators, List<TreeDecorator> logDecorators
|
| 27 | ) implements Feature {
|
| 28 | private static final int STUMP_HEIGHT = 1;
|
| 29 | private static final int STUMP_HEIGHT_PLUS_EMPTY_SPACE = 2;
|
| 30 | private static final int FALLEN_LOG_MAX_FALL_HEIGHT_TO_GROUND = 5;
|
| 31 | private static final int FALLEN_LOG_MAX_GROUND_GAP = 2;
|
| 32 | private static final int FALLEN_LOG_MAX_SPACE_FROM_STUMP = 2;
|
| 33 | public static final MapCodec<FallenTreeFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 34 | i -> i.group(
|
| 35 | BlockStateProvider.CODEC.fieldOf("trunk_provider").forGetter(FallenTreeFeature::trunkProvider),
|
| 36 | IntProviders.codec(0, 16).fieldOf("log_length").forGetter(FallenTreeFeature::logLength),
|
| 37 | TreeDecorator.CODEC.listOf().fieldOf("stump_decorators").forGetter(FallenTreeFeature::stumpDecorators),
|
| 38 | TreeDecorator.CODEC.listOf().fieldOf("log_decorators").forGetter(FallenTreeFeature::logDecorators)
|
| 39 | )
|
| 40 | .apply(i, FallenTreeFeature::new)
|
| 41 | );
|
| 42 |
|
| 43 | @Override
|
| 44 | public MapCodec<FallenTreeFeature> codec() {
|
| 45 | return CODEC;
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 50 | this.placeFallenTree(origin, level, random);
|
| 51 | return true;
|
| 52 | }
|
| 53 |
|
| 54 | private void placeFallenTree(final BlockPos origin, final WorldGenLevel level, final RandomSource random) {
|
| 55 | this.placeStump(level, random, origin.mutable());
|
| 56 | Direction direction = Direction.Plane.HORIZONTAL.getRandomDirection(random);
|
| 57 | int logLength = this.logLength.sample(random) - 2;
|
| 58 | BlockPos.MutableBlockPos logStartPos = origin.relative(direction, 2 + random.nextInt(2)).mutable();
|
| 59 | this.setGroundHeightForFallenLogStartPos(level, logStartPos);
|
| 60 | if (this.canPlaceEntireFallenLog(level, logLength, logStartPos, direction)) {
|
| 61 | this.placeFallenLog(level, random, logLength, logStartPos, direction);
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | private void setGroundHeightForFallenLogStartPos(final WorldGenLevel level, final BlockPos.MutableBlockPos logStartPos) {
|
| 66 | logStartPos.move(Direction.UP, 1);
|
| 67 |
|
| 68 | for (int i = 0; i < 6; i++) {
|
| 69 | if (this.mayPlaceOn(level, logStartPos)) {
|
| 70 | return;
|
| 71 | }
|
| 72 |
|
| 73 | logStartPos.move(Direction.DOWN);
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | private void placeStump(final WorldGenLevel level, final RandomSource random, final BlockPos.MutableBlockPos stumpPos) {
|
| 78 | BlockPos stump = this.placeLogBlock(level, random, stumpPos, Function.identity());
|
| 79 | this.decorateLogs(level, random, Set.of(stump), this.stumpDecorators);
|
| 80 | }
|
| 81 |
|
| 82 | private boolean canPlaceEntireFallenLog(final WorldGenLevel level, final int logLength, final BlockPos.MutableBlockPos logStartPos, final Direction direction) {
|
| 83 | int gapInGround = 0;
|
| 84 |
|
| 85 | for (int i = 0; i < logLength; i++) {
|
| 86 | if (!TreeFeature.validTreePos(level, logStartPos)) {
|
| 87 | return false;
|
| 88 | }
|
| 89 |
|
| 90 | if (!this.isOverSolidGround(level, logStartPos)) {
|
| 91 | if (++gapInGround > 2) {
|
| 92 | return false;
|
| 93 | }
|
| 94 | } else {
|
| 95 | gapInGround = 0;
|
| 96 | }
|
| 97 |
|
| 98 | logStartPos.move(direction);
|
| 99 | }
|
| 100 |
|
| 101 | logStartPos.move(direction.getOpposite(), logLength);
|
| 102 | return true;
|
| 103 | }
|
| 104 |
|
| 105 | private void placeFallenLog(
|
| 106 | final WorldGenLevel level, final RandomSource random, final int logLength, final BlockPos.MutableBlockPos logStartPos, final Direction direction
|
| 107 | ) {
|
| 108 | Set<BlockPos> fallenLog = new HashSet<>();
|
| 109 |
|
| 110 | for (int i = 0; i < logLength; i++) {
|
| 111 | fallenLog.add(this.placeLogBlock(level, random, logStartPos, getSidewaysStateModifier(direction)));
|
| 112 | logStartPos.move(direction);
|
| 113 | }
|
| 114 |
|
| 115 | this.decorateLogs(level, random, fallenLog, this.logDecorators);
|
| 116 | }
|
| 117 |
|
| 118 | private boolean mayPlaceOn(final LevelAccessor level, final BlockPos blockPos) {
|
| 119 | return TreeFeature.validTreePos(level, blockPos) && this.isOverSolidGround(level, blockPos);
|
| 120 | }
|
| 121 |
|
| 122 | private boolean isOverSolidGround(final LevelAccessor level, final BlockPos blockPos) {
|
| 123 | return level.getBlockState(blockPos.below()).isFaceSturdy(level, blockPos, Direction.UP);
|
| 124 | }
|
| 125 |
|
| 126 | private BlockPos placeLogBlock(
|
| 127 | final WorldGenLevel level, final RandomSource random, final BlockPos.MutableBlockPos blockPos, final Function<BlockState, BlockState> sidewaysStateModifier
|
| 128 | ) {
|
| 129 | level.setBlockAndUpdate(blockPos, sidewaysStateModifier.apply(this.trunkProvider.value().getState(level, random, blockPos)));
|
| 130 | this.markAboveForPostProcessing(level, blockPos);
|
| 131 | return blockPos.immutable();
|
| 132 | }
|
| 133 |
|
| 134 | private void decorateLogs(final WorldGenLevel level, final RandomSource random, final Set<BlockPos> logs, final List<TreeDecorator> decorators) {
|
| 135 | if (!decorators.isEmpty()) {
|
| 136 | TreeDecorator.Context decoratorContext = new TreeDecorator.Context(level, this.getDecorationSetter(level), random, logs, Set.of(), Set.of());
|
| 137 | decorators.forEach(decorator -> decorator.place(decoratorContext));
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | private BiConsumer<BlockPos, BlockState> getDecorationSetter(final WorldGenLevel level) {
|
| 142 | return (pos, state) -> level.setBlock(pos, state, 19);
|
| 143 | }
|
| 144 |
|
| 145 | private static Function<BlockState, BlockState> getSidewaysStateModifier(final Direction direction) {
|
| 146 | return state -> state.trySetValue(RotatedPillarBlock.AXIS, direction.getAxis());
|
| 147 | }
|
| 148 |
|
| 149 | public static FallenTreeFeature.Builder builder(final BlockStateProvider trunkProvider, final IntProvider logLength) {
|
| 150 | return new FallenTreeFeature.Builder(trunkProvider, logLength);
|
| 151 | }
|
| 152 |
|
| 153 | public static class Builder {
|
| 154 | private final BlockStateProvider trunkProvider;
|
| 155 | private final IntProvider logLength;
|
| 156 | private final List<TreeDecorator> stumpDecorators = new ArrayList<>();
|
| 157 | private final List<TreeDecorator> logDecorators = new ArrayList<>();
|
| 158 |
|
| 159 | public Builder(final BlockStateProvider trunkProvider, final IntProvider logLength) {
|
| 160 | this.trunkProvider = trunkProvider;
|
| 161 | this.logLength = logLength;
|
| 162 | }
|
| 163 |
|
| 164 | public FallenTreeFeature.Builder stumpDecorator(final TreeDecorator stumpDecorator) {
|
| 165 | this.stumpDecorators.add(stumpDecorator);
|
| 166 | return this;
|
| 167 | }
|
| 168 |
|
| 169 | public FallenTreeFeature.Builder logDecorator(final TreeDecorator logDecorator) {
|
| 170 | this.logDecorators.add(logDecorator);
|
| 171 | return this;
|
| 172 | }
|
| 173 |
|
| 174 | public FallenTreeFeature build() {
|
| 175 | return new FallenTreeFeature(Holder.direct(this.trunkProvider), this.logLength, List.copyOf(this.stumpDecorators), List.copyOf(this.logDecorators));
|
| 176 | }
|
| 177 | }
|
| 178 | }
|
| 179 |
|