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

55 lines
1.9 KiB
Java

package net.minecraft.util.random;
import com.google.common.collect.ImmutableList;
import com.mojang.serialization.Codec;
import java.util.List;
import java.util.Optional;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.util.RandomSource;
public class SimpleWeightedRandomList<E> extends WeightedRandomList<WeightedEntry.Wrapper<E>> {
public static <E> Codec<SimpleWeightedRandomList<E>> wrappedCodecAllowingEmpty(Codec<E> codec) {
return WeightedEntry.Wrapper.codec(codec).listOf().xmap(SimpleWeightedRandomList::new, WeightedRandomList::unwrap);
}
public static <E> Codec<SimpleWeightedRandomList<E>> wrappedCodec(Codec<E> elementCodec) {
return ExtraCodecs.nonEmptyList(WeightedEntry.Wrapper.codec(elementCodec).listOf()).xmap(SimpleWeightedRandomList::new, WeightedRandomList::unwrap);
}
SimpleWeightedRandomList(List<? extends WeightedEntry.Wrapper<E>> items) {
super(items);
}
public static <E> SimpleWeightedRandomList.Builder<E> builder() {
return new SimpleWeightedRandomList.Builder<>();
}
public static <E> SimpleWeightedRandomList<E> empty() {
return new SimpleWeightedRandomList<>(List.of());
}
public static <E> SimpleWeightedRandomList<E> single(E data) {
return new SimpleWeightedRandomList<>(List.of(WeightedEntry.wrap(data, 1)));
}
public Optional<E> getRandomValue(RandomSource random) {
return this.getRandom(random).map(WeightedEntry.Wrapper::data);
}
public static class Builder<E> {
private final ImmutableList.Builder<WeightedEntry.Wrapper<E>> result = ImmutableList.builder();
public SimpleWeightedRandomList.Builder<E> add(E data) {
return this.add(data, 1);
}
public SimpleWeightedRandomList.Builder<E> add(E data, int weight) {
this.result.add(WeightedEntry.wrap(data, weight));
return this;
}
public SimpleWeightedRandomList<E> build() {
return new SimpleWeightedRandomList<>(this.result.build());
}
}
}