Class CommandManager<S>

java.lang.Object
dev.ua.theroer.magicutils.commands.CommandManager<S>
Type Parameters:
S - sender type

public class CommandManager<S> extends Object
Manages registration, execution, and tab completion of commands.
  • Constructor Details

    • CommandManager

      public CommandManager(String permissionPrefix, String pluginName, CommandLogger logger, CommandPlatform<S> platform, TypeParserRegistry<S> typeParserRegistry)
      Constructs a new CommandManager.
      Parameters:
      permissionPrefix - the prefix for permissions
      pluginName - the plugin name for namespaced commands
      logger - command logger implementation
      platform - platform adapter
      typeParserRegistry - type parser registry
  • Method Details

    • register

      public void register(MagicCommand command, CommandInfo info)
      Registers a command and its info.
      Parameters:
      command - the command instance
      info - the command info annotation
    • describe

      @Nullable public @Nullable ResolvedCommandSchema describe(String name)
      Returns the resolved command schema for the provided command label.
      Parameters:
      name - command name, alias, or namespaced label
      Returns:
      resolved schema or null when the command is not registered
    • describe

      @Nullable public @Nullable ResolvedCommandSchema describe(MagicCommand command, CommandInfo info)
      Returns the resolved command schema for a registered command instance.
      Parameters:
      command - command instance
      info - resolved command info
      Returns:
      immutable resolved schema or null when the command is unknown
    • execute

      public CommandResult execute(String name, S sender, List<String> args)
      Executes a command by name.
      Parameters:
      name - the command name
      sender - the command sender
      args - the command arguments
      Returns:
      the result of command execution
    • executeAsync

      public CompletableFuture<CommandResult> executeAsync(String name, S sender, List<String> args, Executor executor)
      Executes a command asynchronously on the provided executor.
      Parameters:
      name - command name
      sender - sender handle
      args - command arguments
      executor - executor to run on (uses common pool when null)
      Returns:
      future with command result
    • executeOrThrow

      public CommandResult executeOrThrow(String name, S sender, List<String> args) throws CommandExecutionException
      Executes a command, optionally bubbling unexpected failures.
      Parameters:
      name - command name
      sender - sender handle
      args - command arguments
      Returns:
      command result
      Throws:
      CommandExecutionException - when execution fails unexpectedly and bubbling is enabled
    • resolveThreading

      public CommandThreading resolveThreading(String name, List<String> args)
      Resolves the threading policy for the targeted command action.
      Parameters:
      name - command name
      args - command arguments
      Returns:
      resolved threading policy
    • canAccessCommand

      public boolean canAccessCommand(String name, S sender, @Nullable @Nullable String targetSubName)
      Checks if a sender can access the command (base permission or any sub/arg permission).
      Parameters:
      name - command label or alias
      sender - sender handle
      targetSubName - optional first subcommand token (for more accurate checks)
      Returns:
      true if accessible
    • canAccessSubCommandPath

      public boolean canAccessSubCommandPath(String name, S sender, List<String> pathSegments)
      Checks whether the sender can access a resolved subcommand path or any executable child under it.
      Parameters:
      name - command label or alias
      sender - sender handle
      pathSegments - path tokens using either canonical names or aliases
      Returns:
      true if the path resolves to an accessible node
    • isSenderArgument

      public boolean isSenderArgument(CommandArgument argument)
      Returns true when the argument represents the sender parameter.
      Parameters:
      argument - command argument
      Returns:
      true if sender argument
    • getSuggestions

      public List<String> getSuggestions(String name, S sender, List<String> args)
      Gets tab completion suggestions for a command.
      Parameters:
      name - the command name
      sender - the command sender
      args - the command arguments
      Returns:
      a list of suggestions
    • generateUsage

      public String generateUsage(MagicCommand command, CommandInfo info)
      Generates a usage string for a command based on its structure.
      Parameters:
      command - the command instance
      info - the command info
      Returns:
      formatted usage string
    • generateSubCommandUsages

      public List<String> generateSubCommandUsages(MagicCommand command, CommandInfo info)
      Generates detailed usage strings for all subcommands.
      Parameters:
      command - the command instance
      info - the command info
      Returns:
      list of usage strings for each subcommand
    • formatArguments

      public String formatArguments(List<CommandArgument> arguments)
      Formats argument list for usage display, skipping sender parameters.
      Parameters:
      arguments - arguments to format
      Returns:
      formatted argument string without leading/trailing spaces
    • formatArguments

      public String formatArguments(List<CommandArgument> arguments, boolean forceOptional)
      Formats argument list for usage display, skipping sender parameters.
      Parameters:
      arguments - arguments to format
      forceOptional - whether to force all arguments to be optional
      Returns:
      formatted argument string without leading/trailing spaces
    • isRegistered

      public boolean isRegistered(String name)
      Checks if a command is registered.
      Parameters:
      name - the command name
      Returns:
      true if registered
    • getAll

      public Collection<MagicCommand> getAll()
      Gets all registered commands.
      Returns:
      a collection of MagicCommand
    • getCommandInfo

      public CommandInfo getCommandInfo(MagicCommand command)
      Gets stored command info for a registered command instance.
      Parameters:
      command - command instance
      Returns:
      command info or null
    • getCommand

      public MagicCommand getCommand(String name)
      Gets a command by name or alias.
      Parameters:
      name - command name
      Returns:
      command instance or null