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

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

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