package net.minecraft.util.random; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; public interface WeightedEntry { Weight getWeight(); static WeightedEntry.Wrapper wrap(T data, int weight) { return new WeightedEntry.Wrapper<>(data, Weight.of(weight)); } public static class IntrusiveBase implements WeightedEntry { private final Weight weight; public IntrusiveBase(int weight) { this.weight = Weight.of(weight); } public IntrusiveBase(Weight weight) { this.weight = weight; } @Override public Weight getWeight() { return this.weight; } } public record Wrapper(T data, Weight weight) implements WeightedEntry { @Override public Weight getWeight() { return this.weight; } public static Codec> codec(Codec elementCodec) { return RecordCodecBuilder.create( instance -> instance.group( elementCodec.fieldOf("data").forGetter(WeightedEntry.Wrapper::data), Weight.CODEC.fieldOf("weight").forGetter(WeightedEntry.Wrapper::weight) ) .apply(instance, WeightedEntry.Wrapper::new) ); } } }