Class CommandManager<C>
- java.lang.Object
-
- cloud.commandframework.CommandManager<C>
-
- Type Parameters:
C
- Command sender type
public abstract class CommandManager<C> extends java.lang.Object
The manager is responsible for command registration, parsing delegation, etc.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CommandManager.ManagerSettings
Configurable command related settings
-
Constructor Summary
Constructors Constructor Description CommandManager(@NonNull java.util.function.Function<@NonNull CommandTree<C>,@NonNull CommandExecutionCoordinator<C>> commandExecutionCoordinator, @NonNull CommandRegistrationHandler commandRegistrationHandler)
Create a new command manager instance
-
Method Summary
Modifier and Type Method Description <T> @NonNull CommandArgument.Builder<C,T>
argumentBuilder(@NonNull java.lang.Class<T> type, @NonNull java.lang.String name)
Create a new command argument builder.@NonNull CommandManager<C>
command(@NonNull Command.Builder<C> command)
Register a new command@NonNull CommandManager<C>
command(@NonNull Command<C> command)
Register a new command@NonNull Command.Builder<C>
commandBuilder(@NonNull java.lang.String name, @NonNull Description description, @NonNull java.lang.String... aliases)
Create a new command builder using default command meta created bycreateDefaultCommandMeta()
.@NonNull Command.Builder<C>
commandBuilder(@NonNull java.lang.String name, @NonNull CommandMeta meta, @NonNull Description description, @NonNull java.lang.String... aliases)
Create a new command builder.@NonNull Command.Builder<C>
commandBuilder(@NonNull java.lang.String name, @NonNull CommandMeta meta, @NonNull java.lang.String... aliases)
Create a new command builder with an empty description.@NonNull Command.Builder<C>
commandBuilder(@NonNull java.lang.String name, @NonNull java.lang.String... aliases)
Create a new command builder using default command meta created bycreateDefaultCommandMeta()
, and an empty description.@NonNull Command.Builder<C>
commandBuilder(@NonNull java.lang.String name, @NonNull java.util.Collection<java.lang.String> aliases, @NonNull Description description, @NonNull CommandMeta meta)
Create a new command builder.@NonNull Command.Builder<C>
commandBuilder(@NonNull java.lang.String name, @NonNull java.util.Collection<java.lang.String> aliases, @NonNull CommandMeta meta)
Create a new command builder with an empty description.abstract @NonNull CommandMeta
createDefaultCommandMeta()
Construct a default command meta instance@NonNull java.util.concurrent.CompletableFuture<CommandResult<C>>
executeCommand(@NonNull C commandSender, @NonNull java.lang.String input)
Execute a command and get a future that completes with the result@NonNull CommandFlag.Builder<java.lang.Void>
flagBuilder(@NonNull java.lang.String name)
Create a new command flag builder@NonNull CaptionRegistry<C>
getCaptionRegistry()
Get the caption registry@NonNull CommandHelpHandler<C>
getCommandHelpHandler()
Get a command help handler instance.@NonNull CommandRegistrationHandler
getCommandRegistrationHandler()
Get the command registration handler@NonNull java.util.Collection<@NonNull Command<C>>
getCommands()
Get a collection containing all registered commands.@NonNull CommandSuggestionProcessor<C>
getCommandSuggestionProcessor()
Get the command suggestions processor instance currently used in this command manager@NonNull CommandSyntaxFormatter<C>
getCommandSyntaxFormatter()
Get the command syntax formatter@NonNull CommandTree<C>
getCommandTree()
Get the internal command tree.<E extends java.lang.Exception>
@Nullable java.util.function.BiConsumer<@NonNull C,@NonNull E>getExceptionHandler(@NonNull java.lang.Class<E> clazz)
Get the exception handler for an exception type, if one has been registeredParserRegistry<C>
getParserRegistry()
Get the parser registry instance.boolean
getSetting(@NonNull CommandManager.ManagerSettings setting)
Get a command manager setting<E extends java.lang.Exception>
voidhandleException(@NonNull C sender, @NonNull java.lang.Class<E> clazz, @NonNull E exception, @NonNull java.util.function.BiConsumer<C,E> defaultHandler)
Handle an exception using the registered exception handler for the exception type, or using the provided default handler if no exception handler has been registered for the exception typeboolean
hasPermission(@NonNull C sender, @NonNull CommandPermission permission)
Check if the command sender has the required permission.abstract boolean
hasPermission(@NonNull C sender, @NonNull java.lang.String permission)
Check if the command sender has the required permission.cloud.commandframework.services.State
postprocessContext(@NonNull CommandContext<C> context, @NonNull Command<C> command)
Postprocess a command context instancecloud.commandframework.services.State
preprocessContext(@NonNull CommandContext<C> context, @NonNull java.util.LinkedList<@NonNull java.lang.String> inputQueue)
Preprocess a command context instancevoid
registerCommandPostProcessor(@NonNull CommandPostprocessor<C> processor)
Register a new command postprocessor.void
registerCommandPreProcessor(@NonNull CommandPreprocessor<C> processor)
Register a new command preprocessor.void
registerDefaultCaptions(@NonNull CaptionRegistry<C> captionRegistry)
Replace the default caption registry<E extends java.lang.Exception>
voidregisterExceptionHandler(@NonNull java.lang.Class<E> clazz, @NonNull java.util.function.BiConsumer<@NonNull C,@NonNull E> handler)
Register an exception handler for an exception type.void
setCaptionRegistry(@NonNull CaptionRegistry<C> captionRegistry)
Replace the caption registryprotected void
setCommandRegistrationHandler(@NonNull CommandRegistrationHandler commandRegistrationHandler)
void
setCommandSuggestionProcessor(@NonNull CommandSuggestionProcessor<C> commandSuggestionProcessor)
Set the command suggestions processor for this command manager.void
setCommandSyntaxFormatter(@NonNull CommandSyntaxFormatter<C> commandSyntaxFormatter)
Set the command syntax formattervoid
setSetting(@NonNull CommandManager.ManagerSettings setting, boolean value)
Set the setting@NonNull java.util.List<@NonNull java.lang.String>
suggest(@NonNull C commandSender, @NonNull java.lang.String input)
Get command suggestions for the "next" argument that would yield a correctly parsing command input
-
-
-
Constructor Detail
-
CommandManager
public CommandManager(@NonNull java.util.function.Function<@NonNull CommandTree<C>,@NonNull CommandExecutionCoordinator<C>> commandExecutionCoordinator, @NonNull CommandRegistrationHandler commandRegistrationHandler)
Create a new command manager instance- Parameters:
commandExecutionCoordinator
- Execution coordinator instancecommandRegistrationHandler
- Command registration handler
-
-
Method Detail
-
executeCommand
public @NonNull java.util.concurrent.CompletableFuture<CommandResult<C>> executeCommand(@NonNull C commandSender, @NonNull java.lang.String input)
Execute a command and get a future that completes with the result- Parameters:
commandSender
- Sender of the commandinput
- Input provided by the sender- Returns:
- Command result
-
suggest
public @NonNull java.util.List<@NonNull java.lang.String> suggest(@NonNull C commandSender, @NonNull java.lang.String input)
Get command suggestions for the "next" argument that would yield a correctly parsing command input- Parameters:
commandSender
- Sender of the commandinput
- Input provided by the sender- Returns:
- List of suggestions
-
command
public @NonNull CommandManager<C> command(@NonNull Command<C> command)
Register a new command- Parameters:
command
- Command to register- Returns:
- The command manager instance
-
command
public @NonNull CommandManager<C> command(@NonNull Command.Builder<C> command)
Register a new command- Parameters:
command
- Command to register.Command.Builder.build()
} will be invoked.- Returns:
- The command manager instance
-
getCommandSyntaxFormatter
public @NonNull CommandSyntaxFormatter<C> getCommandSyntaxFormatter()
Get the command syntax formatter- Returns:
- Command syntax formatter
-
setCommandSyntaxFormatter
public void setCommandSyntaxFormatter(@NonNull CommandSyntaxFormatter<C> commandSyntaxFormatter)
Set the command syntax formatter- Parameters:
commandSyntaxFormatter
- New formatter
-
getCommandRegistrationHandler
public @NonNull CommandRegistrationHandler getCommandRegistrationHandler()
Get the command registration handler- Returns:
- Command registration handler
-
setCommandRegistrationHandler
protected final void setCommandRegistrationHandler(@NonNull CommandRegistrationHandler commandRegistrationHandler)
-
hasPermission
public boolean hasPermission(@NonNull C sender, @NonNull CommandPermission permission)
Check if the command sender has the required permission. If the permission node is empty, this should returntrue
- Parameters:
sender
- Command senderpermission
- Permission node- Returns:
true
if the sender has the permission, elsefalse
-
getCaptionRegistry
public final @NonNull CaptionRegistry<C> getCaptionRegistry()
Get the caption registry- Returns:
- Caption registry
-
setCaptionRegistry
public final void setCaptionRegistry(@NonNull CaptionRegistry<C> captionRegistry)
Replace the caption registry- Parameters:
captionRegistry
- New caption registry
-
registerDefaultCaptions
public final void registerDefaultCaptions(@NonNull CaptionRegistry<C> captionRegistry)
Replace the default caption registry- Parameters:
captionRegistry
- Caption registry to use
-
hasPermission
public abstract boolean hasPermission(@NonNull C sender, @NonNull java.lang.String permission)
Check if the command sender has the required permission. If the permission node is empty, this should returntrue
- Parameters:
sender
- Command senderpermission
- Permission node- Returns:
true
if the sender has the permission, elsefalse
-
commandBuilder
public @NonNull Command.Builder<C> commandBuilder(@NonNull java.lang.String name, @NonNull java.util.Collection<java.lang.String> aliases, @NonNull Description description, @NonNull CommandMeta meta)
Create a new command builder. This will also register the creating manager in the command builder usingCommand.Builder.manager(CommandManager)
, so that the command builder is associated with the creating manager. This allows for parser inference based on the type, with the help of theparser registry
This method will not register the command in the manager. To do that,
command(Command.Builder)
orcommand(Command)
has to be invoked with either theCommand.Builder
instance, or the constructedcommand
instance- Parameters:
name
- Command namealiases
- Command aliasesdescription
- Command descriptionmeta
- Command meta- Returns:
- Builder instance
-
commandBuilder
public @NonNull Command.Builder<C> commandBuilder(@NonNull java.lang.String name, @NonNull java.util.Collection<java.lang.String> aliases, @NonNull CommandMeta meta)
Create a new command builder with an empty description.This will also register the creating manager in the command builder using
Command.Builder.manager(CommandManager)
, so that the command builder is associated with the creating manager. This allows for parser inference based on the type, with the help of theparser registry
This method will not register the command in the manager. To do that,
command(Command.Builder)
orcommand(Command)
has to be invoked with either theCommand.Builder
instance, or the constructedcommand
instance- Parameters:
name
- Command namealiases
- Command aliasesmeta
- Command meta- Returns:
- Builder instance
-
commandBuilder
public @NonNull Command.Builder<C> commandBuilder(@NonNull java.lang.String name, @NonNull CommandMeta meta, @NonNull Description description, @NonNull java.lang.String... aliases)
Create a new command builder. This will also register the creating manager in the command builder usingCommand.Builder.manager(CommandManager)
, so that the command builder is associated with the creating manager. This allows for parser inference based on the type, with the help of theparser registry
This method will not register the command in the manager. To do that,
command(Command.Builder)
orcommand(Command)
has to be invoked with either theCommand.Builder
instance, or the constructedcommand
instance- Parameters:
name
- Command namemeta
- Command metadescription
- Command descriptionaliases
- Command aliases- Returns:
- Builder instance
-
commandBuilder
public @NonNull Command.Builder<C> commandBuilder(@NonNull java.lang.String name, @NonNull CommandMeta meta, @NonNull java.lang.String... aliases)
Create a new command builder with an empty description.This will also register the creating manager in the command builder using
Command.Builder.manager(CommandManager)
, so that the command builder is associated with the creating manager. This allows for parser inference based on the type, with the help of theparser registry
This method will not register the command in the manager. To do that,
command(Command.Builder)
orcommand(Command)
has to be invoked with either theCommand.Builder
instance, or the constructedcommand
instance- Parameters:
name
- Command namemeta
- Command metaaliases
- Command aliases- Returns:
- Builder instance
-
commandBuilder
public @NonNull Command.Builder<C> commandBuilder(@NonNull java.lang.String name, @NonNull Description description, @NonNull java.lang.String... aliases)
Create a new command builder using default command meta created bycreateDefaultCommandMeta()
.This will also register the creating manager in the command builder using
Command.Builder.manager(CommandManager)
, so that the command builder is associated with the creating manager. This allows for parser inference based on the type, with the help of theparser registry
This method will not register the command in the manager. To do that,
command(Command.Builder)
orcommand(Command)
has to be invoked with either theCommand.Builder
instance, or the constructedcommand
instance- Parameters:
name
- Command namedescription
- Command descriptionaliases
- Command aliases- Returns:
- Builder instance
- Throws:
java.lang.UnsupportedOperationException
- If the command manager does not support default command meta creation- See Also:
Default command meta creation
-
commandBuilder
public @NonNull Command.Builder<C> commandBuilder(@NonNull java.lang.String name, @NonNull java.lang.String... aliases)
Create a new command builder using default command meta created bycreateDefaultCommandMeta()
, and an empty description.This will also register the creating manager in the command builder using
Command.Builder.manager(CommandManager)
, so that the command builder is associated with the creating manager. This allows for parser inference based on the type, with the help of theparser registry
This method will not register the command in the manager. To do that,
command(Command.Builder)
orcommand(Command)
has to be invoked with either theCommand.Builder
instance, or the constructedcommand
instance- Parameters:
name
- Command namealiases
- Command aliases- Returns:
- Builder instance
- Throws:
java.lang.UnsupportedOperationException
- If the command manager does not support default command meta creation- See Also:
Default command meta creation
-
argumentBuilder
public <T> @NonNull CommandArgument.Builder<C,T> argumentBuilder(@NonNull java.lang.Class<T> type, @NonNull java.lang.String name)
Create a new command argument builder.This will also invoke
CommandArgument.Builder.manager(CommandManager)
so that the argument is associated with the calling command manager. This allows for parser inference based on the type, with the help of theparser registry
.- Type Parameters:
T
- Generic argument name- Parameters:
type
- Argument typename
- Argument name- Returns:
- Argument builder
-
flagBuilder
public @NonNull CommandFlag.Builder<java.lang.Void> flagBuilder(@NonNull java.lang.String name)
Create a new command flag builder- Parameters:
name
- Flag name- Returns:
- Flag builder
-
getCommandTree
public @NonNull CommandTree<C> getCommandTree()
Get the internal command tree. This should not be accessed unless you know what you are doing- Returns:
- Command tree
-
createDefaultCommandMeta
public abstract @NonNull CommandMeta createDefaultCommandMeta()
Construct a default command meta instance- Returns:
- Default command meta
- Throws:
java.lang.UnsupportedOperationException
- If the command manager does not support this operation
-
registerCommandPreProcessor
public void registerCommandPreProcessor(@NonNull CommandPreprocessor<C> processor)
Register a new command preprocessor. The order they are registered in is respected, and they are called in LIFO order- Parameters:
processor
- Processor to register- See Also:
Preprocess a context
-
registerCommandPostProcessor
public void registerCommandPostProcessor(@NonNull CommandPostprocessor<C> processor)
Register a new command postprocessor. The order they are registered in is respected, and they are called in LIFO order- Parameters:
processor
- Processor to register- See Also:
Preprocess a context
-
preprocessContext
public cloud.commandframework.services.State preprocessContext(@NonNull CommandContext<C> context, @NonNull java.util.LinkedList<@NonNull java.lang.String> inputQueue)
Preprocess a command context instance- Parameters:
context
- Command contextinputQueue
- Command input as supplied by sender- Returns:
State.ACCEPTED
if the command should be parsed and executed, elseState.REJECTED
- See Also:
Register a command preprocessor
-
postprocessContext
public cloud.commandframework.services.State postprocessContext(@NonNull CommandContext<C> context, @NonNull Command<C> command)
Postprocess a command context instance- Parameters:
context
- Command contextcommand
- Command instance- Returns:
State.ACCEPTED
if the command should be parsed and executed, elseState.REJECTED
- See Also:
Register a command postprocessor
-
getCommandSuggestionProcessor
public @NonNull CommandSuggestionProcessor<C> getCommandSuggestionProcessor()
Get the command suggestions processor instance currently used in this command manager- Returns:
- Command suggestions processor
- See Also:
Setting the suggestion processor
-
setCommandSuggestionProcessor
public void setCommandSuggestionProcessor(@NonNull CommandSuggestionProcessor<C> commandSuggestionProcessor)
Set the command suggestions processor for this command manager. This will be called every timesuggest(Object, String)
is called, to process the list of suggestions before it's returned to the caller- Parameters:
commandSuggestionProcessor
- New command suggestions processor
-
getParserRegistry
public ParserRegistry<C> getParserRegistry()
Get the parser registry instance. The parser registry contains default mappings toArgumentParser
and allows for the registration of custom mappings. The parser registry also contains mappings of annotations toParserParameter
which allows for annotations to be used to customize parser settings.When creating a new parser type, it is recommended to register it in the parser registry. In particular, default parser types (shipped with cloud implementations) should be registered in the constructor of the platform
CommandManager
- Returns:
- Parser registry instance
-
getExceptionHandler
public final <E extends java.lang.Exception> @Nullable java.util.function.BiConsumer<@NonNull C,@NonNull E> getExceptionHandler(@NonNull java.lang.Class<E> clazz)
Get the exception handler for an exception type, if one has been registered- Type Parameters:
E
- Exception type- Parameters:
clazz
- Exception class- Returns:
- Exception handler, or
null
- See Also:
Registering an exception handler
-
registerExceptionHandler
public final <E extends java.lang.Exception> void registerExceptionHandler(@NonNull java.lang.Class<E> clazz, @NonNull java.util.function.BiConsumer<@NonNull C,@NonNull E> handler)
Register an exception handler for an exception type. This will then be used whenhandleException(Object, Class, Exception, BiConsumer)
is called for the particular exception type- Type Parameters:
E
- Exception type- Parameters:
clazz
- Exception classhandler
- Exception handler
-
handleException
public final <E extends java.lang.Exception> void handleException(@NonNull C sender, @NonNull java.lang.Class<E> clazz, @NonNull E exception, @NonNull java.util.function.BiConsumer<C,E> defaultHandler)
Handle an exception using the registered exception handler for the exception type, or using the provided default handler if no exception handler has been registered for the exception type- Type Parameters:
E
- Exception type- Parameters:
sender
- Executing command senderclazz
- Exception classexception
- Exception instancedefaultHandler
- Default exception handler. Will be called if there is no exception handler stored for the exception type
-
getCommands
public final @NonNull java.util.Collection<@NonNull Command<C>> getCommands()
Get a collection containing all registered commands.- Returns:
- Unmodifiable view of all registered commands
-
getCommandHelpHandler
public final @NonNull CommandHelpHandler<C> getCommandHelpHandler()
Get a command help handler instance. This can be used to assist in the production of command help menus, etc.- Returns:
- Command help handler. A new instance will be created each time this method is called.
-
getSetting
public boolean getSetting(@NonNull CommandManager.ManagerSettings setting)
Get a command manager setting- Parameters:
setting
- Setting- Returns:
true
if the setting is activated orfalse
if it's not- See Also:
Update a manager setting
-
setSetting
public void setSetting(@NonNull CommandManager.ManagerSettings setting, boolean value)
Set the setting- Parameters:
setting
- Setting to setvalue
- Value- See Also:
Get a manager setting
-
-