Search in sources :

Example 11 with CommandMapping

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

the class ChildCommandElementExecutor method parse.

@Override
public void parse(CommandSource source, CommandArgs args, CommandContext context) throws ArgumentParseException {
    if (this.fallbackExecutor != null && !args.hasNext()) {
        if (this.fallbackElements != null) {
            // there might be optionals to take account of that would parse this successfully.
            this.fallbackElements.parse(source, args, context);
        }
        // execute the fallback regardless in this scenario.
        return;
    }
    Object state = args.getState();
    final String key = args.next();
    Optional<CommandMapping> optionalCommandMapping = this.dispatcher.get(key, source);
    if (optionalCommandMapping.isPresent()) {
        final CommandMapping mapping = optionalCommandMapping.get();
        try {
            if ((mapping.getCallable() instanceof CommandSpec)) {
                CommandSpec spec = ((CommandSpec) mapping.getCallable());
                spec.populateContext(source, args, context);
            } else {
                if (args.hasNext()) {
                    args.next();
                }
                context.putArg(getUntranslatedKey() + "_args", args.getRaw().substring(args.getRawPosition()));
                while (args.hasNext()) {
                    args.next();
                }
            }
            // Success, add to context now so that we don't execute the wrong executor in the first place.
            context.putArg(getUntranslatedKey(), mapping);
        } catch (ArgumentParseException ex) {
            // If we get here, fallback to the elements, if they exist.
            args.setState(state);
            if (this.fallbackOnFail && this.fallbackElements != null) {
                this.fallbackElements.parse(source, args, context);
                return;
            }
            // Get the usage
            args.next();
            if (ex instanceof ArgumentParseException.WithUsage) {
                // This indicates a previous child failed, so we just prepend our child
                throw new ArgumentParseException.WithUsage(ex, Text.of(key, " ", ((ArgumentParseException.WithUsage) ex).getUsage()));
            }
            throw new ArgumentParseException.WithUsage(ex, Text.of(key, " ", mapping.getCallable().getUsage(source)));
        }
    } else {
        // Not a child, so let's continue with the fallback.
        if (this.fallbackExecutor != null && this.fallbackElements != null) {
            args.setState(state);
            this.fallbackElements.parse(source, args, context);
        } else {
            // so specifying it implicitly means we have a child command to execute.
            throw args.createError(t("Input command %s was not a valid subcommand!", key));
        }
    }
}
Also used : CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandMapping(org.spongepowered.api.command.CommandMapping)

Example 12 with CommandMapping

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

the class SimpleDispatcher method removeMapping.

/**
 * Remove a command identified by the given mapping.
 *
 * @param mapping The mapping
 * @return The previous mapping associated with the alias, if one was found
 */
public synchronized Optional<CommandMapping> removeMapping(CommandMapping mapping) {
    checkNotNull(mapping, "mapping");
    CommandMapping found = null;
    Iterator<CommandMapping> it = this.commands.values().iterator();
    while (it.hasNext()) {
        CommandMapping current = it.next();
        if (current.equals(mapping)) {
            it.remove();
            found = current;
        }
    }
    return Optional.ofNullable(found);
}
Also used : ImmutableCommandMapping(org.spongepowered.api.command.ImmutableCommandMapping) CommandMapping(org.spongepowered.api.command.CommandMapping)

Example 13 with CommandMapping

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

the class SimpleDispatcher method getHelp.

@Override
public Optional<Text> getHelp(CommandSource source) {
    if (this.commands.isEmpty()) {
        return Optional.empty();
    }
    Text.Builder build = t("Available commands:\n").toBuilder();
    for (Iterator<String> it = filterCommands(source).iterator(); it.hasNext(); ) {
        final Optional<CommandMapping> mappingOpt = get(it.next(), source);
        if (!mappingOpt.isPresent()) {
            continue;
        }
        CommandMapping mapping = mappingOpt.get();
        final Optional<Text> description = mapping.getCallable().getShortDescription(source);
        build.append(Text.builder(mapping.getPrimaryAlias()).color(TextColors.GREEN).style(TextStyles.UNDERLINE).onClick(TextActions.suggestCommand("/" + mapping.getPrimaryAlias())).build(), SPACE_TEXT, description.orElse(mapping.getCallable().getUsage(source)));
        if (it.hasNext()) {
            build.append(Text.NEW_LINE);
        }
    }
    return Optional.of(build.build());
}
Also used : ImmutableCommandMapping(org.spongepowered.api.command.ImmutableCommandMapping) CommandMapping(org.spongepowered.api.command.CommandMapping) Text(org.spongepowered.api.text.Text)

Example 14 with CommandMapping

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

the class LanternCommandManager 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
        final List<String> aliasesWithPrefix = new ArrayList<>(aliases.size() * 2);
        for (String alias : aliases) {
            final Collection<CommandMapping> ownedCommands = this.owners.get(container);
            for (CommandMapping mapping : this.dispatcher.getAll(alias)) {
                if (ownedCommands.contains(mapping)) {
                    throw new IllegalArgumentException("A plugin may not register multiple commands for the same alias ('" + alias + "')!");
                }
            }
            aliasesWithPrefix.add(alias);
            aliasesWithPrefix.add(container.getId() + ':' + alias);
        }
        final 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)

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