Class CommandManager<C>
- Type Parameters:
C- the command sender type used to execute commands
- All Implemented Interfaces:
CommandBuilderSource<C>,Stateful<RegistrationState>
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCommandManager(@NonNull ExecutionCoordinator<C> executionCoordinator, @NonNull CommandRegistrationHandler<C> commandRegistrationHandler) Create a new command manager instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidappendSuggestionMapper(@NonNull SuggestionMapper<? extends Suggestion> mapper) Sets the suggestion mapper forsuggestionFactory()to the result of appending the provided mapper to the current mapper usingSuggestionMapper.then(SuggestionMapper).Returns an unmodifiable snapshot of the currently registeredcapabilities.Returns the string-producing caption formatter.voidcaptionFormatter(@NonNull CaptionFormatter<C, String> captionFormatter) Sets the string-producing caption formatter.final @NonNull CaptionRegistry<C> Returns the caption registry.final voidcaptionRegistry(@NonNull CaptionRegistry<C> captionRegistry) Replaces the caption registry.Register a new command to the command manager and insert it into the underlying command tree.command(@NonNull CommandFactory<C> commandFactory) Creates a command using the givencommandFactoryand inserts it into the underlying command tree.command(@NonNull Command.Builder<? extends C> command) Register a new commandReturns the command executor.Returns the command registration handler.protected final voidcommandRegistrationHandler(@NonNull CommandRegistrationHandler<C> commandRegistrationHandler) final @NonNull Collection<@NonNull Command<C>> commands()Returns an unmodifiable view of all registered commands.Returns the command syntax formatter.voidcommandSyntaxFormatter(@NonNull CommandSyntaxFormatter<C> commandSyntaxFormatter) Sets the command syntax formatter.Returns the internal command tree.<T> @NonNull CommandComponent.Builder<C, T> componentBuilder(@NonNull Class<T> type, @NonNull String name) Create a new command component builder.Constructs a defaultCommandMetainstance.final @NonNull HelpHandler<C> Creates a new command help handler instance.final @NonNull HelpHandler<C> createHelpHandler(@NonNull CommandPredicate<C> filter) Creates a new command help handler instance.final @NonNull Command.Builder<C> decorateBuilder(@NonNull Command.Builder<C> builder) InvokesCommand.Builder.manager(CommandManager)withthisinstance and returns the updated builder.voiddeleteRootCommand(@NonNull String rootCommand) Deletes the givenrootCommand.final @NonNull ExceptionController<C> Returns the exception controller.flagBuilder(@NonNull String name) Create a new command flag builderbooleanhasCapability(@NonNull CloudCapability capability) Checks whether the cloud implementation has the givencapability.abstract booleanhasPermission(@NonNull C sender, @NonNull String permission) Check if the command sender has the required permission.final @NonNull HelpHandlerFactory<C> Returns the help handler factory.final voidhelpHandlerFactory(@NonNull HelpHandlerFactory<C> helpHandlerFactory) Sets the new help handler factory.booleanCheck if command registration is allowed.protected final voidTransition the command manager from eitherRegistrationState.BEFORE_REGISTRATIONorRegistrationState.REGISTERINGtoRegistrationState.AFTER_REGISTRATION.final @NonNull ParameterInjectorRegistry<C> Get the parameter injector registry instanceReturns the parser registry instance.postprocessContext(@NonNull CommandContext<C> context, @NonNull Command<C> command) Postprocess a command context instancepreprocessContext(@NonNull CommandContext<C> context, @NonNull CommandInput commandInput) Preprocess a command context instanceprotected final voidregisterCapability(@NonNull CloudCapability capability) Registers the givencapability.voidregisterCommandPostProcessor(@NonNull CommandPostprocessor<C> processor) Register a new command postprocessor.voidregisterCommandPreProcessor(@NonNull CommandPreprocessor<C> processor) Register a new command preprocessor.protected voidregisterDefaultExceptionHandlers(@NonNull Consumer<Triplet<CommandContext<C>, Caption, List<@NonNull CaptionVariable>>> messageSender, @NonNull Consumer<Pair<String, Throwable>> logger) Registers the default exception handlers.Returns all root command names.settings()Returns aConfigurableinstance that can be used to modify the settings for this command manager instance.final @NonNull RegistrationStatestate()Returns the current state.@NonNull SuggestionFactory<C, ? extends Suggestion> Returns the suggestion factory.@NonNull SuggestionMapper<? extends Suggestion> Returns the suggestion mapper forsuggestionFactory().voidsuggestionMapper(@NonNull SuggestionMapper<? extends Suggestion> mapper) Sets the suggestion mapper forsuggestionFactory().Returns the command suggestion processor used in this command manager.voidsuggestionProcessor(@NonNull SuggestionProcessor<C> suggestionProcessor) Sets the command suggestion processor.testPermission(@NonNull C sender, @NonNull Permission permission) Checks if the command sender has the required permission and returns the result.final booleanTransitions from theinstate to theoutstate, unless the currentStateful.state()is identical to theoutstate.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.incendo.cloud.CommandBuilderSource
commandBuilder, commandBuilder, commandBuilder, commandBuilder, commandBuilder, commandBuilderMethods inherited from interface org.incendo.cloud.state.Stateful
requireState, transitionOrThrow
-
Constructor Details
-
CommandManager
protected CommandManager(@NonNull ExecutionCoordinator<C> executionCoordinator, @NonNull CommandRegistrationHandler<C> commandRegistrationHandler) Create a new command manager instance.- Parameters:
executionCoordinator- Execution coordinator instance. When choosing the appropriate coordinator for your project, be sure to consider any limitations noted by the platform documentation.commandRegistrationHandler- Command registration handler. This will get called every time a new command is registered to the command manager. This may be used to forward command registration to the platform.
-
-
Method Details
-
commandExecutor
Returns the command executor.The executor is used to parse & execute commands.
- Returns:
- the command executor
-
suggestionFactory
Returns the suggestion factory.Will map results using
suggestionMapper().- Returns:
- the suggestion factory
-
suggestionMapper
Returns the suggestion mapper forsuggestionFactory().Platform command managers may replace the default mapper to better support platform suggestion types. Therefore it's encouraged to chain any additional mappers using
SuggestionMapper.then(SuggestionMapper)orappendSuggestionMapper(SuggestionMapper), rather than replacing the mapper directly.- Returns:
- the suggestion mapper
-
appendSuggestionMapper
Sets the suggestion mapper forsuggestionFactory()to the result of appending the provided mapper to the current mapper usingSuggestionMapper.then(SuggestionMapper).- Parameters:
mapper- suggestion mapper
-
suggestionMapper
Sets the suggestion mapper forsuggestionFactory().- Parameters:
mapper- suggestion mapper- See Also:
-
command
Register a new command to the command manager and insert it into the underlying command tree. The command will be forwarded to theCommandRegistrationHandlerand will, depending on the platform, be forwarded to the platform.Different command manager implementations have different requirements for the command registration. It is possible that a command manager may only allow registration during certain stages of the application lifetime. Read the platform command manager documentation to find out more about your particular platform
- Parameters:
command- Command to register- Returns:
- The command manager instance. This is returned so that these method calls may be chained. This will always
return
this.
-
command
@API(status=STABLE) public @NonNull CommandManager<C> command(@NonNull CommandFactory<C> commandFactory) Creates a command using the givencommandFactoryand inserts it into the underlying command tree. The command will be forwarded to theCommandRegistrationHandlerand will, depending on the platform, be forwarded to the platform.Different command manager implementations have different requirements for the command registration. It is possible that a command manager may only allow registration during certain stages of the application lifetime. Read the platform command manager documentation to find out more about your particular platform
- Parameters:
commandFactory- the command factory to register- Returns:
- The command manager instance. This is returned so that these method calls may be chained. This will always
return
this
-
command
Register a new command- Parameters:
command- Command to register.Command.Builder.build()} will be invoked.- Returns:
- The command manager instance
-
captionFormatter
Returns the string-producing caption formatter.- Returns:
- the formatter
- See Also:
-
captionFormatter
@API(status=STABLE) public void captionFormatter(@NonNull CaptionFormatter<C, String> captionFormatter) Sets the string-producing caption formatter.- Parameters:
captionFormatter- the new formatter- See Also:
-
commandSyntaxFormatter
Returns the command syntax formatter.- Returns:
- the syntax formatter
- See Also:
-
commandSyntaxFormatter
@API(status=STABLE) public void commandSyntaxFormatter(@NonNull CommandSyntaxFormatter<C> commandSyntaxFormatter) Sets the command syntax formatter.The command syntax formatter is used to format the command syntax hints that are used in help and error messages.
- Parameters:
commandSyntaxFormatter- new formatter- See Also:
-
commandRegistrationHandler
Returns the command registration handler.The command registration handler is able to intercept newly created/deleted commands, in order to propagate these changes to the native command handler of the platform.
In platforms without a native command concept, this is likely to return
CommandRegistrationHandler.nullCommandRegistrationHandler().- Returns:
- the command registration handler
-
commandRegistrationHandler
-
registerCapability
Registers the givencapability.- Parameters:
capability- the capability- See Also:
-
hasCapability
Checks whether the cloud implementation has the givencapability.- Parameters:
capability- the capability- Returns:
trueif the implementation has thecapability,falseif not- See Also:
-
capabilities
Returns an unmodifiable snapshot of the currently registeredcapabilities.- Returns:
- the currently registered capabilities
- See Also:
-
testPermission
@API(status=STABLE) public @NonNull PermissionResult testPermission(@NonNull C sender, @NonNull Permission permission) Checks if the command sender has the required permission and returns the result.- Parameters:
sender- the command senderpermission- the permission- Returns:
- a
PermissionResultrepresenting whether the sender has the permission
-
captionRegistry
Returns the caption registry.- Returns:
- the caption registry
- See Also:
-
captionRegistry
Replaces the caption registry.Some platforms may inject their own captions into the default caption registry, and so you may need to insert these captions yourself, if you do decide to replace the caption registry.
- Parameters:
captionRegistry- new caption registry.- See Also:
-
hasPermission
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:
trueif the sender has the permission, elsefalse
-
deleteRootCommand
@API(status=EXPERIMENTAL) public void deleteRootCommand(@NonNull String rootCommand) throws CloudCapability.CloudCapabilityMissingException Deletes the givenrootCommand.This will delete all chains that originate at the root command.
- Parameters:
rootCommand- The root command to delete- Throws:
CloudCapability.CloudCapabilityMissingException- IfCloudCapability.StandardCapabilities.ROOT_COMMAND_DELETIONis missing
-
rootCommands
Returns all root command names.- Returns:
- Root command names.
-
decorateBuilder
InvokesCommand.Builder.manager(CommandManager)withthisinstance and returns the updated builder.- Specified by:
decorateBuilderin interfaceCommandBuilderSource<C>- Parameters:
builder- builder to decorate- Returns:
- the decorated builder
-
componentBuilder
@API(status=STABLE) public <T> @NonNull CommandComponent.Builder<C,T> componentBuilder(@NonNull Class<T> type, @NonNull String name) Create a new command component builder.This will also invoke
CommandComponent.Builder.commandManager(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:
- Component builder
-
flagBuilder
Create a new command flag builder- Parameters:
name- Flag name- Returns:
- Flag builder
-
commandTree
Returns the internal command tree.Be careful when accessing the command tree. Do not interact with it, unless you absolutely know what you're doing.
- Returns:
- the command tree
-
createDefaultCommandMeta
Constructs a defaultCommandMetainstance.- Specified by:
createDefaultCommandMetain interfaceCommandBuilderSource<C>- Returns:
- default command meta
-
registerCommandPreProcessor
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:
-
registerCommandPostProcessor
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:
-
preprocessContext
@API(status=STABLE) public State preprocessContext(@NonNull CommandContext<C> context, @NonNull CommandInput commandInput) Preprocess a command context instance- Parameters:
context- Command contextcommandInput- Command input as supplied by sender- Returns:
State.ACCEPTEDif the command should be parsed and executed, elseState.REJECTED- See Also:
-
postprocessContext
Postprocess a command context instance- Parameters:
context- Command contextcommand- Command instance- Returns:
State.ACCEPTEDif the command should be parsed and executed, elseState.REJECTED- See Also:
-
suggestionProcessor
Returns the command suggestion processor used in this command manager.- Returns:
- the command suggestion processor
- See Also:
-
suggestionProcessor
Sets the command suggestion processor.This will be called every time
SuggestionFactory.suggest(CommandContext, String)is called, to process the list of suggestions before it's returned to the caller.- Parameters:
suggestionProcessor- the new command suggestion processor- See Also:
-
parserRegistry
Returns the parser registry instance.The parser registry contains default mappings to
argument parsersand allows for the registration of custom mappings. The parser registry also contains mappings between annotations andParserParameter, which allows for the customization of parser settings by using annotations.When creating a new parser type, it is highly 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:
- the parser registry instance
-
parameterInjectorRegistry
Get the parameter injector registry instance- Returns:
- Parameter injector registry
-
exceptionController
Returns the exception controller.The exception controller is responsible for exception handler registration.
- Returns:
- the exception controller
-
commands
Returns an unmodifiable view of all registered commands.- Returns:
- unmodifiable view of all registered commands
-
createHelpHandler
Creates a new command help handler instance.The command helper handler can be used to assist in the production of command help menus, etc.
This command help handler instance will display all commands registered in this command manager.
- Returns:
- a new command helper handler instance
-
createHelpHandler
@API(status=STABLE) public final @NonNull HelpHandler<C> createHelpHandler(@NonNull CommandPredicate<C> filter) Creates a new command help handler instance.The command helper handler can be used to assist in the production of commad help menus, etc.
A filter can be specified to filter what commands registered in this command manager are visible in the help menu.
- Parameters:
filter- predicate that filters what commands are displayed in the help menu.- Returns:
- a new command helper handler instance
-
helpHandlerFactory
Returns the help handler factory.- Returns:
- the help handler factory
-
helpHandlerFactory
@API(status=STABLE) public final void helpHandlerFactory(@NonNull HelpHandlerFactory<C> helpHandlerFactory) Sets the new help handler factory.The help handler factory is used to create
HelpHandlerinstances.- Parameters:
helpHandlerFactory- the new factory instance
-
settings
Returns aConfigurableinstance that can be used to modify the settings for this command manager instance.- Returns:
- settings instance
-
state
Description copied from interface:StatefulReturns the current state. -
transitionIfPossible
public final boolean transitionIfPossible(@NonNull RegistrationState in, @NonNull RegistrationState out) Description copied from interface:StatefulTransitions from theinstate to theoutstate, unless the currentStateful.state()is identical to theoutstate.- Specified by:
transitionIfPossiblein interfaceStateful<C>- Parameters:
in- the starting stateout- the ending state- Returns:
trueif the state transition was successful, or the current state is identical to theoutstate
-
lockRegistration
Transition the command manager from eitherRegistrationState.BEFORE_REGISTRATIONorRegistrationState.REGISTERINGtoRegistrationState.AFTER_REGISTRATION.- Throws:
IllegalStateException- if the manager is not in the expected state
-
isCommandRegistrationAllowed
Check if command registration is allowed.On platforms where unsafe registration is possible, this can be overridden by enabling the
ManagerSetting.ALLOW_UNSAFE_REGISTRATIONsetting.- Returns:
trueif the registration is allowed, elsefalse
-
registerDefaultExceptionHandlers
protected void registerDefaultExceptionHandlers(@NonNull Consumer<Triplet<CommandContext<C>, Caption, List<@NonNull CaptionVariable>>> messageSender, @NonNull Consumer<Pair<String, Throwable>> logger) Registers the default exception handlers.- Parameters:
messageSender- consumer that gets invoked when a message should be sent to the command senderlogger- consumer that gets invoked when a message should be logged
-