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 extends WeightedRandomList> { public static Codec> wrappedCodecAllowingEmpty(Codec codec) { return WeightedEntry.Wrapper.codec(codec).listOf().xmap(SimpleWeightedRandomList::new, WeightedRandomList::unwrap); } public static Codec> wrappedCodec(Codec elementCodec) { return ExtraCodecs.nonEmptyList(WeightedEntry.Wrapper.codec(elementCodec).listOf()).xmap(SimpleWeightedRandomList::new, WeightedRandomList::unwrap); } SimpleWeightedRandomList(List> items) { super(items); } public static SimpleWeightedRandomList.Builder builder() { return new SimpleWeightedRandomList.Builder<>(); } public static SimpleWeightedRandomList empty() { return new SimpleWeightedRandomList<>(List.of()); } public static SimpleWeightedRandomList single(E data) { return new SimpleWeightedRandomList<>(List.of(WeightedEntry.wrap(data, 1))); } public Optional getRandomValue(RandomSource random) { return this.getRandom(random).map(WeightedEntry.Wrapper::data); } public static class Builder { private final ImmutableList.Builder> result = ImmutableList.builder(); public SimpleWeightedRandomList.Builder add(E data) { return this.add(data, 1); } public SimpleWeightedRandomList.Builder add(E data, int weight) { this.result.add(WeightedEntry.wrap(data, weight)); return this; } public SimpleWeightedRandomList build() { return new SimpleWeightedRandomList<>(this.result.build()); } } }