Search in sources :

Example 1 with Reloadable

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

the class StandardModule method registerCommandInterceptors.

private void registerCommandInterceptors() throws Exception {
    RegisterCommandInterceptors annotation = getClass().getAnnotation(RegisterCommandInterceptors.class);
    if (annotation != null) {
        // for each annotation, attempt to register the service.
        for (Class<? extends ICommandInterceptor> service : annotation.value()) {
            // create the impl
            ICommandInterceptor impl;
            try {
                impl = service.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                String error = "ERROR: Cannot instantiate ICommandInterceptor " + service.getName();
                Nucleus.getNucleus().getLogger().error(error);
                throw new IllegalStateException(error, e);
            }
            if (impl instanceof Reloadable) {
                Reloadable reloadable = (Reloadable) impl;
                Nucleus.getNucleus().registerReloadable(reloadable);
                reloadable.onReload();
            }
            AbstractCommand.registerInterceptor(impl);
        }
    }
}
Also used : RegisterCommandInterceptors(io.github.nucleuspowered.nucleus.internal.annotations.RegisterCommandInterceptors) ICommandInterceptor(io.github.nucleuspowered.nucleus.internal.command.ICommandInterceptor) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)

Example 2 with Reloadable

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

the class StandardModule method registerServices.

@SuppressWarnings("unchecked")
private void registerServices() throws Exception {
    RegisterService[] annotations = getClass().getAnnotationsByType(RegisterService.class);
    if (annotations != null && annotations.length > 0) {
        // for each annotation, attempt to register the service.
        for (RegisterService service : annotations) {
            Class<?> impl = service.value();
            // create the impl
            Object serviceImpl;
            try {
                serviceImpl = impl.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                String error = "ERROR: Cannot instantiate " + impl.getName();
                Nucleus.getNucleus().getLogger().error(error);
                throw new IllegalStateException(error, e);
            }
            Class<?> api = service.apiService();
            if (api != Object.class) {
                if (api.isInstance(serviceImpl)) {
                    // OK
                    register((Class) api, (Class) impl, serviceImpl);
                } else {
                    String error = "ERROR: " + api.getName() + " does not inherit from " + impl.getName();
                    Nucleus.getNucleus().getLogger().error(error);
                    throw new IllegalStateException(error);
                }
            } else {
                register((Class) impl, serviceImpl);
            }
            if (serviceImpl instanceof Reloadable) {
                Reloadable reloadable = (Reloadable) serviceImpl;
                Nucleus.getNucleus().registerReloadable(reloadable);
                reloadable.onReload();
            }
            if (serviceImpl instanceof ContextCalculator) {
                try {
                    // boolean matches(Context context, T calculable);
                    serviceImpl.getClass().getMethod("matches", Context.class, Subject.class);
                    // register it
                    ServiceChangeListener.getInstance().registerCalculator((ContextCalculator<Subject>) serviceImpl);
                } catch (NoSuchMethodException e) {
                // ignored
                }
            }
        }
    }
}
Also used : Subject(org.spongepowered.api.service.permission.Subject) RegisterService(io.github.nucleuspowered.nucleus.internal.annotations.RegisterService) ContextCalculator(org.spongepowered.api.service.context.ContextCalculator) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)

Example 3 with Reloadable

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

the class CommandBuilder method buildCommand.

<T extends AbstractCommand<?>> Optional<T> buildCommand(Class<T> commandClass, boolean rootCmd) {
    Optional<T> optionalCommand = getInstance(commandClass);
    if (!optionalCommand.isPresent()) {
        return Optional.empty();
    }
    T c = optionalCommand.get();
    try {
        c.setModuleName(moduleID, moduleName);
        c.setModuleCommands(commandSet);
        c.setCommandBuilder(this);
        c.postInit();
    } catch (Exception e) {
        e.printStackTrace();
        return Optional.empty();
    }
    // If we are using DocGen, add the command information to the system.
    plugin.getDocGenCache().ifPresent(x -> x.addCommand(moduleID, c));
    String commandSection = c.getAliases()[0].toLowerCase();
    sn.getNode(commandSection).setValue(c.getDefaults());
    if (plugin.getCommandsConfig().getCommandNode(commandSection).getNode("enabled").getBoolean(true)) {
        ConfigurationNode cn = plugin.getCommandsConfig().getCommandNode(commandSection);
        ConfigurationNode node = cn.getNode("aliases");
        if (node.getValue() == null) {
            cn.removeChild("aliases");
        }
        try {
            // Register the commands.
            if (rootCmd) {
                // This will return true for the first anyway
                String first = c.getAliases()[0];
                String[] aliases = Arrays.stream(c.getAliases()).filter(x -> x.equals(first) || node.getNode(x).getBoolean(true)).toArray(String[]::new);
                checkMapping(Sponge.getCommandManager().register(plugin, c, aliases).orElse(null), aliases);
            }
            // Register as another full blown command.
            for (String st : c.getRootCommandAliases()) {
                if (cn.getNode("aliases", st).getBoolean(true)) {
                    checkMapping(Sponge.getCommandManager().register(plugin, c, st).orElse(null), new String[] { st });
                // Sponge.getCommandManager().register(plugin, c, st);
                }
            }
            if (c instanceof Reloadable) {
                plugin.registerReloadable(((Reloadable) c));
            }
        } catch (Exception e) {
            throw new IllegalStateException(plugin.getMessageProvider().getMessageWithFormat("startup.commandfailiure", c.getAliases()[0], commandClass.getName()));
        }
        registeredCommands.add(c.getClass());
        return Optional.of(c);
    }
    return Optional.empty();
}
Also used : Arrays(java.util.Arrays) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) PluginInfo(io.github.nucleuspowered.nucleus.PluginInfo) NucleusPlugin(io.github.nucleuspowered.nucleus.NucleusPlugin) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) SkipOnError(io.github.nucleuspowered.nucleus.internal.annotations.SkipOnError) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) CommandMapping(org.spongepowered.api.command.CommandMapping) SimpleCommentedConfigurationNode(ninja.leaping.configurate.commented.SimpleCommentedConfigurationNode) Sets(com.google.common.collect.Sets) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) Optional(java.util.Optional) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Nullable(javax.annotation.Nullable) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) SimpleCommentedConfigurationNode(ninja.leaping.configurate.commented.SimpleCommentedConfigurationNode) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)

Example 4 with Reloadable

use of io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable 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

Reloadable (io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)4 Sets (com.google.common.collect.Sets)1 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)1 NucleusPlugin (io.github.nucleuspowered.nucleus.NucleusPlugin)1 PluginInfo (io.github.nucleuspowered.nucleus.PluginInfo)1 ListenerBase (io.github.nucleuspowered.nucleus.internal.ListenerBase)1 RegisterCommandInterceptors (io.github.nucleuspowered.nucleus.internal.annotations.RegisterCommandInterceptors)1 RegisterService (io.github.nucleuspowered.nucleus.internal.annotations.RegisterService)1 SkipOnError (io.github.nucleuspowered.nucleus.internal.annotations.SkipOnError)1 ICommandInterceptor (io.github.nucleuspowered.nucleus.internal.command.ICommandInterceptor)1 DocGenCache (io.github.nucleuspowered.nucleus.internal.docgen.DocGenCache)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Nullable (javax.annotation.Nullable)1 ConfigurationNode (ninja.leaping.configurate.ConfigurationNode)1 CommentedConfigurationNode (ninja.leaping.configurate.commented.CommentedConfigurationNode)1 SimpleCommentedConfigurationNode (ninja.leaping.configurate.commented.SimpleCommentedConfigurationNode)1 Sponge (org.spongepowered.api.Sponge)1 CommandMapping (org.spongepowered.api.command.CommandMapping)1