Interface CommandHandler

All Known Subinterfaces:
BukkitCommandHandler, BungeeCommandHandler, ConsoleCommandHandler, JDACommandHandler, SpongeCommandHandler, VelocityCommandHandler
All Known Implementing Classes:
BaseCommandHandler, BukkitHandler, BungeeHandler, CLIHandler, JDAHandler, SpongeHandler, VelocityHandler

public interface CommandHandler
The main handler for registering commands, resolvers, interceptors, handlers, tab completions and other stuff.
  • Method Details

    • register

      @NotNull @NotNull CommandHandler register(@NotNull @NotNull Object... commands)
      Registers the specified command from an instance. This will automatically set all Dependency-annotated fields with their values.
      Parameters:
      commands - The commands object instances. Can be a class if methods are static.
      Returns:
      This command handler
    • getLocale

      @NotNull @NotNull Locale getLocale()
      Gets the current, default locale used by this handler
      Returns:
      The default locale
    • setLocale

      void setLocale(@NotNull @NotNull Locale locale)
      Sets the locale of this handler.
      Parameters:
      locale - The locale of this handler
    • getTranslator

      @NotNull @NotNull Translator getTranslator()
      Returns the translator of this command handler
      Returns:
      The message translator
    • setMethodCallerFactory

      @NotNull @NotNull CommandHandler setMethodCallerFactory(@NotNull @NotNull MethodCallerFactory factory)
      Sets the MethodCallerFactory responsible for generating access to reflection methods.

      This method allows using alternative strategy for accessing methods reflectively.

      Parameters:
      factory - Factory to set
      Returns:
      This command handler
      See Also:
    • getArgumentParser

      @NotNull @NotNull ArgumentParser getArgumentParser()
      Returns the ArgumentParser responsible for controlling the logic of parsing strings.
      Returns:
      The argument parser.
    • parseArgumentsForCompletion

      ArgumentStack parseArgumentsForCompletion(String... arguments) throws ArgumentParseException
      Parses the string and returns an ArgumentStack for it. This method behaves exactly like parseArguments(String[]), however it returns a singleton list of an empty string when the text is empty, suitable for auto-completion.
      Parameters:
      arguments - Strings to parse. These will get joined to a string separated by spaces.
      Returns:
      The argument stack.
      Throws:
      ArgumentParseException
    • parseArguments

      ArgumentStack parseArguments(String... arguments) throws ArgumentParseException
      Parses the string array and returns an ArgumentStack for it.
      Parameters:
      arguments - String array to parse. This will be joined as a single string with spaces.
      Returns:
      The argument stack.
      Throws:
      ArgumentParseException
      See Also:
    • setArgumentParser

      CommandHandler setArgumentParser(@NotNull @NotNull ArgumentParser parser)
      Sets the ArgumentParser responsible for controlling the logic of parsing strings.
      Parameters:
      parser - The argument parser to use.
      Returns:
      This command handler
    • setExceptionHandler

      @NotNull @NotNull CommandHandler setExceptionHandler(@NotNull @NotNull CommandExceptionHandler handler)
      Sets the CommandExceptionHandler to use for handling any exceptions that are thrown from the command.

      If not set, a default one will be used.

      Parameters:
      handler - The exception handler
      Returns:
      This command handler
      See Also:
    • registerExceptionHandler

      @NotNull <T extends Throwable> @NotNull CommandHandler registerExceptionHandler(@NotNull @NotNull Class<T> exceptionType, @NotNull @NotNull BiConsumer<CommandActor,T> handler)
      Convenient method to register exception handlers. These will have higher priority over CommandExceptionHandler.
      Parameters:
      exceptionType - The exception class
      handler - The exception handler
      Returns:
      This command handler
      See Also:
    • setSwitchPrefix

      @NotNull @NotNull CommandHandler setSwitchPrefix(@NotNull @NotNull String prefix)
      Sets the prefix that all parameters annotated with Switch will be checked against. If not set,
      -
      will be used
      Parameters:
      prefix - New prefix to set
      Returns:
      This command handler
      Throws:
      IllegalArgumentException - if the prefix is empty
    • setFlagPrefix

      @NotNull @NotNull CommandHandler setFlagPrefix(@NotNull @NotNull String prefix)
      Sets the prefix that all parameters annotated with Flag will be checked against. If not set,
      -
      will be used
      Parameters:
      prefix - New prefix to set
      Returns:
      This command handler
      Throws:
      IllegalArgumentException - if the prefix is empty
    • setMessagePrefix

      @NotNull @NotNull CommandHandler setMessagePrefix(@NotNull @NotNull String prefix)
      Returns the prefix that should preced all messages sent with CommandActor.reply(String) and CommandActor.error(String).

      Note that the prefix will NOT be followed by spaces, it is your responsibility to include it in the prefix you set.

      Parameters:
      prefix - Message prefix
      Returns:
      This command handler
    • setHelpWriter

      @NotNull <T> @NotNull CommandHandler setHelpWriter(@NotNull @NotNull CommandHelpWriter<T> helpWriter)
      Sets the CommandHelpWriter responsible for generating help pages
      Type Parameters:
      T - The help entry type.
      Parameters:
      helpWriter - Help writer to use
      Returns:
      This command handler
      See Also:
    • disableStackTraceSanitizing

      @NotNull @NotNull CommandHandler disableStackTraceSanitizing()
      Disables stacktrace sanitization.

      By default, printed stack-trace is sanitized and stripped from internal, extra trace elements. This helps to keep the trace clean and readable, and removes away unnecessary paths. When disabled, full stacktrace will be printed.

      Returns:
      This command handler
    • failOnTooManyArguments

      @NotNull @NotNull CommandHandler failOnTooManyArguments()
      Sets the command to fail when too many arguments are specified in the command.
      Returns:
      This command handler
      See Also:
    • registerSenderResolver

      @NotNull @NotNull CommandHandler registerSenderResolver(@NotNull @NotNull SenderResolver resolver)
      Registers the given sender resolver, which resolves parameters at index 0 that may be potentially a custom sender implementation.

      See SenderResolver for more information.

      Parameters:
      resolver - Resolver to register
      Returns:
      This command handler
      See Also:
    • registerPermissionReader

      @NotNull @NotNull CommandHandler registerPermissionReader(@NotNull @NotNull PermissionReader reader)
      Registers the given permission reader, which allows registering custom CommandPermission implementations with annotations.
      Parameters:
      reader - Permission reader to register
      Returns:
      This command handler
      See Also:
    • registerValueResolver

      @NotNull <T> @NotNull CommandHandler registerValueResolver(@NotNull @NotNull Class<T> type, @NotNull @NotNull ValueResolver<T> resolver)
      Registers a parameter resolver that gets its value from the command arguments.

      See ValueResolver for more information

      Parameters:
      type - The parameter type to resolve
      resolver - The resolver
      Returns:
      This command handler
      See Also:
    • registerValueResolver

      @NotNull <T> @NotNull CommandHandler registerValueResolver(int priority, @NotNull @NotNull Class<T> type, @NotNull @NotNull ValueResolver<T> resolver)
      Registers a parameter resolver that gets its value from the command arguments.

      See ValueResolver for more information

      Parameters:
      priority - The resolver priority. Zero represents the highest.
      type - The parameter type to resolve
      resolver - The resolver
      Returns:
      This command handler
      See Also:
    • registerContextResolver

      @NotNull <T> @NotNull CommandHandler registerContextResolver(@NotNull @NotNull Class<T> type, @NotNull @NotNull ContextResolver<T> resolver)
      Registers a parameter resolver that gets its value from the command context.

      See ContextResolver for more information

      Parameters:
      type - The parameter type to resolve
      resolver - The resolver
      Returns:
      This command handler
      See Also:
    • registerContextResolver

      @NotNull <T> @NotNull CommandHandler registerContextResolver(int priority, @NotNull @NotNull Class<T> type, @NotNull @NotNull ContextResolver<T> resolver)
      Registers a parameter resolver that gets its value from the command context.

      See ContextResolver for more information

      Parameters:
      priority - The resolver priority. Zero represents the highest.
      type - The parameter type to resolve
      resolver - The resolver
      Returns:
      This command handler
      See Also:
    • registerContextValue

      @NotNull <T> @NotNull CommandHandler registerContextValue(@NotNull @NotNull Class<T> type, @Nullable T value)
      Registers a parameter type to always be a static value. This is useful for registering singletons as parameters.

      This is equivalent to calling registerContextResolver(type, ContextResolver.of(type))

      See ContextResolver for more information

      Parameters:
      type - The parameter type to resolve
      value - The value to retrun
      Returns:
      This command handler
      See Also:
    • registerContextValue

      @NotNull <T> @NotNull CommandHandler registerContextValue(int priority, @NotNull @NotNull Class<T> type, @NotNull T value)
      Registers a parameter type to always be a static value. This is useful for registering singletons as parameters.

      This is equivalent to calling registerContextResolver(priority, type, ContextResolver.of(value))

      See ContextResolver for more information

      Parameters:
      priority - The resolver priority. Zero represents the highest.
      type - The parameter type to resolve
      value - The value to retrun
      Returns:
      This command handler
      See Also:
    • registerValueResolverFactory

      @NotNull @NotNull CommandHandler registerValueResolverFactory(@NotNull @NotNull ValueResolverFactory factory)
      Registers a ValueResolverFactory to this handler
      Parameters:
      factory - Factory to register
      Returns:
      This command handler
      See Also:
    • registerValueResolverFactory

      @NotNull @NotNull CommandHandler registerValueResolverFactory(int priority, @NotNull @NotNull ValueResolverFactory factory)
      Registers a ValueResolverFactory to this handler
      Parameters:
      priority - The resolver priority. Zero represents the highest.
      factory - Factory to register
      Returns:
      This command handler
      See Also:
    • registerContextResolverFactory

      @NotNull @NotNull CommandHandler registerContextResolverFactory(@NotNull @NotNull ContextResolverFactory factory)
      Registers a ContextResolverFactory to this handler
      Parameters:
      factory - Factory to register
      Returns:
      This command handler
      See Also:
    • registerContextResolverFactory

      @NotNull @NotNull CommandHandler registerContextResolverFactory(int priority, @NotNull @NotNull ContextResolverFactory factory)
      Registers a ContextResolverFactory to this handler
      Parameters:
      priority - The resolver priority. Zero represents the highest.
      factory - Factory to register
      Returns:
      This command handler
      See Also:
    • registerCondition

      @NotNull @NotNull CommandHandler registerCondition(@NotNull @NotNull CommandCondition condition)
      Registers the specified condition in which all commands will be validated with.
      Parameters:
      condition - Condition to register
      Returns:
      This command handler
    • registerDependency

      @NotNull <T> @NotNull CommandHandler registerDependency(@NotNull @NotNull Class<T> type, @NotNull @NotNull Supplier<T> supplier)
      Registers a dependency for dependency injection.

      Any fields in the command class or instance with the Dependency annotation will have their value set from this supplier.

      Type Parameters:
      T - The dependency type
      Parameters:
      type - The dependency class type. This must match the field type.
      supplier - The dependency supplier
      Returns:
      This command handler
      See Also:
    • registerDependency

      @NotNull <T> @NotNull CommandHandler registerDependency(@NotNull @NotNull Class<T> type, T value)
      Registers a (static) dependency for dependency injection.

      Any fields in the command class or instance with the Dependency annotation will have their value set to this value.

      Type Parameters:
      T - The dependency type
      Parameters:
      type - The dependency class type. This must match the field type
      value - The dependency value
      Returns:
      This command handler
      See Also:
    • registerParameterValidator

      @NotNull <T> @NotNull CommandHandler registerParameterValidator(@NotNull @NotNull Class<T> type, @NotNull @NotNull ParameterValidator<T> validator)
      Registers a ParameterValidator for the specified parameter type. Parameter validators can access all information about a parameter, including the name and annotations.
      Type Parameters:
      T - The parameter type
      Parameters:
      type - The parameter type
      validator - The validator for this parameter
      Returns:
      This command handler
    • registerResponseHandler

      @NotNull <T> @NotNull CommandHandler registerResponseHandler(@NotNull @NotNull Class<T> responseType, @NotNull @NotNull ResponseHandler<T> handler)
      Registers a response handler for the specified response type. Response handlers do post-handling with results returned from command methods.

      Note that response handlers are captured by ExecutableCommands when they are registered, so they should be registered before the command itself is registered.

      Type Parameters:
      T - The response type
      Parameters:
      responseType - The response class
      handler - The response handler implementation
      Returns:
      This command handler
    • registerAnnotationReplacer

      @NotNull <T extends Annotation> @NotNull CommandHandler registerAnnotationReplacer(@NotNull @NotNull Class<T> annotationType, @NotNull @NotNull AnnotationReplacer<T> replacer)
      Registers an annotation replacer. Annotation replacers allow to dynamically generate annotations at runtime using APIs such as Annotations.
    • accept

      @NotNull @NotNull CommandHandler accept(@NotNull @NotNull CommandHandlerVisitor visitor)
      Accepts the given CommandHandlerVisitor and allows it to visit this command handler.
      Parameters:
      visitor - Visitor to accept
      Returns:
      This command handler
      See Also:
    • getAutoCompleter

      @NotNull @NotNull AutoCompleter getAutoCompleter()
      Returns the auto-completion handler of this command handler
      Returns:
      The auto-completion handler
    • getCommand

      ExecutableCommand getCommand(@NotNull @NotNull CommandPath path)
      Returns the given ExecutableCommand that matches the given path. This can return null if no command exists at such a path.

      Note that CommandPaths are, by default, case-insensitive.

      Parameters:
      path - Path to look for
      Returns:
      The command at the given path
    • getCategory

      CommandCategory getCategory(@NotNull @NotNull CommandPath path)
      Returns the given CommandCategory that matches the given path. This can return null if no category exists at such a path.

      Note that CommandPaths are, by default, case-insensitive.

      Parameters:
      path - Path to look for
      Returns:
      The category at the given path
    • getCommands

      @NotNull @NotNull @UnmodifiableView Map<CommandPath,ExecutableCommand> getCommands()
      Returns an unmodifiable view of all the registered commands in this command handler.
      Returns:
      The registered commands
    • getCategories

      @NotNull @NotNull @UnmodifiableView Map<CommandPath,CommandCategory> getCategories()
      Returns an unmodifiable view of all the registered categories in this command handler.
      Returns:
      The registered categories
    • getExceptionHandler

      @NotNull @NotNull CommandExceptionHandler getExceptionHandler()
      Returns the command exception handler currently used by this command handler
      Returns:
      The command exception handler
    • getMethodCallerFactory

      @NotNull @NotNull MethodCallerFactory getMethodCallerFactory()
      Returns the MethodCallerFactory responsible for generating reflective calls.
      Returns:
      The method caller factory
    • getHelpWriter

      <T> CommandHelpWriter<T> getHelpWriter()
      Returns the CommandHelpWriter of this command handler. This can be null if no writer is registered.
      Type Parameters:
      T - Command help entries type
      Returns:
      The help writer of this handler
    • unregister

      boolean unregister(@NotNull @NotNull CommandPath path)
      Unregisters the given path and all the sub-paths that belong to it
      Parameters:
      path - Path to unregister
      Returns:
      True if one or more elements were removed by this call.
    • unregister

      boolean unregister(@NotNull @NotNull String commandPath)
      Unregisters the given path and all the sub-paths that belong to it
      Parameters:
      commandPath - Path to unregister
      Returns:
      True if one or more elements were removed by this call.
    • unregisterAllCommands

      void unregisterAllCommands()
      Unregisters all the command instances in this command handler.
    • getRootPaths

      @NotNull @NotNull Set<CommandPath> getRootPaths()
      Returns all the root commands or categories paths in this command handler.
      Returns:
      All root command paths
    • getSwitchPrefix

      @NotNull @NotNull String getSwitchPrefix()
      Returns the prefix that comes before all Switch parameters when they are fetched from the command.
      Returns:
      The switch prefix
    • getFlagPrefix

      @NotNull @NotNull String getFlagPrefix()
      Returns the prefix that comes before all Flag parameters when they are fetched from the command.
      Returns:
      The switch prefix
    • getMessagePrefix

      @NotNull @NotNull String getMessagePrefix()
      Returns the prefix that preceds all messages sent by CommandActor.reply(String) and CommandActor.error(String).

      Set with setMessagePrefix(String)

      Returns:
      The message prefix
    • getDependency

      <T> Supplier<T> getDependency(@NotNull @NotNull Class<T> dependencyType)
      Returns the dependency registered for the given type
      Type Parameters:
      T - Dependency type
      Parameters:
      dependencyType - Dependency type to look for
      Returns:
      The dependency, or null if not found.
    • getDependency

      <T> Supplier<T> getDependency(@NotNull @NotNull Class<T> dependencyType, Supplier<T> def)
      Returns the dependency registered for the given type, otherwise returns the given def value.
      Type Parameters:
      T - Dependency type
      Parameters:
      dependencyType - Dependency type to look for
      def - Default value if no dependency is registered for the given type.
      Returns:
      The dependency, or null if not found.
    • dispatch

      @NotNull <T> @NotNull Optional<@Nullable T> dispatch(@NotNull @NotNull CommandActor actor, @NotNull @NotNull ArgumentStack arguments)
      Evaluates the command from the given arguments
      Parameters:
      actor - Actor to execute as
      arguments - Arguments to invoke the command with
      Returns:
      The result returned from invoking the command method. The optional value may be null if an exception was thrown.
    • dispatch

      @NotNull <T> @NotNull Optional<@Nullable T> dispatch(@NotNull @NotNull CommandActor actor, @NotNull @NotNull String commandInput)
      Evaluates the command from the given input
      Parameters:
      actor - Actor to execute as
      commandInput - Input to invoke with