Search in sources :

Example 1 with AbstractCommand

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);
            });
        }
    }
}
Also used : NotifyIfAFK(io.github.nucleuspowered.nucleus.internal.annotations.command.NotifyIfAFK) AFKConfig(io.github.nucleuspowered.nucleus.modules.afk.config.AFKConfig) CommandResult(org.spongepowered.api.command.CommandResult) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) User(org.spongepowered.api.entity.living.player.User) CommandSource(org.spongepowered.api.command.CommandSource) CauseStackHelper(io.github.nucleuspowered.nucleus.util.CauseStackHelper) Sponge(org.spongepowered.api.Sponge) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) Objects(java.util.Objects) Cause(org.spongepowered.api.event.cause.Cause) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) ICommandInterceptor(io.github.nucleuspowered.nucleus.internal.command.ICommandInterceptor) AFKHandler(io.github.nucleuspowered.nucleus.modules.afk.handlers.AFKHandler) AFKEvents(io.github.nucleuspowered.nucleus.modules.afk.events.AFKEvents) AFKConfigAdapter(io.github.nucleuspowered.nucleus.modules.afk.config.AFKConfigAdapter) Nullable(javax.annotation.Nullable) User(org.spongepowered.api.entity.living.player.User) Cause(org.spongepowered.api.event.cause.Cause) Objects(java.util.Objects) Text(org.spongepowered.api.text.Text) AFKEvents(io.github.nucleuspowered.nucleus.modules.afk.events.AFKEvents) NotifyIfAFK(io.github.nucleuspowered.nucleus.internal.annotations.command.NotifyIfAFK)

Example 2 with AbstractCommand

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();
    }
}
Also used : Arrays(java.util.Arrays) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) NucleusPlugin(io.github.nucleuspowered.nucleus.NucleusPlugin) BiFunction(java.util.function.BiFunction) RequiresPlatform(io.github.nucleuspowered.nucleus.internal.annotations.RequiresPlatform) Tokens(io.github.nucleuspowered.nucleus.internal.text.Tokens) Map(java.util.Map) Constants(io.github.nucleuspowered.nucleus.internal.Constants) ServiceChangeListener(io.github.nucleuspowered.nucleus.internal.permissions.ServiceChangeListener) Subject(org.spongepowered.api.service.permission.Subject) Method(java.lang.reflect.Method) AbstractConfigAdapter(uk.co.drnaylor.quickstart.config.AbstractConfigAdapter) CommandPermissionHandler(io.github.nucleuspowered.nucleus.internal.CommandPermissionHandler) User(org.spongepowered.api.entity.living.player.User) ImmutableMap(com.google.common.collect.ImmutableMap) CommandSource(org.spongepowered.api.command.CommandSource) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) ContextCalculator(org.spongepowered.api.service.context.ContextCalculator) Stream(java.util.stream.Stream) RegisterService(io.github.nucleuspowered.nucleus.internal.annotations.RegisterService) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) ServerOnly(io.github.nucleuspowered.nucleus.internal.annotations.ServerOnly) DocGenCache(io.github.nucleuspowered.nucleus.internal.docgen.DocGenCache) SkipOnError(io.github.nucleuspowered.nucleus.internal.annotations.SkipOnError) Platform(org.spongepowered.api.Platform) Scan(io.github.nucleuspowered.nucleus.internal.annotations.command.Scan) HashSet(java.util.HashSet) Text(org.spongepowered.api.text.Text) ICommandInterceptor(io.github.nucleuspowered.nucleus.internal.command.ICommandInterceptor) Task(org.spongepowered.api.scheduler.Task) ListenerBase(io.github.nucleuspowered.nucleus.internal.ListenerBase) RegisterCommandInterceptors(io.github.nucleuspowered.nucleus.internal.annotations.RegisterCommandInterceptors) ModuleData(uk.co.drnaylor.quickstart.annotations.ModuleData) Nullable(javax.annotation.Nullable) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandBuilder(io.github.nucleuspowered.nucleus.internal.command.CommandBuilder) Context(org.spongepowered.api.service.context.Context) CommandsConfig(io.github.nucleuspowered.nucleus.config.CommandsConfig) InternalServiceManager(io.github.nucleuspowered.nucleus.internal.InternalServiceManager) RequireExistenceOf(io.github.nucleuspowered.nucleus.internal.annotations.RequireExistenceOf) Store(io.github.nucleuspowered.nucleus.annotationprocessor.Store) Module(uk.co.drnaylor.quickstart.Module) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) TimeUnit(java.util.concurrent.TimeUnit) SeenHandler(io.github.nucleuspowered.nucleus.modules.playerinfo.handlers.SeenHandler) MissingDependencyException(uk.co.drnaylor.quickstart.exceptions.MissingDependencyException) TaskBase(io.github.nucleuspowered.nucleus.internal.TaskBase) BasicSeenInformationProvider(io.github.nucleuspowered.nucleus.modules.playerinfo.handlers.BasicSeenInformationProvider) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) MissingDependencyException(uk.co.drnaylor.quickstart.exceptions.MissingDependencyException) CommandBuilder(io.github.nucleuspowered.nucleus.internal.command.CommandBuilder)

Example 3 with AbstractCommand

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("\"", "&quot;");
    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);
}
Also used : ConfigurationOptions(ninja.leaping.configurate.ConfigurationOptions) Arrays(java.util.Arrays) NoCost(io.github.nucleuspowered.nucleus.internal.annotations.command.NoCost) NoPermissions(io.github.nucleuspowered.nucleus.internal.annotations.command.NoPermissions) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) Since(io.github.nucleuspowered.nucleus.internal.annotations.Since) TypeToken(com.google.common.reflect.TypeToken) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) NucleusConfigAdapter(io.github.nucleuspowered.nucleus.internal.qsml.NucleusConfigAdapter) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) AbstractConfigAdapter(uk.co.drnaylor.quickstart.config.AbstractConfigAdapter) NoCooldown(io.github.nucleuspowered.nucleus.internal.annotations.command.NoCooldown) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) HoconConfigurationLoader(ninja.leaping.configurate.hocon.HoconConfigurationLoader) CommentedConfigurationNode(ninja.leaping.configurate.commented.CommentedConfigurationNode) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) Collection(java.util.Collection) NoDocumentation(io.github.nucleuspowered.nucleus.internal.annotations.command.NoDocumentation) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) NoWarmup(io.github.nucleuspowered.nucleus.internal.annotations.command.NoWarmup) ConfigurableModule(io.github.nucleuspowered.nucleus.internal.qsml.module.ConfigurableModule) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) ConfigurateHelper(io.github.nucleuspowered.nucleus.configurate.ConfigurateHelper) ArrayList(java.util.ArrayList) Text(org.spongepowered.api.text.Text) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) NoPermissions(io.github.nucleuspowered.nucleus.internal.annotations.command.NoPermissions) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) Since(io.github.nucleuspowered.nucleus.internal.annotations.Since)

Example 4 with AbstractCommand

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;
}
Also used : AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) PermissionsFrom(io.github.nucleuspowered.nucleus.internal.annotations.command.PermissionsFrom)

Aggregations

AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)4 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)3 Sponge (org.spongepowered.api.Sponge)3 Text (org.spongepowered.api.text.Text)3 ICommandInterceptor (io.github.nucleuspowered.nucleus.internal.command.ICommandInterceptor)2 Reloadable (io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable)2 Objects (java.util.Objects)2 Nullable (javax.annotation.Nullable)2 CommandSource (org.spongepowered.api.command.CommandSource)2 User (org.spongepowered.api.entity.living.player.User)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 TypeToken (com.google.common.reflect.TypeToken)1 NucleusPlugin (io.github.nucleuspowered.nucleus.NucleusPlugin)1 Store (io.github.nucleuspowered.nucleus.annotationprocessor.Store)1 NucleusTextTemplate (io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate)1 CommandsConfig (io.github.nucleuspowered.nucleus.config.CommandsConfig)1 ConfigurateHelper (io.github.nucleuspowered.nucleus.configurate.ConfigurateHelper)1 CommandPermissionHandler (io.github.nucleuspowered.nucleus.internal.CommandPermissionHandler)1