minecraft-src/net/minecraft/util/random/SimpleWeightedRandomList.java
2025-07-04 03:15:13 +03:00

56 lines
1.8 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;
import net.minecraft.util.random.WeightedEntry.Wrapper;
public class SimpleWeightedRandomList<E> extends WeightedRandomList<Wrapper<E>> {
public static <E> Codec<SimpleWeightedRandomList<E>> wrappedCodecAllowingEmpty(Codec<E> codec) {
return Wrapper.codec(codec).listOf().xmap(SimpleWeightedRandomList::new, WeightedRandomList::unwrap);
}
public static <E> Codec<SimpleWeightedRandomList<E>> wrappedCodec(Codec<E> elementCodec) {
return ExtraCodecs.nonEmptyList(Wrapper.codec(elementCodec).listOf()).xmap(SimpleWeightedRandomList::new, WeightedRandomList::unwrap);
}
SimpleWeightedRandomList(List<? extends Wrapper<E>> list) {
super(list);
}
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(Wrapper::data);
}
public static class Builder<E> {
private final ImmutableList.Builder<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());
}
}
}