cn.nukkit.item.randomitem.Selector 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.Map;
/**
* @author Snake1999
* @since 2016/1/15
*/
public class Selector {
private Selector parent;
public Selector(Selector parent) {
this.setParent(parent);
}
public Selector setParent(Selector parent) {
this.parent = parent;
return parent;
}
public Selector getParent() {
return parent;
}
public Object select() {
return this;
}
public static Selector selectRandom(Map selectorChanceMap) {
final float[] totalChance = {0};
selectorChanceMap.values().forEach(f -> totalChance[0] += f);
float resultChance = (float) (Math.random() * totalChance[0]);
final float[] flag = {0};
final boolean[] found = {false};
final Selector[] temp = {null};
selectorChanceMap.forEach((o, f) -> {
flag[0] += f;
if (flag[0] > resultChance && !found[0]) {
temp[0] = o;
found[0] = true;
}
});
return temp[0];
}
}