public final class SimpleDispatcher extends Object implements Dispatcher
Dispatcher
.Modifier and Type | Field and Description |
---|---|
static Disambiguator |
FIRST_DISAMBIGUATOR
This is a disambiguator function that returns the first matching command.
|
Constructor and Description |
---|
SimpleDispatcher()
Creates a basic new dispatcher.
|
SimpleDispatcher(Disambiguator disambiguatorFunc)
Creates a new dispatcher with a specific disambiguator.
|
Modifier and Type | Method and Description |
---|---|
boolean |
containsAlias(String alias)
Returns whether the dispatcher contains a registered command for the
given alias.
|
boolean |
containsMapping(CommandMapping mapping)
Returns whether the dispatcher contains the given mapping.
|
Optional<CommandMapping> |
get(String alias)
Gets the
CommandMapping associated with an alias. |
Optional<CommandMapping> |
get(String alias,
CommandSource source)
Gets the
CommandMapping associated with an alias in the context
of a given CommandSource . |
Set<String> |
getAliases()
Gets a list of all the command aliases, which includes the primary alias.
|
Multimap<String,CommandMapping> |
getAll()
Gets all commands currently registered with this dispatcher.
|
Set<CommandMapping> |
getAll(String alias)
Gets all the
CommandMapping s associated with an alias. |
Set<CommandMapping> |
getCommands()
Gets a list of commands.
|
Optional<Text> |
getHelp(CommandSource source)
Gets a longer formatted help message about this command.
|
Set<String> |
getPrimaryAliases()
Gets a list of primary aliases.
|
Optional<Text> |
getShortDescription(CommandSource source)
Gets a short one-line description of this command.
|
List<String> |
getSuggestions(CommandSource src,
String arguments,
Location<World> targetPosition)
Gets a list of suggestions based on input.
|
Text |
getUsage(CommandSource source)
Gets the usage string of this command.
|
CommandResult |
process(CommandSource source,
String commandLine)
Execute the command based on input arguments.
|
Optional<CommandMapping> |
register(CommandCallable callable,
List<String> aliases)
Register a given command using the given list of aliases.
|
Optional<CommandMapping> |
register(CommandCallable callable,
List<String> aliases,
Function<List<String>,List<String>> callback)
Register a given command using a given list of aliases.
|
Optional<CommandMapping> |
register(CommandCallable callable,
String... alias)
Register a given command using the given list of aliases.
|
Collection<CommandMapping> |
remove(String alias)
Remove a mapping identified by the given alias.
|
boolean |
removeAll(Collection<?> aliases)
Remove all mappings identified by the given aliases.
|
Optional<CommandMapping> |
removeMapping(CommandMapping mapping)
Remove a command identified by the given mapping.
|
boolean |
removeMappings(Collection<?> mappings)
Remove all mappings contained with the given collection.
|
int |
size()
Gets the number of registered aliases.
|
boolean |
testPermission(CommandSource source)
Test whether this command can probably be executed by the given source.
|
public static final Disambiguator FIRST_DISAMBIGUATOR
public SimpleDispatcher()
public SimpleDispatcher(Disambiguator disambiguatorFunc)
disambiguatorFunc
- Function that returns the preferred command if
multiple exist for a given aliaspublic Optional<CommandMapping> register(CommandCallable callable, String... alias)
If there is a conflict with one of the aliases (i.e. that alias is already assigned to another command), then the alias will be skipped. It is possible for there to be no alias to be available out of the provided list of aliases, which would mean that the command would not be assigned to any aliases.
The first non-conflicted alias becomes the "primary alias."
callable
- The commandalias
- An array of aliasespublic Optional<CommandMapping> register(CommandCallable callable, List<String> aliases)
If there is a conflict with one of the aliases (i.e. that alias is already assigned to another command), then the alias will be skipped. It is possible for there to be no alias to be available out of the provided list of aliases, which would mean that the command would not be assigned to any aliases.
The first non-conflicted alias becomes the "primary alias."
callable
- The commandaliases
- A list of aliasespublic Optional<CommandMapping> register(CommandCallable callable, List<String> aliases, Function<List<String>,List<String>> callback)
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
IllegalArgumentException
to be thrown.
The first non-conflicted alias becomes the "primary alias."
callable
- The commandaliases
- A list of aliasescallback
- The callbackpublic Collection<CommandMapping> remove(String alias)
alias
- The aliaspublic boolean removeAll(Collection<?> aliases)
aliases
- A collection of aliasespublic Optional<CommandMapping> removeMapping(CommandMapping mapping)
mapping
- The mappingpublic boolean removeMappings(Collection<?> mappings)
mappings
- The collectionpublic Set<CommandMapping> getCommands()
Dispatcher
The returned collection cannot be modified.
getCommands
in interface Dispatcher
public Set<String> getPrimaryAliases()
Dispatcher
The returned collection cannot be modified.
getPrimaryAliases
in interface Dispatcher
public Set<String> getAliases()
Dispatcher
A command may have more than one alias assigned to it. The returned collection cannot be modified.
getAliases
in interface Dispatcher
public Optional<CommandMapping> get(String alias)
Dispatcher
CommandMapping
associated with an alias. Returns null if
no command is named by the given alias.get
in interface Dispatcher
alias
- The aliaspublic Optional<CommandMapping> get(String alias, @Nullable CommandSource source)
Dispatcher
CommandMapping
associated with an alias in the context
of a given CommandSource
. Returns null if no command is named by
the given alias.get
in interface Dispatcher
alias
- The alias to look upsource
- The source this alias is being looked up forpublic boolean containsAlias(String alias)
Dispatcher
containsAlias
in interface Dispatcher
alias
- The aliaspublic boolean containsMapping(CommandMapping mapping)
Dispatcher
containsMapping
in interface Dispatcher
mapping
- The mappingpublic CommandResult process(CommandSource source, String commandLine) throws CommandException
CommandCallable
The implementing class must perform the necessary permission checks.
process
in interface CommandCallable
source
- The caller of the commandcommandLine
- The raw arguments for this commandCommandException
- Thrown on a command errorpublic List<String> getSuggestions(CommandSource src, String arguments, @Nullable Location<World> targetPosition) throws CommandException
CommandCallable
If a suggestion is chosen by the user, it will replace the last word.
getSuggestions
in interface CommandCallable
src
- The command sourcearguments
- The arguments entered up to this pointtargetPosition
- The position the source is looking at when
performing tab completionCommandException
- Thrown if there was a parsing errorpublic boolean testPermission(CommandSource source)
CommandCallable
If implementations are unsure if the command can be executed by
the source, true
should be returned. Return values of this method
may be used to determine whether this command is listed in command
listings.
testPermission
in interface CommandCallable
source
- The caller of the commandpublic Optional<Text> getShortDescription(CommandSource source)
CommandCallable
The help system may display the description in the command list.
getShortDescription
in interface CommandCallable
source
- The source of the help requestpublic Optional<Text> getHelp(CommandSource source)
CommandCallable
It is recommended to use the default text color and style. Sections with text actions (e.g. hyperlinks) should be underlined.
Multi-line messages can be created by separating the lines with
\n
.
The help system may display this message when a source requests detailed information about a command.
getHelp
in interface CommandCallable
source
- The source of the help requestpublic int size()
public Text getUsage(CommandSource source)
CommandCallable
A usage string may look like
[-w <world>] <var1> <var2>
.
The string must not contain the command alias.
getUsage
in interface CommandCallable
source
- The source of the help requestpublic Set<CommandMapping> getAll(String alias)
Dispatcher
CommandMapping
s associated with an alias.getAll
in interface Dispatcher
alias
- The aliaspublic Multimap<String,CommandMapping> getAll()
Dispatcher
getAll
in interface Dispatcher