| 1 | package net.minecraft.world.level.levelgen.placement;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import java.util.function.Consumer;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.util.RandomSource;
|
| 7 | import net.minecraft.util.valueproviders.ConstantInt;
|
| 8 | import net.minecraft.util.valueproviders.IntProvider;
|
| 9 | import net.minecraft.util.valueproviders.IntProviders;
|
| 10 | import net.minecraft.world.level.block.Blocks;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 13 |
|
| 14 | @Deprecated
|
| 15 | public record CountOnEveryLayerPlacement(IntProvider count) implements PlacementModifier {
|
| 16 | public static final MapCodec<CountOnEveryLayerPlacement> CODEC = IntProviders.codec(0, 256)
|
| 17 | .fieldOf("count")
|
| 18 | .xmap(CountOnEveryLayerPlacement::new, CountOnEveryLayerPlacement::count);
|
| 19 |
|
| 20 | public static CountOnEveryLayerPlacement of(final IntProvider count) {
|
| 21 | return new CountOnEveryLayerPlacement(count);
|
| 22 | }
|
| 23 |
|
| 24 | public static CountOnEveryLayerPlacement of(final int count) {
|
| 25 | return of(ConstantInt.of(count));
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public void modify(final PlacementContext context, final RandomSource random, final BlockPos origin, final Consumer<BlockPos> output) {
|
| 30 | int layer = 0;
|
| 31 |
|
| 32 | boolean foundAny;
|
| 33 | do {
|
| 34 | foundAny = false;
|
| 35 |
|
| 36 | for (int i = 0; i < this.count.sample(random); i++) {
|
| 37 | int x = random.nextInt(16) + origin.getX();
|
| 38 | int z = random.nextInt(16) + origin.getZ();
|
| 39 | int startY = context.getHeight(Heightmap.Types.MOTION_BLOCKING, x, z);
|
| 40 | int y = findOnGroundYPosition(context, x, startY, z, layer);
|
| 41 | if (y != Integer.MAX_VALUE) {
|
| 42 | output.accept(new BlockPos(x, y, z));
|
| 43 | foundAny = true;
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | layer++;
|
| 48 | } while (foundAny);
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public MapCodec<CountOnEveryLayerPlacement> codec() {
|
| 53 | return CODEC;
|
| 54 | }
|
| 55 |
|
| 56 | private static int findOnGroundYPosition(final PlacementContext context, final int xStart, final int yStart, final int zStart, final int layerToPlaceOn) {
|
| 57 | BlockPos.MutableBlockPos currentPos = new BlockPos.MutableBlockPos(xStart, yStart, zStart);
|
| 58 | int currentLayer = 0;
|
| 59 | BlockState currentBlock = context.getBlockState(currentPos);
|
| 60 |
|
| 61 | for (int y = yStart; y >= context.getMinY() + 1; y--) {
|
| 62 | currentPos.setY(y - 1);
|
| 63 | BlockState belowBlock = context.getBlockState(currentPos);
|
| 64 | if (!isEmpty(belowBlock) && isEmpty(currentBlock) && !belowBlock.is(Blocks.BEDROCK)) {
|
| 65 | if (currentLayer == layerToPlaceOn) {
|
| 66 | return currentPos.getY() + 1;
|
| 67 | }
|
| 68 |
|
| 69 | currentLayer++;
|
| 70 | }
|
| 71 |
|
| 72 | currentBlock = belowBlock;
|
| 73 | }
|
| 74 |
|
| 75 | return Integer.MAX_VALUE;
|
| 76 | }
|
| 77 |
|
| 78 | private static boolean isEmpty(final BlockState blockState) {
|
| 79 | return blockState.isAir() || blockState.is(Blocks.WATER) || blockState.is(Blocks.LAVA);
|
| 80 | }
|
| 81 | }
|
| 82 |
|