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);
}
}
}
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
}
}
}
}
}
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();
}
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);
}
});
}
Aggregations