TrapezoidInt.java

net.minecraft.util.valueproviders.TrapezoidInt

信息

  • 全限定名:net.minecraft.util.valueproviders.TrapezoidInt
  • 类型:public record
  • 包:net.minecraft.util.valueproviders
  • 源码路径:src/main/java/net/minecraft/util/valueproviders/TrapezoidInt.java
  • 起始行号:L10
  • 实现:IntProvider
  • 职责:

    TODO

字段/常量

  • MAP_CODEC
    • 类型: MapCodec<TrapezoidInt>
    • 修饰符: public static final
    • 源码定位: L11
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static TrapezoidInt of(int min, int max, int plateau) @ L31

  • 方法名:of
  • 源码定位:L31
  • 返回类型:TrapezoidInt
  • 修饰符:public static

参数:

  • min: int
  • max: int
  • plateau: int

说明:

TODO

public static IntProvider triangle(int range) @ L35

  • 方法名:triangle
  • 源码定位:L35
  • 返回类型:IntProvider
  • 修饰符:public static

参数:

  • range: int

说明:

TODO

public int sample(RandomSource random) @ L39

  • 方法名:sample
  • 源码定位:L39
  • 返回类型:int
  • 修饰符:public

参数:

  • random: RandomSource

说明:

TODO

public MapCodec<TrapezoidInt> codec() @ L55

  • 方法名:codec
  • 源码定位:L55
  • 返回类型:MapCodec
  • 修饰符:public

参数:

说明:

TODO

public String toString() @ L60

  • 方法名:toString
  • 源码定位:L60
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

代码

public record TrapezoidInt(int minInclusive, int maxInclusive, int plateau) implements IntProvider {
    public static final MapCodec<TrapezoidInt> MAP_CODEC = RecordCodecBuilder.<TrapezoidInt>mapCodec(
            i -> i.group(
                    Codec.INT.fieldOf("min").forGetter(TrapezoidInt::minInclusive),
                    Codec.INT.fieldOf("max").forGetter(TrapezoidInt::maxInclusive),
                    Codec.INT.fieldOf("plateau").forGetter(TrapezoidInt::plateau)
                )
                .apply(i, TrapezoidInt::new)
        )
        .validate(
            c -> {
                if (c.maxInclusive < c.minInclusive) {
                    return DataResult.error(() -> "Max must be larger than min: [" + c.minInclusive + ", " + c.maxInclusive + "]");
                } else {
                    return c.plateau > c.maxInclusive - c.minInclusive
                        ? DataResult.error(() -> "Plateau can at most be the full span: [" + c.minInclusive + ", " + c.maxInclusive + "]")
                        : DataResult.success(c);
                }
            }
        );
 
    public static TrapezoidInt of(int min, int max, int plateau) {
        return new TrapezoidInt(min, max, plateau);
    }
 
    public static IntProvider triangle(int range) {
        return of(-range, range, 0);
    }
 
    @Override
    public int sample(RandomSource random) {
        if (this.plateau == 0 && this.maxInclusive == -this.minInclusive) {
            return random.nextInt(this.maxInclusive + 1) - random.nextInt(this.maxInclusive + 1);
        } else {
            int range = this.maxInclusive - this.minInclusive;
            if (this.plateau == range) {
                return Mth.randomBetweenInclusive(random, this.minInclusive, this.maxInclusive);
            } else {
                int plateauStart = (range - this.plateau) / 2;
                int plateauEnd = range - plateauStart;
                return this.minInclusive + Mth.randomBetweenInclusive(random, 0, plateauEnd) + Mth.randomBetweenInclusive(random, 0, plateauStart);
            }
        }
    }
 
    @Override
    public MapCodec<TrapezoidInt> codec() {
        return MAP_CODEC;
    }
 
    @Override
    public String toString() {
        return "trapezoid(" + this.plateau + ") in [" + this.minInclusive + "-" + this.maxInclusive + "]";
    }
}

引用的其他类

  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.randomBetweenInclusive()
  • RandomSource

    • 引用位置: 参数
  • IntProvider

    • 引用位置: 实现/返回值