use of io.github.nucleuspowered.nucleus.internal.command.AbstractCommand in project Nucleus by NucleusPowered.
the class AFKCommandInterceptor method onPostCommand.
@Override
public void onPostCommand(Class<? extends AbstractCommand<?>> commandClass, CommandSource source, CommandContext context, CommandResult result) {
if (this.send && result.getSuccessCount().orElse(0) > 0 && commandClass.isAnnotationPresent(NotifyIfAFK.class)) {
NotifyIfAFK annotation = commandClass.getAnnotation(NotifyIfAFK.class);
Cause cause = CauseStackHelper.createCause(source);
for (String key : annotation.value()) {
context.getAll(key).stream().filter(x -> x instanceof User).map(x -> ((User) x).getPlayer().orElse(null)).filter(Objects::nonNull).filter(this.handler::isAFK).forEach(x -> {
Text messageToSend = this.message == null ? null : message.getForCommandSource(x);
AFKEvents.Notify event = new AFKEvents.Notify(x, messageToSend, cause);
Sponge.getEventManager().post(event);
event.getMessage().ifPresent(source::sendMessage);
});
}
}
}
use of io.github.nucleuspowered.nucleus.internal.command.AbstractCommand in project Nucleus by NucleusPowered.
the class StandardModule method loadCommands.
@SuppressWarnings("unchecked")
private void loadCommands() {
Set<Class<? extends AbstractCommand<?>>> cmds;
if (this.msls != null) {
cmds = new HashSet<>();
List<String> l = this.msls.get(Constants.COMMAND);
if (l == null) {
return;
}
for (String s : l) {
try {
checkPlatformOpt((Class<? extends AbstractCommand<?>>) Class.forName(s)).ifPresent(cmds::add);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
} else {
cmds = new HashSet<>(performFilter(getStreamForModule(AbstractCommand.class).map(x -> (Class<? extends AbstractCommand<?>>) x)).collect(Collectors.toSet()));
// Find all commands that are also scannable.
performFilter(plugin.getModuleContainer().getLoadedClasses().stream().filter(x -> x.getPackage().getName().startsWith(packageName)).filter(x -> x.isAnnotationPresent(Scan.class)).flatMap(x -> Arrays.stream(x.getDeclaredClasses())).filter(AbstractCommand.class::isAssignableFrom).map(x -> (Class<? extends AbstractCommand<?>>) x)).forEach(cmds::add);
}
// We all love the special injector. We just want to provide the module with more commands, in case it needs a child.
Set<Class<? extends AbstractCommand>> commandBases = cmds.stream().filter(x -> {
RegisterCommand rc = x.getAnnotation(RegisterCommand.class);
return (rc != null && rc.subcommandOf().equals(AbstractCommand.class));
}).collect(Collectors.toSet());
CommandBuilder builder = new CommandBuilder(plugin, cmds, moduleId, moduleName);
commandBases.forEach(builder::buildCommand);
try {
commandsConfig.mergeDefaults(builder.getNodeToMerge());
commandsConfig.save();
} catch (Exception e) {
plugin.getLogger().error("Could not save defaults.");
e.printStackTrace();
}
}
use of io.github.nucleuspowered.nucleus.internal.command.AbstractCommand in project Nucleus by NucleusPowered.
the class DocGenCache method addCommand.
public void addCommand(final String moduleID, final AbstractCommand<?> abstractCommand) {
if (abstractCommand.getClass().isAnnotationPresent(NoDocumentation.class)) {
return;
}
CommandDoc cmd = new CommandDoc();
String cmdPath = abstractCommand.getCommandPath().replaceAll("\\.", " ");
cmd.setCommandName(cmdPath);
cmd.setAliases(String.join(", ", Lists.newArrayList(abstractCommand.getAliases())));
if (abstractCommand.getRootCommandAliases().length > 0) {
cmd.setRootAliases(String.join(", ", Arrays.asList(abstractCommand.getRootCommandAliases())));
}
cmd.setPermissionbase(abstractCommand.getPermissionHandler().getBase());
Class<? extends AbstractCommand> cac = abstractCommand.getClass();
Permissions s = cac.getAnnotation(Permissions.class);
if (s == null) {
cmd.setDefaultLevel(cac.isAnnotationPresent(NoPermissions.class) ? SuggestedLevel.USER.name() : SuggestedLevel.ADMIN.name());
} else {
cmd.setDefaultLevel(s.suggestedLevel().name());
}
cmd.setModule(moduleID);
if (!cac.isAnnotationPresent(NoModifiers.class)) {
cmd.setCooldown(!cac.isAnnotationPresent(NoCooldown.class));
cmd.setCost(!cac.isAnnotationPresent(NoCost.class));
cmd.setWarmup(!cac.isAnnotationPresent(NoWarmup.class));
}
cmd.setSince(cac.getAnnotation(Since.class));
String desc = abstractCommand.getDescription();
if (!desc.contains(" ")) {
logger.warn("Cannot generate description for: " + abstractCommand.getCommandPath() + ": " + desc);
}
cmd.setOneLineDescription(desc);
String extendedDescription = abstractCommand.getExtendedDescription().replace("\n", "|br|").replace("\"", """);
if (!extendedDescription.isEmpty()) {
cmd.setExtendedDescription(extendedDescription);
}
List<PermissionDoc> lp = new ArrayList<>();
abstractCommand.getPermissionHandler().getSuggestedPermissions().forEach((k, v) -> lp.add(addPermissionDocs(moduleID, k, v)));
cmd.setPermissions(lp);
cmd.setUsageString(abstractCommand.getUsageString(Sponge.getServer().getConsole()));
cmd.setSubcommands(abstractCommand.getChildrenUsage(Sponge.getServer().getConsole()).map(Text::toPlain).orElse(""));
cmd.setSimpleUsage(abstractCommand.getSimpleUsage(Sponge.getServer().getConsole()));
// Essentials
EssentialsEquivalent ee = abstractCommand.getClass().getAnnotation(EssentialsEquivalent.class);
if (ee != null) {
List<String> ss = Arrays.asList(ee.value());
cmd.setEssentialsEquivalents(ss);
cmd.setEssNotes(ee.notes());
cmd.setExactEssEquiv(ee.isExact());
EssentialsDoc doc = new EssentialsDoc();
doc.setEssentialsCommands(ss);
int i = cmdPath.lastIndexOf(" ");
String c;
if (i > -1) {
c = cmdPath.substring(0, i) + " ";
} else {
c = "";
}
List<String> a = Lists.newArrayList(abstractCommand.getAliases()).stream().map(x -> c + x).collect(Collectors.toList());
if (abstractCommand.getRootCommandAliases().length > 0) {
a.addAll(Arrays.asList(abstractCommand.getRootCommandAliases()));
}
doc.setNucleusEquiv(a);
doc.setExact(ee.isExact());
doc.setNotes(ee.notes());
essentialsDocs.add(doc);
}
commandDocs.add(cmd);
}
use of io.github.nucleuspowered.nucleus.internal.command.AbstractCommand in project Nucleus by NucleusPowered.
the class PermissionRegistry method getPermissionsForNucleusCommand.
public CommandPermissionHandler getPermissionsForNucleusCommand(Class<? extends AbstractCommand> command) {
if (this.serviceRegistry.containsKey(command)) {
return this.serviceRegistry.get(command);
}
PermissionsFrom p = command.getAnnotation(PermissionsFrom.class);
if (p != null && p.value() != AbstractCommand.class) {
return getPermissionsForNucleusCommand(p.value());
}
CommandPermissionHandler handler = new CommandPermissionHandler(command, Nucleus.getNucleus());
serviceRegistry.put(command, handler);
return handler;
}
Aggregations