cn.nukkit.item.randomitem.RandomItem Maven / Gradle / Ivy
package cn.nukkit.item.randomitem;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* @author Snake1999
* @since 2016/1/15
*/
public final class RandomItem {
private static final Map selectors = new HashMap<>();
public static final Selector ROOT = new Selector(null);
public static Selector putSelector(Selector selector) {
return putSelector(selector, 1);
}
public static Selector putSelector(Selector selector, float chance) {
if (selector.getParent() == null) selector.setParent(ROOT);
selectors.put(selector, chance);
return selector;
}
static Object selectFrom(Selector selector) {
Objects.requireNonNull(selector);
Map child = new HashMap<>();
selectors.forEach((s, f) -> {
if (s.getParent() == selector) child.put(s, f);
});
if (child.size() == 0) return selector.select();
return selectFrom(Selector.selectRandom(child));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy