package net.minecraft.util.random; import java.util.List; import java.util.Optional; import net.minecraft.Util; import net.minecraft.util.RandomSource; public class WeightedRandom { private WeightedRandom() { } public static int getTotalWeight(List entries) { long l = 0L; for (WeightedEntry weightedEntry : entries) { l += weightedEntry.getWeight().asInt(); } if (l > 2147483647L) { throw new IllegalArgumentException("Sum of weights must be <= 2147483647"); } else { return (int)l; } } public static Optional getRandomItem(RandomSource random, List entries, int totalWeight) { if (totalWeight < 0) { throw (IllegalArgumentException)Util.pauseInIde((T)(new IllegalArgumentException("Negative total weight in getRandomItem"))); } else if (totalWeight == 0) { return Optional.empty(); } else { int i = random.nextInt(totalWeight); return getWeightedItem(entries, i); } } public static Optional getWeightedItem(List entries, int weightedIndex) { for (T weightedEntry : entries) { weightedIndex -= weightedEntry.getWeight().asInt(); if (weightedIndex < 0) { return Optional.of(weightedEntry); } } return Optional.empty(); } public static Optional getRandomItem(RandomSource random, List entries) { return getRandomItem(random, entries, getTotalWeight(entries)); } }