| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.HolderSet;
|
| 8 | import net.minecraft.core.registries.Registries;
|
| 9 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 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 net.minecraft.world.level.material.FluidState;
|
| 16 |
|
| 17 | public record SpringFeature(FluidState state, boolean requiresBlockBelow, int rockCount, int holeCount, HolderSet<Block> validBlocks) implements Feature {
|
| 18 | public static final MapCodec<SpringFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 19 | i -> i.group(
|
| 20 | FluidState.CODEC.fieldOf("state").forGetter(SpringFeature::state),
|
| 21 | Codec.BOOL.optionalFieldOf("requires_block_below", true).forGetter(SpringFeature::requiresBlockBelow),
|
| 22 | Codec.INT.optionalFieldOf("rock_count", 4).forGetter(SpringFeature::rockCount),
|
| 23 | Codec.INT.optionalFieldOf("hole_count", 1).forGetter(SpringFeature::holeCount),
|
| 24 | RegistryCodecs.holderSet(Registries.BLOCK).fieldOf("valid_blocks").forGetter(SpringFeature::validBlocks)
|
| 25 | )
|
| 26 | .apply(i, SpringFeature::new)
|
| 27 | );
|
| 28 |
|
| 29 | @Override
|
| 30 | public MapCodec<SpringFeature> codec() {
|
| 31 | return CODEC;
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 36 | if (!level.getBlockState(origin.above()).is(this.validBlocks)) {
|
| 37 | return false;
|
| 38 | }
|
| 39 |
|
| 40 | if (this.requiresBlockBelow && !level.getBlockState(origin.below()).is(this.validBlocks)) {
|
| 41 | return false;
|
| 42 | }
|
| 43 |
|
| 44 | BlockState currentState = level.getBlockState(origin);
|
| 45 | if (!currentState.isAir() && !currentState.is(this.validBlocks)) {
|
| 46 | return false;
|
| 47 | }
|
| 48 |
|
| 49 | int placed = 0;
|
| 50 | int rockCount = 0;
|
| 51 | if (level.getBlockState(origin.west()).is(this.validBlocks)) {
|
| 52 | rockCount++;
|
| 53 | }
|
| 54 |
|
| 55 | if (level.getBlockState(origin.east()).is(this.validBlocks)) {
|
| 56 | rockCount++;
|
| 57 | }
|
| 58 |
|
| 59 | if (level.getBlockState(origin.north()).is(this.validBlocks)) {
|
| 60 | rockCount++;
|
| 61 | }
|
| 62 |
|
| 63 | if (level.getBlockState(origin.south()).is(this.validBlocks)) {
|
| 64 | rockCount++;
|
| 65 | }
|
| 66 |
|
| 67 | if (level.getBlockState(origin.below()).is(this.validBlocks)) {
|
| 68 | rockCount++;
|
| 69 | }
|
| 70 |
|
| 71 | int holeCount = 0;
|
| 72 | if (level.isEmptyBlock(origin.west())) {
|
| 73 | holeCount++;
|
| 74 | }
|
| 75 |
|
| 76 | if (level.isEmptyBlock(origin.east())) {
|
| 77 | holeCount++;
|
| 78 | }
|
| 79 |
|
| 80 | if (level.isEmptyBlock(origin.north())) {
|
| 81 | holeCount++;
|
| 82 | }
|
| 83 |
|
| 84 | if (level.isEmptyBlock(origin.south())) {
|
| 85 | holeCount++;
|
| 86 | }
|
| 87 |
|
| 88 | if (level.isEmptyBlock(origin.below())) {
|
| 89 | holeCount++;
|
| 90 | }
|
| 91 |
|
| 92 | if (rockCount == this.rockCount && holeCount == this.holeCount) {
|
| 93 | level.setBlock(origin, this.state.createLegacyBlock(), 2);
|
| 94 | level.scheduleTick(origin, this.state.getType(), 0);
|
| 95 | placed++;
|
| 96 | }
|
| 97 |
|
| 98 | return placed > 0;
|
| 99 | }
|
| 100 | }
|
| 101 |
|