| 1 | package net.minecraft.world.level.levelgen.structure.placement;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.Optional;
|
| 8 | import net.minecraft.core.Vec3i;
|
| 9 | import net.minecraft.world.level.ChunkPos;
|
| 10 | import net.minecraft.world.level.chunk.ChunkGeneratorStructureState;
|
| 11 | import net.minecraft.world.level.levelgen.LegacyRandomSource;
|
| 12 | import net.minecraft.world.level.levelgen.WorldgenRandom;
|
| 13 |
|
| 14 | public class RandomSpreadStructurePlacement extends AbstractSpreadingStructurePlacement {
|
| 15 | public static final MapCodec<RandomSpreadStructurePlacement> CODEC = RecordCodecBuilder.<RandomSpreadStructurePlacement>mapCodec(
|
| 16 | i -> placementCodec(i)
|
| 17 | .and(
|
| 18 | i.group(
|
| 19 | Codec.intRange(0, 4096).fieldOf("spacing").forGetter(RandomSpreadStructurePlacement::spacing),
|
| 20 | Codec.intRange(0, 4096).fieldOf("separation").forGetter(RandomSpreadStructurePlacement::separation),
|
| 21 | RandomSpreadType.CODEC.optionalFieldOf("spread_type", RandomSpreadType.LINEAR).forGetter(RandomSpreadStructurePlacement::spreadType)
|
| 22 | )
|
| 23 | )
|
| 24 | .apply(i, RandomSpreadStructurePlacement::new)
|
| 25 | )
|
| 26 | .validate(RandomSpreadStructurePlacement::validate);
|
| 27 | private final int spacing;
|
| 28 | private final int separation;
|
| 29 | private final RandomSpreadType spreadType;
|
| 30 |
|
| 31 | private static DataResult<RandomSpreadStructurePlacement> validate(final RandomSpreadStructurePlacement c) {
|
| 32 | return c.spacing <= c.separation ? DataResult.error(() -> "Spacing has to be larger than separation") : DataResult.success(c);
|
| 33 | }
|
| 34 |
|
| 35 | public RandomSpreadStructurePlacement(
|
| 36 | final Vec3i locateOffset,
|
| 37 | final AbstractSpreadingStructurePlacement.FrequencyReductionMethod frequencyReductionMethod,
|
| 38 | final float frequency,
|
| 39 | final int salt,
|
| 40 | final Optional<AbstractSpreadingStructurePlacement.ExclusionZone> exclusionZone,
|
| 41 | final int spacing,
|
| 42 | final int separation,
|
| 43 | final RandomSpreadType spreadType
|
| 44 | ) {
|
| 45 | super(locateOffset, frequencyReductionMethod, frequency, salt, exclusionZone);
|
| 46 | this.spacing = spacing;
|
| 47 | this.separation = separation;
|
| 48 | this.spreadType = spreadType;
|
| 49 | }
|
| 50 |
|
| 51 | public RandomSpreadStructurePlacement(final int spacing, final int separation, final RandomSpreadType spreadType, final int salt) {
|
| 52 | this(Vec3i.ZERO, AbstractSpreadingStructurePlacement.FrequencyReductionMethod.DEFAULT, 1.0F, salt, Optional.empty(), spacing, separation, spreadType);
|
| 53 | }
|
| 54 |
|
| 55 | public int spacing() {
|
| 56 | return this.spacing;
|
| 57 | }
|
| 58 |
|
| 59 | public int separation() {
|
| 60 | return this.separation;
|
| 61 | }
|
| 62 |
|
| 63 | public RandomSpreadType spreadType() {
|
| 64 | return this.spreadType;
|
| 65 | }
|
| 66 |
|
| 67 | public ChunkPos getPotentialStructureChunk(final long seed, final int sourceX, final int sourceZ) {
|
| 68 | int spacedGridX = Math.floorDiv(sourceX, this.spacing);
|
| 69 | int spacedGridZ = Math.floorDiv(sourceZ, this.spacing);
|
| 70 | WorldgenRandom random = new WorldgenRandom(new LegacyRandomSource(0L));
|
| 71 | random.setLargeFeatureWithSalt(seed, spacedGridX, spacedGridZ, this.salt());
|
| 72 | int limit = this.spacing - this.separation;
|
| 73 | int spreadX = this.spreadType.evaluate(random, limit);
|
| 74 | int spreadZ = this.spreadType.evaluate(random, limit);
|
| 75 | return new ChunkPos(spacedGridX * this.spacing + spreadX, spacedGridZ * this.spacing + spreadZ);
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | protected boolean isPlacementChunk(final ChunkGeneratorStructureState state, final int sourceX, final int sourceZ) {
|
| 80 | ChunkPos chunkPos = this.getPotentialStructureChunk(state.getLevelSeed(), sourceX, sourceZ);
|
| 81 | return chunkPos.x() == sourceX && chunkPos.z() == sourceZ;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public MapCodec<RandomSpreadStructurePlacement> codec() {
|
| 86 | return CODEC;
|
| 87 | }
|
| 88 | }
|
| 89 |
|