| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.util.valueproviders.IntProvider;
|
| 10 | import net.minecraft.util.valueproviders.IntProviders;
|
| 11 | import net.minecraft.world.level.LevelAccessor;
|
| 12 | import net.minecraft.world.level.WorldGenLevel;
|
| 13 | import net.minecraft.world.level.block.Block;
|
| 14 | import net.minecraft.world.level.block.Blocks;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 17 |
|
| 18 | public record DeltaFeature(BlockState contents, BlockState rim, IntProvider size, IntProvider rimSize) implements Feature {
|
| 19 | public static final MapCodec<DeltaFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 20 | i -> i.group(
|
| 21 | BlockState.CODEC.fieldOf("contents").forGetter(DeltaFeature::contents),
|
| 22 | BlockState.CODEC.fieldOf("rim").forGetter(DeltaFeature::rim),
|
| 23 | IntProviders.codec(0, 16).fieldOf("size").forGetter(DeltaFeature::size),
|
| 24 | IntProviders.codec(0, 16).fieldOf("rim_size").forGetter(DeltaFeature::rimSize)
|
| 25 | )
|
| 26 | .apply(i, DeltaFeature::new)
|
| 27 | );
|
| 28 | private static final ImmutableList<Block> CANNOT_REPLACE = ImmutableList.of(
|
| 29 | Blocks.BEDROCK, Blocks.NETHER_BRICKS, Blocks.NETHER_BRICK_FENCE, Blocks.NETHER_BRICK_STAIRS, Blocks.NETHER_WART, Blocks.CHEST, Blocks.SPAWNER
|
| 30 | );
|
| 31 | private static final Direction[] DIRECTIONS = Direction.values();
|
| 32 | private static final double RIM_SPAWN_CHANCE = 0.9;
|
| 33 |
|
| 34 | @Override
|
| 35 | public MapCodec<DeltaFeature> codec() {
|
| 36 | return CODEC;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 41 | boolean anyPlaced = false;
|
| 42 | boolean spawnRim = random.nextDouble() < 0.9;
|
| 43 | int rimX = spawnRim ? this.rimSize.sample(random) : 0;
|
| 44 | int rimZ = spawnRim ? this.rimSize.sample(random) : 0;
|
| 45 | boolean hasRim = spawnRim && rimX != 0 && rimZ != 0;
|
| 46 | int radiusX = this.size.sample(random);
|
| 47 | int radiusZ = this.size.sample(random);
|
| 48 | int radiusLimit = Math.max(radiusX, radiusZ);
|
| 49 |
|
| 50 | for (BlockPos pos : BlockPos.withinBoxByManhattanDistance(origin, radiusX, 0, radiusZ)) {
|
| 51 | if (pos.distManhattan(origin) > radiusLimit) {
|
| 52 | break;
|
| 53 | }
|
| 54 |
|
| 55 | if (this.isClear(level, pos)) {
|
| 56 | if (hasRim) {
|
| 57 | anyPlaced = true;
|
| 58 | this.setBlock(level, pos, this.rim);
|
| 59 | }
|
| 60 |
|
| 61 | BlockPos posOffset = pos.offset(rimX, 0, rimZ);
|
| 62 | if (this.isClear(level, posOffset)) {
|
| 63 | anyPlaced = true;
|
| 64 | this.setBlock(level, posOffset, this.contents);
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | return anyPlaced;
|
| 70 | }
|
| 71 |
|
| 72 | private boolean isClear(final LevelAccessor level, final BlockPos pos) {
|
| 73 | BlockState state = level.getBlockState(pos);
|
| 74 | if (state.is(this.contents.getBlock())) {
|
| 75 | return false;
|
| 76 | }
|
| 77 |
|
| 78 | if (CANNOT_REPLACE.contains(state.getBlock())) {
|
| 79 | return false;
|
| 80 | }
|
| 81 |
|
| 82 | for (Direction d : DIRECTIONS) {
|
| 83 | boolean isAir = level.getBlockState(pos.relative(d)).isAir();
|
| 84 | if (isAir && d != Direction.UP || !isAir && d == Direction.UP) {
|
| 85 | return false;
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | return true;
|
| 90 | }
|
| 91 | }
|
| 92 |
|