| 1 | package net.minecraft.world.level.levelgen.heightproviders;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.level.levelgen.VerticalAnchor;
|
| 10 | import net.minecraft.world.level.levelgen.WorldGenerationContext;
|
| 11 | import org.slf4j.Logger;
|
| 12 |
|
| 13 | public class VeryBiasedToBottomHeight extends HeightProvider {
|
| 14 | public static final MapCodec<VeryBiasedToBottomHeight> CODEC = RecordCodecBuilder.mapCodec(
|
| 15 | i -> i.group(
|
| 16 | VerticalAnchor.CODEC.fieldOf("min_inclusive").forGetter(u -> u.minInclusive),
|
| 17 | VerticalAnchor.CODEC.fieldOf("max_inclusive").forGetter(u -> u.maxInclusive),
|
| 18 | Codec.intRange(1, Integer.MAX_VALUE).optionalFieldOf("inner", 1).forGetter(u -> u.inner)
|
| 19 | )
|
| 20 | .apply(i, VeryBiasedToBottomHeight::new)
|
| 21 | );
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 | private final VerticalAnchor minInclusive;
|
| 24 | private final VerticalAnchor maxInclusive;
|
| 25 | private final int inner;
|
| 26 |
|
| 27 | private VeryBiasedToBottomHeight(final VerticalAnchor minInclusive, final VerticalAnchor maxInclusive, final int inner) {
|
| 28 | this.minInclusive = minInclusive;
|
| 29 | this.maxInclusive = maxInclusive;
|
| 30 | this.inner = inner;
|
| 31 | }
|
| 32 |
|
| 33 | public static VeryBiasedToBottomHeight of(final VerticalAnchor minInclusive, final VerticalAnchor maxInclusive, final int offset) {
|
| 34 | return new VeryBiasedToBottomHeight(minInclusive, maxInclusive, offset);
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public int sample(final RandomSource random, final WorldGenerationContext context) {
|
| 39 | int min = this.minInclusive.resolveY(context);
|
| 40 | int max = this.maxInclusive.resolveY(context);
|
| 41 | if (max - min - this.inner + 1 <= 0) {
|
| 42 | LOGGER.warn("Empty height range: {}", this);
|
| 43 | return min;
|
| 44 | } else {
|
| 45 | int upperInclusive = Mth.nextInt(random, min + this.inner, max);
|
| 46 | int biasedUpperInclusive = Mth.nextInt(random, min, upperInclusive - 1);
|
| 47 | return Mth.nextInt(random, min, biasedUpperInclusive - 1 + this.inner);
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public HeightProviderType<?> getType() {
|
| 53 | return HeightProviderType.VERY_BIASED_TO_BOTTOM;
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public String toString() {
|
| 58 | return "biased[" + this.minInclusive + "-" + this.maxInclusive + " inner: " + this.inner + "]";
|
| 59 | }
|
| 60 | }
|
| 61 |
|