public class SimpleCommandService extends Object implements CommandService
CommandService
.
This service calls the appropriate events for a command.Constructor and Description |
---|
SimpleCommandService(Game game,
Logger logger)
Construct a simple
CommandService . |
SimpleCommandService(Game game,
Logger logger,
Disambiguator disambiguator)
Construct a simple
CommandService . |
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)
Get the
CommandMapping associated with an alias. |
Set<String> |
getAliases()
Get a list of all the command aliases, which includes the primary alias.
|
Multimap<String,CommandMapping> |
getAll()
Get all commands currently registered with this dispatcher.
|
Set<? extends CommandMapping> |
getAll(String alias)
Gets all the
CommandMapping s associated with an alias. |
Set<CommandMapping> |
getCommands()
Get a list of commands.
|
Optional<Text> |
getHelp(CommandSource source)
Get a longer formatted help message about this command.
|
Set<CommandMapping> |
getOwnedBy(Object instance)
Get a set of commands owned by the given plugin instance.
|
Set<PluginContainer> |
getPluginContainers()
Get a set of plugin containers that have commands registered.
|
Set<String> |
getPrimaryAliases()
Get a list of primary aliases.
|
Optional<Text> |
getShortDescription(CommandSource source)
Get a short one-line description of this command.
|
List<String> |
getSuggestions(CommandSource src,
String arguments)
Get a list of suggestions based on input.
|
Text |
getUsage(CommandSource source)
Get the usage string of this command.
|
CommandResult |
process(CommandSource source,
String commandLine)
Execute the command based on input arguments.
|
Optional<CommandMapping> |
register(Object plugin,
CommandCallable callable,
List<String> aliases)
Register a given command using the given list of aliases.
|
Optional<CommandMapping> |
register(Object plugin,
CommandCallable callable,
List<String> aliases,
Function<List<String>,List<String>> callback)
Register a given command using a given list of aliases.
|
Optional<CommandMapping> |
register(Object plugin,
CommandCallable callable,
String... alias)
Register a given command using the given list of aliases.
|
Optional<CommandMapping> |
removeMapping(CommandMapping mapping)
Remove a command identified by the given mapping.
|
int |
size()
Get the number of registered aliases.
|
boolean |
testPermission(CommandSource source)
Test whether this command can probably be executed by the given source.
|
@Inject public SimpleCommandService(Game game, Logger logger)
CommandService
.game
- The game to use for this CommandServicelogger
- The logger to log error messages topublic SimpleCommandService(Game game, Logger logger, Disambiguator disambiguator)
CommandService
.game
- The game to use for this CommandServicelogger
- The logger to log error messages todisambiguator
- The function to resolve a single command when multiple options are availablepublic Optional<CommandMapping> register(Object plugin, CommandCallable callable, String... alias)
CommandService
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."
register
in interface CommandService
plugin
- A plugin instancecallable
- The commandalias
- An array of aliasespublic Optional<CommandMapping> register(Object plugin, CommandCallable callable, List<String> aliases)
CommandService
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."
register
in interface CommandService
plugin
- A plugin instancecallable
- The commandaliases
- A list of aliasespublic Optional<CommandMapping> register(Object plugin, CommandCallable callable, List<String> aliases, Function<List<String>,List<String>> callback)
CommandService
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."
register
in interface CommandService
plugin
- A plugin instancecallable
- The commandaliases
- A list of aliasescallback
- The callbackpublic Optional<CommandMapping> removeMapping(CommandMapping mapping)
CommandService
removeMapping
in interface CommandService
mapping
- The mappingpublic Set<PluginContainer> getPluginContainers()
CommandService
getPluginContainers
in interface CommandService
public Set<CommandMapping> getCommands()
Dispatcher
The returned collection cannot be modified.
getCommands
in interface Dispatcher
public Set<CommandMapping> getOwnedBy(Object instance)
CommandService
getOwnedBy
in interface CommandService
instance
- The plugin instancepublic 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 Set<? extends 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
public 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)
CommandService
The implementing class must perform the necessary permission checks.
process
in interface CommandService
process
in interface CommandCallable
source
- The caller of the commandcommandLine
- The raw arguments for this commandpublic List<String> getSuggestions(CommandSource src, String arguments)
CommandService
If a suggestion is chosen by the user, it will replace the last word.
getSuggestions
in interface CommandService
getSuggestions
in interface CommandCallable
src
- The command sourcearguments
- The arguments entered up to this pointpublic 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 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 int size()
CommandService
size
in interface CommandService