Search in sources :

Example 1 with ModulesLoadEvent

use of pl.themolka.arcade.module.ModulesLoadEvent in project Arcade2 by ShootGame.

the class ArcadePlugin method loadModules.

private void loadModules() {
    Node ignoredModules = this.getSettings().getData().child("ignored-modules");
    if (ignoredModules == null) {
        ignoredModules = Node.empty();
    }
    List<String> ignoredModuleList = new ArrayList<>();
    for (Node ignoredModule : ignoredModules.children("module")) {
        String id = ignoredModule.getValue();
        if (id != null && !id.isEmpty()) {
            ignoredModuleList.add(id);
        }
    }
    List<Module<?>> moduleList = new ArrayList<>();
    try (InputStream input = this.getClass().getClassLoader().getResourceAsStream(ModulesFile.DEFAULT_FILENAME)) {
        ModulesFile file = new ModulesFile(this, input);
        for (Module<?> module : file.getModules()) {
            ModuleInfo infoAnnotation = module.getClass().getAnnotation(ModuleInfo.class);
            if (infoAnnotation == null) {
                continue;
            }
            if (!ignoredModuleList.contains(infoAnnotation.id().toLowerCase())) {
                moduleList.add(module);
            }
        }
    } catch (DOMException | IOException ex) {
        ex.printStackTrace();
    }
    ModulesLoadEvent loadEvent = new ModulesLoadEvent(this, moduleList);
    this.getEventBus().publish(loadEvent);
    List<Module<?>> success = new ArrayList<>();
    for (Module<?> module : loadEvent.getModules()) {
        try {
            module.initialize(this);
            module.registerCommandObject(module);
            success.add(module);
        } catch (Throwable th) {
            this.getLogger().log(Level.SEVERE, "Could not load module '" + module.getId() + "': " + th.getMessage(), th);
        }
    }
    this.getLogger().info("Successfully loaded " + success.size() + " of " + loadEvent.getModules().size() + " available modules.");
    this.getModules().register(success.toArray(new Module<?>[success.size()]));
    Node globalModules = this.getSettings().getData().child("modules");
    if (globalModules == null) {
        globalModules = Node.empty();
    }
    for (Module<?> module : this.getModules().getModules()) {
        try {
            Node moduleNode = globalModules.child(module.getId());
            module.setGlobal(moduleNode != null);
            module.registerListeners();
            module.onEnable(moduleNode);
        } catch (Throwable th) {
            this.getLogger().log(Level.SEVERE, "Could not enable module '" + module.getId() + "': " + th.getMessage(), th);
        }
    }
}
Also used : ModulesLoadEvent(pl.themolka.arcade.module.ModulesLoadEvent) InputStream(java.io.InputStream) Node(pl.themolka.arcade.dom.Node) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DOMException(pl.themolka.arcade.dom.DOMException) ModulesFile(pl.themolka.arcade.module.ModulesFile) ModuleInfo(pl.themolka.arcade.module.ModuleInfo) Module(pl.themolka.arcade.module.Module)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 DOMException (pl.themolka.arcade.dom.DOMException)1 Node (pl.themolka.arcade.dom.Node)1 Module (pl.themolka.arcade.module.Module)1 ModuleInfo (pl.themolka.arcade.module.ModuleInfo)1 ModulesFile (pl.themolka.arcade.module.ModulesFile)1 ModulesLoadEvent (pl.themolka.arcade.module.ModulesLoadEvent)1