Search in sources :

Example 1 with CommandMapping

use of org.spongepowered.api.command.CommandMapping in project SpongeCommon by SpongePowered.

the class SpongeCommandManager method register.

@Override
public Optional<CommandMapping> register(Object plugin, CommandCallable callable, List<String> aliases, Function<List<String>, List<String>> callback) {
    checkNotNull(plugin, "plugin");
    Optional<PluginContainer> containerOptional = Sponge.getGame().getPluginManager().fromInstance(plugin);
    if (!containerOptional.isPresent()) {
        throw new IllegalArgumentException("The provided plugin object does not have an associated plugin container " + "(in other words, is 'plugin' actually your plugin object?");
    }
    PluginContainer container = containerOptional.get();
    synchronized (this.lock) {
        // <namespace>:<alias> for all commands
        List<String> aliasesWithPrefix = new ArrayList<>(aliases.size() * 3);
        for (final String originalAlias : aliases) {
            final String alias = this.fixAlias(container, originalAlias);
            if (aliasesWithPrefix.contains(alias)) {
                this.logger.debug("Plugin '{}' is attempting to register duplicate alias '{}'", container.getId(), alias);
                continue;
            }
            final Collection<CommandMapping> ownedCommands = this.owners.get(container);
            for (CommandMapping mapping : this.dispatcher.getAll(alias)) {
                if (ownedCommands.contains(mapping)) {
                    boolean isWrapper = callable instanceof MinecraftCommandWrapper;
                    if (!(isWrapper && ((MinecraftCommandWrapper) callable).suppressDuplicateAlias(alias))) {
                        throw new IllegalArgumentException("A plugin may not register multiple commands for the same alias ('" + alias + "')!");
                    }
                }
            }
            aliasesWithPrefix.add(alias);
            aliasesWithPrefix.add(container.getId() + ':' + alias);
        }
        Optional<CommandMapping> mapping = this.dispatcher.register(callable, aliasesWithPrefix, callback);
        if (mapping.isPresent()) {
            this.owners.put(container, mapping.get());
            this.reverseOwners.put(mapping.get(), container);
        }
        return mapping;
    }
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer) CommandMapping(org.spongepowered.api.command.CommandMapping) ArrayList(java.util.ArrayList)

Example 2 with CommandMapping

use of org.spongepowered.api.command.CommandMapping 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)

Example 3 with CommandMapping

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

the class ChildCommandElementExecutor method complete.

@Override
public List<String> complete(final CommandSource src, CommandArgs args, CommandContext context) {
    List<String> completions = Lists.newArrayList();
    if (this.fallbackElements != null) {
        Object state = args.getState();
        completions.addAll(this.fallbackElements.complete(src, args, context));
        args.setState(state);
    }
    final Optional<String> commandComponent = args.nextIfPresent();
    if (!commandComponent.isPresent()) {
        return ImmutableList.copyOf(filterCommands(src));
    }
    if (args.hasNext()) {
        Optional<CommandMapping> child = this.dispatcher.get(commandComponent.get(), src);
        if (!child.isPresent()) {
            return ImmutableList.of();
        }
        if (child.get().getCallable() instanceof CommandSpec) {
            return ((CommandSpec) child.get().getCallable()).complete(src, args, context);
        }
        args.nextIfPresent();
        final String arguments = args.getRaw().substring(args.getRawPosition());
        while (args.hasNext()) {
            args.nextIfPresent();
        }
        try {
            return child.get().getCallable().getSuggestions(src, arguments, context.<Location<World>>getOne(CommandContext.TARGET_BLOCK_ARG).orElse(null));
        } catch (CommandException e) {
            Text eText = e.getText();
            if (eText != null) {
                src.sendMessage(error(eText));
            }
            return ImmutableList.of();
        }
    }
    completions.addAll(filterCommands(src).stream().filter(new StartsWithPredicate(commandComponent.get())).collect(ImmutableList.toImmutableList()));
    return completions;
}
Also used : CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandMapping(org.spongepowered.api.command.CommandMapping) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) Location(org.spongepowered.api.world.Location)

Example 4 with CommandMapping

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

the class SimpleDispatcher method register.

/**
 * Register a given command using a given list of aliases.
 *
 * <p>The provided callback function will be called with a list of aliases
 * that are not taken (from the list of aliases that were requested) and
 * it should return a list of aliases to actually register. Aliases may be
 * removed, and if no aliases remain, then the command will not be
 * registered. It may be possible that no aliases are available, and thus
 * the callback would receive an empty list. New aliases should not be added
 * to the list in the callback as this may cause
 * {@link IllegalArgumentException} to be thrown.</p>
 *
 * <p>The first non-conflicted alias becomes the "primary alias."</p>
 *
 * @param callable The command
 * @param aliases A list of aliases
 * @param callback The callback
 * @return The registered command mapping, unless no aliases could
 *     be registered
 */
public synchronized Optional<CommandMapping> register(CommandCallable callable, List<String> aliases, Function<List<String>, List<String>> callback) {
    checkNotNull(aliases, "aliases");
    checkNotNull(callable, "callable");
    checkNotNull(callback, "callback");
    // Invoke the callback with the commands that /can/ be registered
    // noinspection ConstantConditions
    aliases = ImmutableList.copyOf(callback.apply(aliases));
    if (aliases.isEmpty()) {
        return Optional.empty();
    }
    String primary = aliases.get(0);
    List<String> secondary = aliases.subList(1, aliases.size());
    CommandMapping mapping = new ImmutableCommandMapping(callable, primary, secondary);
    for (String alias : aliases) {
        this.commands.put(alias.toLowerCase(), mapping);
    }
    return Optional.of(mapping);
}
Also used : ImmutableCommandMapping(org.spongepowered.api.command.ImmutableCommandMapping) CommandMapping(org.spongepowered.api.command.CommandMapping) ImmutableCommandMapping(org.spongepowered.api.command.ImmutableCommandMapping)

Example 5 with CommandMapping

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

the class SimpleDispatcher method process.

@Override
public CommandResult process(CommandSource source, String commandLine) throws CommandException {
    final String[] argSplit = commandLine.split(" ", 2);
    Optional<CommandMapping> cmdOptional = get(argSplit[0], source);
    if (!cmdOptional.isPresent()) {
        // TODO: Fix properly to use a SpongeTranslation??
        throw new CommandNotFoundException(t("commands.generic.notFound"), argSplit[0]);
    }
    final String arguments = argSplit.length > 1 ? argSplit[1] : "";
    CommandMapping mapping = cmdOptional.get();
    Optional<PluginContainer> pluginOwner = Sponge.getCommandManager().getOwner(mapping);
    if (pluginOwner.isPresent()) {
        Sponge.getCauseStackManager().pushCause(pluginOwner.get());
    }
    final CommandCallable spec = mapping.getCallable();
    try {
        return spec.process(source, arguments);
    } catch (CommandNotFoundException e) {
        throw new CommandException(t("No such child command: %s", e.getCommand()));
    } finally {
        if (pluginOwner.isPresent()) {
            Sponge.getCauseStackManager().popCause();
        }
    }
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer) ImmutableCommandMapping(org.spongepowered.api.command.ImmutableCommandMapping) CommandMapping(org.spongepowered.api.command.CommandMapping) CommandException(org.spongepowered.api.command.CommandException) CommandNotFoundException(org.spongepowered.api.command.CommandNotFoundException) CommandCallable(org.spongepowered.api.command.CommandCallable)

Aggregations

CommandMapping (org.spongepowered.api.command.CommandMapping)14 CommandException (org.spongepowered.api.command.CommandException)5 PluginContainer (org.spongepowered.api.plugin.PluginContainer)5 Text (org.spongepowered.api.text.Text)5 ImmutableCommandMapping (org.spongepowered.api.command.ImmutableCommandMapping)4 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Optional (java.util.Optional)3 Sponge (org.spongepowered.api.Sponge)3 CommandCallable (org.spongepowered.api.command.CommandCallable)3 Collection (java.util.Collection)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Nullable (javax.annotation.Nullable)2 CommandResult (org.spongepowered.api.command.CommandResult)2 CommandSource (org.spongepowered.api.command.CommandSource)2 SendCommandEvent (org.spongepowered.api.event.command.SendCommandEvent)2 TextActions (org.spongepowered.api.text.action.TextActions)2 StartsWithPredicate (org.spongepowered.api.util.StartsWithPredicate)2