package emu.grasscutter.utils; import java.util.NavigableMap; import java.util.TreeMap; import java.util.concurrent.ThreadLocalRandom; public class WeightedList { private final NavigableMap map = new TreeMap(); private double total = 0; public WeightedList() { } public WeightedList add(double weight, E result) { if (weight <= 0) return this; total += weight; map.put(total, result); return this; } public E next() { double value = ThreadLocalRandom.current().nextDouble() * total; return map.higherEntry(value).getValue(); } public int size() { return map.size(); } }