| 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.util.RandomSource;
|
| 8 | import net.minecraft.util.valueproviders.IntProvider;
|
| 9 | import net.minecraft.util.valueproviders.IntProviders;
|
| 10 | import net.minecraft.world.level.LevelAccessor;
|
| 11 | import net.minecraft.world.level.WorldGenLevel;
|
| 12 | import net.minecraft.world.level.block.Block;
|
| 13 | import net.minecraft.world.level.block.state.BlockState;
|
| 14 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | public record ReplaceBlobsFeature(BlockState targetState, BlockState replaceState, IntProvider radius) implements Feature {
|
| 18 | public static final MapCodec<ReplaceBlobsFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 19 | i -> i.group(
|
| 20 | BlockState.CODEC.fieldOf("target").forGetter(ReplaceBlobsFeature::targetState),
|
| 21 | BlockState.CODEC.fieldOf("state").forGetter(ReplaceBlobsFeature::replaceState),
|
| 22 | IntProviders.codec(0, 12).fieldOf("radius").forGetter(ReplaceBlobsFeature::radius)
|
| 23 | )
|
| 24 | .apply(i, ReplaceBlobsFeature::new)
|
| 25 | );
|
| 26 |
|
| 27 | @Override
|
| 28 | public MapCodec<ReplaceBlobsFeature> codec() {
|
| 29 | return CODEC;
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 34 | Block targetBlock = this.targetState.getBlock();
|
| 35 | BlockPos centerPos = findTarget(level, origin.mutable().clamp(Direction.Axis.Y, level.getMinY() + 1, level.getMaxY()), targetBlock);
|
| 36 | if (centerPos == null) {
|
| 37 | return false;
|
| 38 | }
|
| 39 |
|
| 40 | int radiusX = this.radius.sample(random);
|
| 41 | int radiusY = this.radius.sample(random);
|
| 42 | int radiusZ = this.radius.sample(random);
|
| 43 | int maximumRadius = Math.max(radiusX, Math.max(radiusY, radiusZ));
|
| 44 | boolean replacedAny = false;
|
| 45 |
|
| 46 | for (BlockPos pos : BlockPos.withinBoxByManhattanDistance(centerPos, radiusX, radiusY, radiusZ)) {
|
| 47 | if (pos.distManhattan(centerPos) > maximumRadius) {
|
| 48 | break;
|
| 49 | }
|
| 50 |
|
| 51 | BlockState blockState = level.getBlockState(pos);
|
| 52 | if (blockState.is(targetBlock)) {
|
| 53 | this.setBlock(level, pos, this.replaceState);
|
| 54 | replacedAny = true;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | return replacedAny;
|
| 59 | }
|
| 60 |
|
| 61 | private static @Nullable BlockPos findTarget(final LevelAccessor level, final BlockPos.MutableBlockPos cursor, final Block target) {
|
| 62 | while (cursor.getY() > level.getMinY() + 1) {
|
| 63 | BlockState blockState = level.getBlockState(cursor);
|
| 64 | if (blockState.is(target)) {
|
| 65 | return cursor;
|
| 66 | }
|
| 67 |
|
| 68 | cursor.move(Direction.DOWN);
|
| 69 | }
|
| 70 |
|
| 71 | return null;
|
| 72 | }
|
| 73 | }
|
| 74 |
|