Search in sources :

Example 1 with ImmutableCommandMapping

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

Aggregations

CommandMapping (org.spongepowered.api.command.CommandMapping)1 ImmutableCommandMapping (org.spongepowered.api.command.ImmutableCommandMapping)1