Weighted.java

net.minecraft.util.random.Weighted

信息

  • 全限定名:net.minecraft.util.random.Weighted
  • 类型:public record
  • 包:net.minecraft.util.random
  • 源码路径:src/main/java/net/minecraft/util/random/Weighted.java
  • 起始行号:L17
  • 职责:

    TODO

字段/常量

  • LOGGER
    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L18
    • 说明:

      TODO

内部类/嵌套类型

构造器

public Weighted(T value, int weight) @ L20

  • 构造器名:Weighted
  • 源码定位:L20
  • 修饰符:public

参数:

  • value: T
  • weight: int

说明:

TODO

方法

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

public static <E> Codec<Weighted<E>> codec(Codec<E> elementCodec) @ L33

  • 方法名:codec
  • 源码定位:L33
  • 返回类型: Codec<Weighted>
  • 修饰符:public static

参数:

  • elementCodec: Codec

说明:

TODO

public static <E> Codec<Weighted<E>> codec(MapCodec<E> elementCodec) @ L37

  • 方法名:codec
  • 源码定位:L37
  • 返回类型: Codec<Weighted>
  • 修饰符:public static

参数:

  • elementCodec: MapCodec

说明:

TODO

public static <B extends ByteBuf,T> StreamCodec<B,Weighted<T>> streamCodec(StreamCodec<B,T> valueCodec) @ L47

  • 方法名:streamCodec
  • 源码定位:L47
  • 返回类型:<B extends ByteBuf,T> StreamCodec<B,Weighted>
  • 修饰符:public static

参数:

  • valueCodec: StreamCodec<B,T>

说明:

TODO

public <U> Weighted<U> map(Function<T,U> function) @ L51

  • 方法名:map
  • 源码定位:L51
  • 返回类型: Weighted
  • 修饰符:public

参数:

  • function: Function<T,U>

说明:

TODO

代码

public record Weighted<T>(T value, int weight) {
    private static final Logger LOGGER = LogUtils.getLogger();
 
    public Weighted(T value, int weight) {
        if (weight < 0) {
            throw (IllegalArgumentException)Util.pauseInIde(new IllegalArgumentException("Weight should be >= 0"));
        } else {
            if (weight == 0 && SharedConstants.IS_RUNNING_IN_IDE) {
                LOGGER.warn("Found 0 weight, make sure this is intentional!");
            }
 
            this.value = value;
            this.weight = weight;
        }
    }
 
    public static <E> Codec<Weighted<E>> codec(Codec<E> elementCodec) {
        return codec(elementCodec.fieldOf("data"));
    }
 
    public static <E> Codec<Weighted<E>> codec(MapCodec<E> elementCodec) {
        return RecordCodecBuilder.create(
            i -> i.group(
                    elementCodec.forGetter((Function<Weighted<E>, E>)(Weighted::value)),
                    ExtraCodecs.NON_NEGATIVE_INT.fieldOf("weight").forGetter(Weighted::weight)
                )
                .apply(i, (BiFunction<E, Integer, Weighted<E>>)(Weighted::new))
        );
    }
 
    public static <B extends ByteBuf, T> StreamCodec<B, Weighted<T>> streamCodec(StreamCodec<B, T> valueCodec) {
        return StreamCodec.composite(valueCodec, Weighted::value, ByteBufCodecs.VAR_INT, Weighted::weight, Weighted::new);
    }
 
    public <U> Weighted<U> map(Function<T, U> function) {
        return new Weighted<>(function.apply(this.value()), this.weight);
    }
}

引用的其他类

  • StreamCodec

    • 引用位置: 参数/方法调用/返回值
    • 关联成员: StreamCodec.composite()
  • Util

    • 引用位置: 方法调用
    • 关联成员: Util.pauseInIde()