| 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.util.ExtraCodecs;
|
| 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.state.BlockState;
|
| 15 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 16 | import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
|
| 17 | import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
| 18 |
|
| 19 | public record ProjectedRandomPatchySquare(Holder<BlockStateProvider> block, BlockPredicate projectThrough, IntProvider size, int maxProjectionHeight)
|
| 20 | implements Feature {
|
| 21 | public static final MapCodec<ProjectedRandomPatchySquare> CODEC = RecordCodecBuilder.mapCodec(
|
| 22 | i -> i.group(
|
| 23 | BlockStateProvider.CODEC.fieldOf("block").forGetter(ProjectedRandomPatchySquare::block),
|
| 24 | BlockPredicate.CODEC.fieldOf("project_through").forGetter(ProjectedRandomPatchySquare::projectThrough),
|
| 25 | IntProviders.codec(1, 16).fieldOf("size").forGetter(ProjectedRandomPatchySquare::size),
|
| 26 | ExtraCodecs.NON_NEGATIVE_INT.fieldOf("max_projection_height").forGetter(ProjectedRandomPatchySquare::maxProjectionHeight)
|
| 27 | )
|
| 28 | .apply(i, ProjectedRandomPatchySquare::new)
|
| 29 | );
|
| 30 |
|
| 31 | @Override
|
| 32 | public MapCodec<ProjectedRandomPatchySquare> codec() {
|
| 33 | return CODEC;
|
| 34 | }
|
| 35 |
|
| 36 | @Override
|
| 37 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos pos) {
|
| 38 | BlockPos.MutableBlockPos basePos = new BlockPos.MutableBlockPos();
|
| 39 | BlockPos.MutableBlockPos tmpPos = new BlockPos.MutableBlockPos();
|
| 40 | int size = this.size.sample(random);
|
| 41 | int bound = Mth.square(size) + 1;
|
| 42 |
|
| 43 | for (int dx = -size; dx <= size; dx++) {
|
| 44 | for (int dz = -size; dz <= size; dz++) {
|
| 45 | int probability = Mth.abs(dx) * Mth.abs(dz);
|
| 46 | if (random.nextInt(bound) < bound - probability) {
|
| 47 | basePos.setWithOffset(pos, dx, 0, dz);
|
| 48 | int drop = this.maxProjectionHeight;
|
| 49 |
|
| 50 | while (this.projectThrough.test(level, tmpPos.setWithOffset(basePos, Direction.DOWN))) {
|
| 51 | basePos.move(Direction.DOWN);
|
| 52 | if (--drop <= 0) {
|
| 53 | break;
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | BlockState state = this.block.value().getOptionalState(level, random, basePos);
|
| 58 | if (state != null) {
|
| 59 | level.setBlock(basePos, state, 2);
|
| 60 | }
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | return true;
|
| 66 | }
|
| 67 | }
|
| 68 |
|