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

cn.nukkit.plugin.js.JSFeatures Maven / Gradle / Ivy

There is a newer version: 1.20.40-r1
Show newest version
package cn.nukkit.plugin.js;

import cn.nukkit.plugin.js.feature.ConvertFeature;
import cn.nukkit.plugin.js.feature.TestFeature;
import cn.nukkit.plugin.js.feature.WsClientFeature;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public final class JSFeatures {
    public static final Map JS_FEATURE_MAP = new HashMap<>();
    public static final Map FEATURE_MODULE_MAP = new HashMap<>();
    public static final Int2ObjectOpenHashMap FEATURE_GENERATED_TMP_MAP = new Int2ObjectOpenHashMap<>();
    public static final AtomicInteger FEATURE_GENERATED_TMP_ID = new AtomicInteger(0);

    private JSFeatures() {
    }

    public static void registerFeature(JSFeature feature) {
        JS_FEATURE_MAP.put(feature.getName(), feature);
        for (String moduleName : feature.availableModuleNames()) {
            FEATURE_MODULE_MAP.put(moduleName, feature);
        }
    }

    public static JSFeature getFeature(String name) {
        return JS_FEATURE_MAP.get(name);
    }

    public static JSFeature getFeatureByModule(String moduleName) {
        return FEATURE_MODULE_MAP.get(moduleName);
    }

    public static boolean hasFeature(String name) {
        return JS_FEATURE_MAP.containsKey(name);
    }

    public static boolean hasFeatureByModule(String moduleName) {
        return FEATURE_MODULE_MAP.containsKey(moduleName);
    }

    public static void unregisterFeature(String name) {
        JS_FEATURE_MAP.remove(name);
        for (String moduleName : JS_FEATURE_MAP.get(name).availableModuleNames()) {
            FEATURE_MODULE_MAP.remove(moduleName);
        }
    }

    public static void clearFeatures() {
        JS_FEATURE_MAP.clear();
    }

    public static void initInternalFeatures() {
        registerFeature(new TestFeature());
        registerFeature(new WsClientFeature());
        registerFeature(new ConvertFeature());
    }
}