All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.nukkit.item.randomitem.Selector Maven / Gradle / Ivy

There is a newer version: 1.20.40-r1
Show newest version
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];
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy