Search in sources :

Example 1 with ListenerBase

use of io.github.nucleuspowered.nucleus.internal.ListenerBase in project Nucleus by NucleusPowered.

the class StandardModule method loadEvents.

@SuppressWarnings("unchecked")
private void loadEvents() {
    Set<Class<? extends ListenerBase>> listenersToLoad;
    if (msls != null) {
        listenersToLoad = new HashSet<>();
        List<String> l = this.msls.get(Constants.LISTENER);
        if (l == null) {
            return;
        }
        for (String s : l) {
            try {
                checkPlatformOpt((Class<? extends ListenerBase>) Class.forName(s)).ifPresent(listenersToLoad::add);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    } else {
        listenersToLoad = getStreamForModule(ListenerBase.class).collect(Collectors.toSet());
    }
    Optional<DocGenCache> docGenCache = plugin.getDocGenCache();
    listenersToLoad.stream().map(x -> this.getInstance(x, true)).filter(Objects::nonNull).forEach(c -> {
        // Register suggested permissions
        c.getPermissions().forEach((k, v) -> plugin.getPermissionRegistry().registerOtherPermission(k, v));
        docGenCache.ifPresent(x -> x.addPermissionDocs(moduleId, c.getPermissions()));
        if (c instanceof ListenerBase.Conditional) {
            // Add reloadable to load in the listener dynamically if required.
            Reloadable tae = () -> {
                Sponge.getEventManager().unregisterListeners(c);
                if (c instanceof Reloadable) {
                    ((Reloadable) c).onReload();
                }
                if (((ListenerBase.Conditional) c).shouldEnable()) {
                    Sponge.getEventManager().registerListeners(plugin, c);
                }
            };
            plugin.registerReloadable(tae);
            try {
                tae.onReload();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (c instanceof Reloadable) {
            plugin.registerReloadable(((Reloadable) c));
            Sponge.getEventManager().registerListeners(plugin, c);
        } else {
            Sponge.getEventManager().registerListeners(plugin, c);
        }
    });
}
Also used : DocGenCache(io.github.nucleuspowered.nucleus.internal.docgen.DocGenCache) ListenerBase(io.github.nucleuspowered.nucleus.internal.ListenerBase) MissingDependencyException(uk.co.drnaylor.quickstart.exceptions.MissingDependencyException) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)

Aggregations

ListenerBase (io.github.nucleuspowered.nucleus.internal.ListenerBase)1 DocGenCache (io.github.nucleuspowered.nucleus.internal.docgen.DocGenCache)1 Reloadable (io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)1 MissingDependencyException (uk.co.drnaylor.quickstart.exceptions.MissingDependencyException)1