Search in sources :

Example 6 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project SkinsRestorerX by DoNotSpamPls.

the class SkinsRestorer method onInitialize.

@Listener
public void onInitialize(GameInitializationEvent e) {
    instance = this;
    directory = Sponge.getGame().getConfigManager().getPluginConfig(this).getDirectory().toString();
    try {
        reloadConfigs();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    if (!Sponge.getServer().getOnlineMode())
        Sponge.getEventManager().registerListener(this, ClientConnectionEvent.Login.class, new LoginListener());
    CommandSpec skinCommand = CommandSpec.builder().description(Text.of("Set your skin")).arguments(GenericArguments.onlyOne(GenericArguments.string(Text.of("skin")))).executor(new SkinCommand()).build();
    Sponge.getCommandManager().register(this, skinCommand, "skin");
}
Also used : LoginListener(skinsrestorer.sponge.listeners.LoginListener) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) SkinCommand(skinsrestorer.sponge.commands.SkinCommand) LoginListener(skinsrestorer.sponge.listeners.LoginListener) Listener(org.spongepowered.api.event.Listener)

Example 7 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project AdamantineShield by Karanum.

the class AdamantineShield method registerCommands.

private void registerCommands(CommandManager man) {
    CommandSpec filterCommand = CommandSpec.builder().permission(Permissions.FILTER.get()).arguments(GenericArguments.allOf(GenericArguments.string(Text.of("filter")))).executor(new CommandFilter(this)).build();
    CommandSpec helpCommand = CommandSpec.builder().executor(new CommandMain()).build();
    CommandSpec inspectCommand = CommandSpec.builder().permission(Permissions.LOOKUP.get()).executor(new CommandInspect(this)).build();
    CommandSpec lookupCommand = CommandSpec.builder().permission(Permissions.LOOKUP.get()).arguments(GenericArguments.allOf(GenericArguments.string(Text.of("filter")))).executor(new CommandLookup(this)).build();
    CommandSpec nextPageCommand = CommandSpec.builder().permission(Permissions.LOOKUP.get()).executor(new CommandNextPage()).build();
    CommandSpec pageCommand = CommandSpec.builder().permission(Permissions.LOOKUP.get()).arguments(GenericArguments.onlyOne(GenericArguments.integer(Text.of("page")))).executor(new CommandPage()).build();
    CommandSpec prevPageCommand = CommandSpec.builder().permission(Permissions.LOOKUP.get()).executor(new CommandPrevPage()).build();
    CommandSpec purgeCommand = CommandSpec.builder().permission(Permissions.PURGE.get()).arguments(GenericArguments.onlyOne(new TimeStringArgument(Text.of("time")))).executor(new CommandPurge(this)).build();
    // CommandSpec reloadCommand = CommandSpec.builder()
    // .permission(Permissions.RELOAD.get())
    // .executor(new CommandReload())
    // .build();
    // CommandSpec rollbackCommand = CommandSpec.builder()
    // .permission(Permissions.ROLLBACK.get())
    // .arguments(GenericArguments.allOf(GenericArguments.string(Text.of("filter"))))
    // .executor(new CommandRollback(this))
    // .build();
    // 
    // CommandSpec undoCommand = CommandSpec.builder()
    // .permission(Permissions.UNDO.get())
    // .arguments(GenericArguments.allOf(GenericArguments.string(Text.of("filter"))))
    // .executor(new CommandUndo(this))
    // .build();
    CommandSpec parentCommand = CommandSpec.builder().description(Text.of("Main command for AdamantineShield")).child(inspectCommand, "inspect", "i").child(lookupCommand, "lookup", "l").child(filterCommand, "filter", "f").child(pageCommand, "page", "p").child(nextPageCommand, "nextpage", "next").child(prevPageCommand, "prevpage", "prev").child(purgeCommand, "purge").child(helpCommand, "help", "?").executor(new CommandMain()).build();
    man.register(this, parentCommand, "ashield", "as");
}
Also used : CommandSpec(org.spongepowered.api.command.spec.CommandSpec) TimeStringArgument(com.karanumcoding.adamantineshield.commands.arguments.TimeStringArgument)

Example 8 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project LanternServer by LanternPowered.

the class LanternPaginationService method buildPaginationCommand.

@SuppressWarnings("ConstantConditions")
private CommandSpec buildPaginationCommand() {
    // TODO Completely redo this once command refactor is out and PR changes to Sponge as well
    final ActivePaginationCommandElement paginationElement = new ActivePaginationCommandElement(t("pagination-id"));
    CommandSpec next = CommandSpec.builder().description(t("Go to the next page")).executor((src, args) -> {
        args.<ActivePagination>getOne("pagination-id").get().nextPage();
        return CommandResult.success();
    }).build();
    CommandSpec prev = CommandSpec.builder().description(t("Go to the previous page")).executor((src, args) -> {
        args.<ActivePagination>getOne("pagination-id").get().previousPage();
        return CommandResult.success();
    }).build();
    CommandElement pageArgs = integer(t("page"));
    CommandExecutor pageExecutor = (src, args) -> {
        args.<ActivePagination>getOne("pagination-id").get().specificPage(args.<Integer>getOne("page").get());
        return CommandResult.success();
    };
    CommandSpec page = CommandSpec.builder().description(t("Go to a specific page")).arguments(pageArgs).executor(pageExecutor).build();
    // Fallback to page arguments
    ChildCommandElementExecutor childDispatcher = new ChildCommandElementExecutor(pageExecutor);
    childDispatcher.register(next, "next", "n");
    childDispatcher.register(prev, "prev", "p", "previous");
    childDispatcher.register(page, "page");
    // https://github.com/SpongePowered/SpongeAPI/issues/1272
    return CommandSpec.builder().arguments(paginationElement, firstParsing(childDispatcher, pageArgs)).executor(childDispatcher).description(t("Helper command for paginations occurring")).build();
}
Also used : MessageReceiver(org.spongepowered.api.text.channel.MessageReceiver) Iterables(com.google.common.collect.Iterables) GenericArguments.firstParsing(org.spongepowered.api.command.args.GenericArguments.firstParsing) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) ConcurrentMap(java.util.concurrent.ConcurrentMap) PaginationList(org.spongepowered.api.service.pagination.PaginationList) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Map(java.util.Map) Nullable(javax.annotation.Nullable) CommandResult(org.spongepowered.api.command.CommandResult) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) CommandSource(org.spongepowered.api.command.CommandSource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) UUID(java.util.UUID) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) GenericArguments.integer(org.spongepowered.api.command.args.GenericArguments.integer) List(java.util.List) ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor) Lantern(org.lanternpowered.server.game.Lantern) Optional(java.util.Optional) MapMaker(com.google.common.collect.MapMaker) ChildCommandElementExecutor(org.spongepowered.api.command.args.ChildCommandElementExecutor) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) CommandElement(org.spongepowered.api.command.args.CommandElement)

Example 9 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project LanternServer by LanternPowered.

the class CommandHelp method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    final Comparator<CommandMapping> comparator = Comparator.comparing(CommandMapping::getPrimaryAlias);
    specBuilder.arguments(GenericArguments.optional(new CommandElement(Text.of("command")) {

        @Nullable
        @Override
        protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
            return args.next();
        }

        @Override
        public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
            final String nextArg = args.nextIfPresent().orElse("");
            return Lantern.getGame().getCommandManager().getAliases().stream().filter(new StartsWithPredicate(nextArg)).collect(Collectors.toList());
        }
    })).description(Text.of("View a list of all commands")).extendedDescription(Text.of("View a list of all commands. Hover over\n" + " a command to view its description. Click\n" + " a command to insert it into your chat bar.")).executor((src, args) -> {
        Optional<String> command = args.getOne("command");
        if (command.isPresent()) {
            Optional<? extends CommandMapping> mapping = Sponge.getCommandManager().get(command.get());
            if (mapping.isPresent()) {
                CommandCallable callable = mapping.get().getCallable();
                Optional<? extends Text> desc;
                // command name in the usage message
                if (callable instanceof CommandSpec) {
                    Text.Builder builder = Text.builder();
                    callable.getShortDescription(src).ifPresent(des -> builder.append(des, Text.NEW_LINE));
                    builder.append(t("commands.generic.usage", t("/%s %s", command.get(), callable.getUsage(src))));
                    Text extendedDescription;
                    try {
                        // TODO: Why is there no method :(
                        extendedDescription = (Text) extendedDescriptionField.get(callable);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                    if (extendedDescription != null) {
                        builder.append(Text.NEW_LINE, extendedDescription);
                    }
                    src.sendMessage(builder.build());
                } else if ((desc = callable.getHelp(src)).isPresent()) {
                    src.sendMessage(desc.get());
                } else {
                    src.sendMessage(t("commands.generic.usage", t("/%s %s", command.get(), callable.getUsage(src))));
                }
                return CommandResult.success();
            }
            throw new CommandException(Text.of("No such command: ", command.get()));
        }
        Lantern.getGame().getScheduler().submitAsyncTask(() -> {
            TreeSet<CommandMapping> commands = new TreeSet<>(comparator);
            commands.addAll(Collections2.filter(Sponge.getCommandManager().getAll().values(), input -> input.getCallable().testPermission(src)));
            final Text title = Text.builder("Available commands:").color(TextColors.DARK_GREEN).build();
            final List<Text> lines = commands.stream().map(c -> getDescription(src, c)).collect(Collectors.toList());
            // Console sources cannot see/use the pagination
            if (!(src instanceof ConsoleSource)) {
                Sponge.getGame().getServiceManager().provide(PaginationService.class).get().builder().title(title).padding(Text.of(TextColors.DARK_GREEN, "=")).contents(lines).sendTo(src);
            } else {
                src.sendMessage(title);
                src.sendMessages(lines);
            }
            return null;
        });
        return CommandResult.success();
    });
}
Also used : ConsoleSource(org.spongepowered.api.command.source.ConsoleSource) CommandCallable(org.spongepowered.api.command.CommandCallable) CommandMapping(org.spongepowered.api.command.CommandMapping) Collections2(com.google.common.collect.Collections2) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) GenericArguments(org.spongepowered.api.command.args.GenericArguments) TreeSet(java.util.TreeSet) PaginationList(org.spongepowered.api.service.pagination.PaginationList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) PluginContainer(org.spongepowered.api.plugin.PluginContainer) TextColors(org.spongepowered.api.text.format.TextColors) Nullable(javax.annotation.Nullable) CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) CommandSource(org.spongepowered.api.command.CommandSource) TextStyles(org.spongepowered.api.text.format.TextStyles) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Field(java.lang.reflect.Field) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) Lantern(org.lanternpowered.server.game.Lantern) Optional(java.util.Optional) Comparator(java.util.Comparator) CommandContext(org.spongepowered.api.command.args.CommandContext) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandCallable(org.spongepowered.api.command.CommandCallable) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) TreeSet(java.util.TreeSet) CommandMapping(org.spongepowered.api.command.CommandMapping) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) CommandArgs(org.spongepowered.api.command.args.CommandArgs) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) CommandSource(org.spongepowered.api.command.CommandSource) CommandElement(org.spongepowered.api.command.args.CommandElement) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource) Nullable(javax.annotation.Nullable)

Example 10 with CommandSpec

use of org.spongepowered.api.command.spec.CommandSpec in project SpongeAPI by SpongePowered.

the class ChildCommandElementExecutor method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    CommandMapping mapping = args.<CommandMapping>getOne(getUntranslatedKey()).orElse(null);
    if (mapping == null) {
        if (this.fallbackExecutor == null) {
            throw new CommandException(t("Invalid subcommand state -- no more than one mapping may be provided for child arg %s", getKey()));
        }
        return this.fallbackExecutor.execute(src, args);
    }
    if (mapping.getCallable() instanceof CommandSpec) {
        CommandSpec spec = ((CommandSpec) mapping.getCallable());
        spec.checkPermission(src);
        return spec.getExecutor().execute(src, args);
    }
    final String arguments = args.<String>getOne(getUntranslatedKey() + "_args").orElse("");
    return mapping.getCallable().process(src, arguments);
}
Also used : CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandMapping(org.spongepowered.api.command.CommandMapping) CommandException(org.spongepowered.api.command.CommandException)

Aggregations

CommandSpec (org.spongepowered.api.command.spec.CommandSpec)28 Text (org.spongepowered.api.text.Text)18 CommandContext (org.spongepowered.api.command.args.CommandContext)14 CommandExecutor (org.spongepowered.api.command.spec.CommandExecutor)14 List (java.util.List)12 ArgumentParseException (org.spongepowered.api.command.args.ArgumentParseException)12 GenericArguments (org.spongepowered.api.command.args.GenericArguments)12 ImmutableList (com.google.common.collect.ImmutableList)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 CommandResult (org.spongepowered.api.command.CommandResult)10 Lists (com.google.common.collect.Lists)9 Sponge (org.spongepowered.api.Sponge)9 CommandException (org.spongepowered.api.command.CommandException)9 Test (org.junit.Test)8 CommandSource (org.spongepowered.api.command.CommandSource)8 Player (org.spongepowered.api.entity.living.player.Player)8 SimpleDispatcher (org.spongepowered.api.command.dispatcher.SimpleDispatcher)7 TextColors (org.spongepowered.api.text.format.TextColors)7 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertFalse (org.junit.Assert.assertFalse)6