package net.minecraft.util.random; import java.util.List; import java.util.Optional; import java.util.function.ToIntFunction; import net.minecraft.Util; import net.minecraft.util.RandomSource; public class WeightedRandom { private WeightedRandom() { } public static int getTotalWeight(List p_146313_, ToIntFunction p_394201_) { long i = 0L; for (T t : p_146313_) { i += p_394201_.applyAsInt(t); } if (i > 2147483647L) { throw new IllegalArgumentException("Sum of weights must be <= 2147483647"); } else { return (int)i; } } public static Optional getRandomItem(RandomSource p_216826_, List p_216827_, int p_216828_, ToIntFunction p_392166_) { if (p_216828_ < 0) { throw (IllegalArgumentException)Util.pauseInIde(new IllegalArgumentException("Negative total weight in getRandomItem")); } else if (p_216828_ == 0) { return Optional.empty(); } else { int i = p_216826_.nextInt(p_216828_); return getWeightedItem(p_216827_, i, p_392166_); } } public static Optional getWeightedItem(List p_146315_, int p_146316_, ToIntFunction p_394629_) { for (T t : p_146315_) { p_146316_ -= p_394629_.applyAsInt(t); if (p_146316_ < 0) { return Optional.of(t); } } return Optional.empty(); } public static Optional getRandomItem(RandomSource p_216823_, List p_216824_, ToIntFunction p_392808_) { return getRandomItem(p_216823_, p_216824_, getTotalWeight(p_216824_, p_392808_), p_392808_); } }