| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.List;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.core.Holder;
|
| 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.chunk.ChunkGenerator;
|
| 15 | import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
|
| 16 | import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
| 17 |
|
| 18 | public record BlockColumnFeature(List<BlockColumnFeature.Layer> layers, Direction direction, BlockPredicate allowedPlacement, boolean prioritizeTip)
|
| 19 | implements Feature {
|
| 20 | public static final MapCodec<BlockColumnFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 21 | i -> i.group(
|
| 22 | BlockColumnFeature.Layer.CODEC.listOf().fieldOf("layers").forGetter(BlockColumnFeature::layers),
|
| 23 | Direction.CODEC.fieldOf("direction").forGetter(BlockColumnFeature::direction),
|
| 24 | BlockPredicate.CODEC.fieldOf("allowed_placement").forGetter(BlockColumnFeature::allowedPlacement),
|
| 25 | Codec.BOOL.fieldOf("prioritize_tip").forGetter(BlockColumnFeature::prioritizeTip)
|
| 26 | )
|
| 27 | .apply(i, BlockColumnFeature::new)
|
| 28 | );
|
| 29 |
|
| 30 | public static BlockColumnFeature.Layer layer(final IntProvider height, final Holder<BlockStateProvider> state) {
|
| 31 | return new BlockColumnFeature.Layer(height, state);
|
| 32 | }
|
| 33 |
|
| 34 | public static BlockColumnFeature.Layer layer(final IntProvider height, final BlockStateProvider state) {
|
| 35 | return layer(height, Holder.direct(state));
|
| 36 | }
|
| 37 |
|
| 38 | public static BlockColumnFeature simple(final IntProvider height, final BlockStateProvider state) {
|
| 39 | return new BlockColumnFeature(List.of(layer(height, Holder.direct(state))), Direction.UP, BlockPredicate.ONLY_IN_AIR_PREDICATE, false);
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public MapCodec<BlockColumnFeature> codec() {
|
| 44 | return CODEC;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 49 | int layerCount = this.layers.size();
|
| 50 | int[] layerHeights = new int[layerCount];
|
| 51 | int totalHeight = 0;
|
| 52 |
|
| 53 | for (int i = 0; i < layerCount; i++) {
|
| 54 | layerHeights[i] = this.layers.get(i).height().sample(random);
|
| 55 | totalHeight += layerHeights[i];
|
| 56 | }
|
| 57 |
|
| 58 | if (totalHeight == 0) {
|
| 59 | return false;
|
| 60 | }
|
| 61 |
|
| 62 | BlockPos.MutableBlockPos placePos = origin.mutable();
|
| 63 | BlockPos.MutableBlockPos nextPos = placePos.mutable().move(this.direction);
|
| 64 |
|
| 65 | for (int y = 0; y < totalHeight; y++) {
|
| 66 | if (!this.allowedPlacement.test(level, nextPos)) {
|
| 67 | truncate(layerHeights, totalHeight, y, this.prioritizeTip);
|
| 68 | break;
|
| 69 | }
|
| 70 |
|
| 71 | nextPos.move(this.direction);
|
| 72 | }
|
| 73 |
|
| 74 | for (int i = 0; i < layerCount; i++) {
|
| 75 | int count = layerHeights[i];
|
| 76 | if (count != 0) {
|
| 77 | BlockColumnFeature.Layer layer = this.layers.get(i);
|
| 78 |
|
| 79 | for (int y = 0; y < count; y++) {
|
| 80 | level.setBlock(placePos, layer.state().value().getState(level, random, placePos), 2);
|
| 81 | placePos.move(this.direction);
|
| 82 | }
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | return true;
|
| 87 | }
|
| 88 |
|
| 89 | private static void truncate(final int[] layerHeights, final int totalHeight, final int newHeight, final boolean prioritizeTip) {
|
| 90 | int amountToRemove = totalHeight - newHeight;
|
| 91 | int direction = prioritizeTip ? 1 : -1;
|
| 92 | int start = prioritizeTip ? 0 : layerHeights.length - 1;
|
| 93 | int end = prioritizeTip ? layerHeights.length : -1;
|
| 94 |
|
| 95 | for (int i = start; i != end && amountToRemove > 0; i += direction) {
|
| 96 | int thisLayer = layerHeights[i];
|
| 97 | int toRemoveFromLayer = Math.min(thisLayer, amountToRemove);
|
| 98 | amountToRemove -= toRemoveFromLayer;
|
| 99 | layerHeights[i] -= toRemoveFromLayer;
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | public record Layer(IntProvider height, Holder<BlockStateProvider> state) {
|
| 104 | public static final Codec<BlockColumnFeature.Layer> CODEC = RecordCodecBuilder.create(
|
| 105 | i -> i.group(
|
| 106 | IntProviders.NON_NEGATIVE_CODEC.fieldOf("height").forGetter(BlockColumnFeature.Layer::height),
|
| 107 | BlockStateProvider.CODEC.fieldOf("provider").forGetter(BlockColumnFeature.Layer::state)
|
| 108 | )
|
| 109 | .apply(i, BlockColumnFeature.Layer::new)
|
| 110 | );
|
| 111 | }
|
| 112 | }
|
| 113 |
|