cn.nukkit.item.randomitem.RandomItem Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powernukkit Show documentation
Show all versions of powernukkit Show documentation
A Minecraft Bedrock Edition server software implementation made in Java from scratch which supports all new features.
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));
}
}