| 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.core.HolderSet;
|
| 9 | import net.minecraft.core.registries.Registries;
|
| 10 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.util.valueproviders.IntProvider;
|
| 13 | import net.minecraft.util.valueproviders.IntProviders;
|
| 14 | import net.minecraft.world.level.WorldGenLevel;
|
| 15 | import net.minecraft.world.level.block.Block;
|
| 16 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 17 | import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
|
| 18 | import net.minecraft.world.level.levelgen.feature.stateproviders.BlockStateProvider;
|
| 19 |
|
| 20 | public record RandomNeighborSpreadFeature(
|
| 21 | Holder<BlockStateProvider> block,
|
| 22 | HolderSet<Block> acceptedNeighbors,
|
| 23 | BlockPredicate canReplace,
|
| 24 | IntProvider attempts,
|
| 25 | IntProvider xzOffset,
|
| 26 | IntProvider yOffset
|
| 27 | ) implements Feature {
|
| 28 | public static final MapCodec<RandomNeighborSpreadFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 29 | i -> i.group(
|
| 30 | BlockStateProvider.CODEC.fieldOf("block").forGetter(RandomNeighborSpreadFeature::block),
|
| 31 | RegistryCodecs.holderSet(Registries.BLOCK).fieldOf("accepted_neighbors").forGetter(RandomNeighborSpreadFeature::acceptedNeighbors),
|
| 32 | BlockPredicate.CODEC.fieldOf("can_replace").forGetter(RandomNeighborSpreadFeature::canReplace),
|
| 33 | IntProviders.codec(1, 3000).fieldOf("attempts").forGetter(RandomNeighborSpreadFeature::attempts),
|
| 34 | IntProviders.codec(-16, 16).fieldOf("xz_offset").forGetter(RandomNeighborSpreadFeature::xzOffset),
|
| 35 | IntProviders.codec(-16, 16).fieldOf("y_offset").forGetter(RandomNeighborSpreadFeature::yOffset)
|
| 36 | )
|
| 37 | .apply(i, RandomNeighborSpreadFeature::new)
|
| 38 | );
|
| 39 | private static final Direction[] DIRECTIONS = Direction.values();
|
| 40 |
|
| 41 | @Override
|
| 42 | public MapCodec<RandomNeighborSpreadFeature> codec() {
|
| 43 | return CODEC;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 48 | level.setBlock(origin, this.block.value().getState(level, random, origin), 2);
|
| 49 | BlockPos.MutableBlockPos placePos = new BlockPos.MutableBlockPos();
|
| 50 | BlockPos.MutableBlockPos neighborPos = new BlockPos.MutableBlockPos();
|
| 51 | int attempts = this.attempts.sample(random);
|
| 52 |
|
| 53 | for (int i = 0; i < attempts; i++) {
|
| 54 | placePos.setWithOffset(origin, this.xzOffset.sample(random), this.yOffset.sample(random), this.xzOffset.sample(random));
|
| 55 | if (this.canReplace.test(level, placePos)) {
|
| 56 | int neighbours = 0;
|
| 57 |
|
| 58 | for (Direction direction : DIRECTIONS) {
|
| 59 | neighborPos.setWithOffset(placePos, direction);
|
| 60 | if (level.getBlockState(neighborPos).is(this.acceptedNeighbors)) {
|
| 61 | neighbours++;
|
| 62 | }
|
| 63 |
|
| 64 | if (neighbours > 1) {
|
| 65 | break;
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | if (neighbours == 1) {
|
| 70 | level.setBlock(placePos, this.block.value().getState(level, random, placePos), 2);
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | return true;
|
| 76 | }
|
| 77 | }
|
| 78 |
|