minecraft-src/net/minecraft/util/random/WeightedEntry.java
2025-07-04 01:41:11 +03:00

45 lines
1.1 KiB
Java

package net.minecraft.util.random;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
public interface WeightedEntry {
Weight getWeight();
static <T> WeightedEntry.Wrapper<T> 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>(T data, Weight weight) implements WeightedEntry {
@Override
public Weight getWeight() {
return this.weight;
}
public static <E> Codec<WeightedEntry.Wrapper<E>> codec(Codec<E> 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)
);
}
}
}