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;
}
}
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);
}
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;
}
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);
}
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();
}
}
}
Aggregations