| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.HolderSet;
|
| 9 | import net.minecraft.core.registries.Registries;
|
| 10 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 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.LevelAccessor;
|
| 15 | import net.minecraft.world.level.WorldGenLevel;
|
| 16 | import net.minecraft.world.level.block.Block;
|
| 17 | import net.minecraft.world.level.block.state.BlockState;
|
| 18 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 19 | import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
|
| 20 | import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public record SteppedColumnClusterFeature(
|
| 24 | Holder<BlockStateProvider> block,
|
| 25 | BlockPredicate continueThrough,
|
| 26 | BlockPredicate canReplace,
|
| 27 | HolderSet<Block> cannotPlaceOn,
|
| 28 | IntProvider clusterReach,
|
| 29 | IntProvider columnCount,
|
| 30 | IntProvider columnReach,
|
| 31 | IntProvider height
|
| 32 | ) implements Feature {
|
| 33 | public static final MapCodec<SteppedColumnClusterFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 34 | i -> i.group(
|
| 35 | BlockStateProvider.CODEC.fieldOf("block").forGetter(SteppedColumnClusterFeature::block),
|
| 36 | BlockPredicate.CODEC.fieldOf("continue_through").forGetter(SteppedColumnClusterFeature::continueThrough),
|
| 37 | BlockPredicate.CODEC.fieldOf("can_replace").forGetter(SteppedColumnClusterFeature::canReplace),
|
| 38 | RegistryCodecs.holderSet(Registries.BLOCK).fieldOf("cannot_place_on").forGetter(SteppedColumnClusterFeature::cannotPlaceOn),
|
| 39 | IntProviders.codec(0, 13).fieldOf("cluster_reach").forGetter(SteppedColumnClusterFeature::clusterReach),
|
| 40 | IntProviders.codec(1, 150).fieldOf("column_count").forGetter(SteppedColumnClusterFeature::columnCount),
|
| 41 | IntProviders.codec(0, 3).fieldOf("column_reach").forGetter(SteppedColumnClusterFeature::columnReach),
|
| 42 | IntProviders.codec(1, 10).fieldOf("height").forGetter(SteppedColumnClusterFeature::height)
|
| 43 | )
|
| 44 | .apply(i, SteppedColumnClusterFeature::new)
|
| 45 | );
|
| 46 |
|
| 47 | @Override
|
| 48 | public MapCodec<SteppedColumnClusterFeature> codec() {
|
| 49 | return CODEC;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 54 | int lavaSeaLevel = chunkGenerator.getSeaLevel();
|
| 55 | if (!this.canPlaceAt(level, origin.mutable())) {
|
| 56 | return false;
|
| 57 | }
|
| 58 |
|
| 59 | int columnHeight = this.height.sample(random);
|
| 60 | int clusterReach = Math.min(columnHeight, this.clusterReach.sample(random));
|
| 61 | int count = this.columnCount.sample(random);
|
| 62 | boolean placed = false;
|
| 63 |
|
| 64 | for (BlockPos pos : BlockPos.randomBetweenClosed(
|
| 65 | random,
|
| 66 | count,
|
| 67 | origin.getX() - clusterReach,
|
| 68 | origin.getY(),
|
| 69 | origin.getZ() - clusterReach,
|
| 70 | origin.getX() + clusterReach,
|
| 71 | origin.getY(),
|
| 72 | origin.getZ() + clusterReach
|
| 73 | )) {
|
| 74 | int blocksToPlaceY = columnHeight - pos.distManhattan(origin);
|
| 75 | if (blocksToPlaceY >= 0) {
|
| 76 | placed |= this.placeColumn(level, random, lavaSeaLevel, pos, blocksToPlaceY, this.columnReach.sample(random));
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | return placed;
|
| 81 | }
|
| 82 |
|
| 83 | private boolean placeColumn(
|
| 84 | final WorldGenLevel level, final RandomSource random, final int lavaSeaLevel, final BlockPos origin, final int columnHeight, final int reach
|
| 85 | ) {
|
| 86 | boolean placedAny = false;
|
| 87 |
|
| 88 | for (BlockPos pos : BlockPos.betweenClosed(
|
| 89 | origin.getX() - reach, origin.getY(), origin.getZ() - reach, origin.getX() + reach, origin.getY(), origin.getZ() + reach
|
| 90 | )) {
|
| 91 | int stepLimit = pos.distManhattan(origin);
|
| 92 | BlockPos columnPos = this.canReplace.test(level, pos)
|
| 93 | ? this.findSurface(level, lavaSeaLevel, pos.mutable(), stepLimit)
|
| 94 | : this.findAir(level, pos.mutable(), stepLimit);
|
| 95 | if (columnPos != null) {
|
| 96 | int blocksY = columnHeight - stepLimit / 2;
|
| 97 | BlockPos.MutableBlockPos cursor = columnPos.mutable();
|
| 98 |
|
| 99 | while (blocksY >= 0) {
|
| 100 | if (this.canReplace.test(level, cursor)) {
|
| 101 | this.setBlock(level, cursor, this.block.value().getState(level, random, cursor));
|
| 102 | cursor.move(Direction.UP);
|
| 103 | placedAny = true;
|
| 104 | } else {
|
| 105 | if (!this.continueThrough.test(level, cursor)) {
|
| 106 | break;
|
| 107 | }
|
| 108 |
|
| 109 | cursor.move(Direction.UP);
|
| 110 | }
|
| 111 |
|
| 112 | blocksY--;
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | return placedAny;
|
| 118 | }
|
| 119 |
|
| 120 | private @Nullable BlockPos findSurface(final WorldGenLevel level, final int lavaSeaLevel, final BlockPos.MutableBlockPos cursor, int limit) {
|
| 121 | while (cursor.getY() > level.getMinY() + 1 && limit > 0) {
|
| 122 | limit--;
|
| 123 | if (this.canPlaceAt(level, cursor)) {
|
| 124 | return cursor;
|
| 125 | }
|
| 126 |
|
| 127 | cursor.move(Direction.DOWN);
|
| 128 | }
|
| 129 |
|
| 130 | return null;
|
| 131 | }
|
| 132 |
|
| 133 | private boolean canPlaceAt(final WorldGenLevel level, final BlockPos.MutableBlockPos cursor) {
|
| 134 | if (!this.canReplace.test(level, cursor)) {
|
| 135 | return false;
|
| 136 | }
|
| 137 |
|
| 138 | BlockState blockState = level.getBlockState(cursor.move(Direction.DOWN));
|
| 139 | cursor.move(Direction.UP);
|
| 140 | return !blockState.isAir() && !blockState.is(this.cannotPlaceOn);
|
| 141 | }
|
| 142 |
|
| 143 | private @Nullable BlockPos findAir(final LevelAccessor level, final BlockPos.MutableBlockPos cursor, int limit) {
|
| 144 | while (cursor.getY() <= level.getMaxY() && limit > 0) {
|
| 145 | limit--;
|
| 146 | BlockState blockState = level.getBlockState(cursor);
|
| 147 | if (blockState.is(this.cannotPlaceOn)) {
|
| 148 | return null;
|
| 149 | }
|
| 150 |
|
| 151 | if (blockState.isAir()) {
|
| 152 | return cursor;
|
| 153 | }
|
| 154 |
|
| 155 | cursor.move(Direction.UP);
|
| 156 | }
|
| 157 |
|
| 158 | return null;
|
| 159 | }
|
| 160 | }
|
| 161 |
|