Class CommandManager

java.lang.Object
com.ultikits.ultitools.manager.CommandManager

public class CommandManager extends Object
Command manager.
  • Field Details

    • commandListMap

      private final Map<UltiToolsPlugin,List<org.bukkit.command.Command>> commandListMap
    • externalCommandMap

      private final Map<String,List<org.bukkit.command.Command>> externalCommandMap
  • Constructor Details

    • CommandManager

      public CommandManager()
  • Method Details

    • register

      public void register(UltiToolsPlugin plugin, Class<? extends org.bukkit.command.CommandExecutor> clazz, String permission, String description, String... aliases)
      Manually register a command. Only used to register classes annotated with @CmdExecutor. Dependencies will be injected automatically.
      Parameters:
      plugin - UltiTools Plugin instance
      clazz - Command executor class
      permission - Permission
      description - Description
      aliases - Aliases
    • register

      private void register(UltiToolsPlugin plugin, org.bukkit.command.CommandExecutor commandExecutor, String permission, String description, String... aliases)
      Manually register a command, will not be managed by the container. Dependencies will not be injected automatically.
      Parameters:
      plugin - UltiTools Plugin instance
      commandExecutor - Command executor instance
      permission - Permission
      description - Description
      aliases - Aliases
    • register

      public void register(UltiToolsPlugin plugin, Class<? extends org.bukkit.command.CommandExecutor> clazz)
      Manually register a command. Only used to register classes annotated with @CmdExecutor. Dependencies will be injected automatically.
      Parameters:
      plugin - UltiTools Plugin instance
      clazz - Command executor class
    • register

      private void register(UltiToolsPlugin plugin, org.bukkit.command.CommandExecutor commandExecutor)
      Manually register a command, will not be managed by the container. Dependencies will not be injected automatically.
      Parameters:
      plugin - UltiTools Plugin instance
      commandExecutor - Command executor instance
    • registerCommandDirect

      private org.bukkit.command.PluginCommand registerCommandDirect(org.bukkit.command.CommandExecutor commandExecutor, String permission, String description, String... aliases)
      Registers commandExecutor with Bukkit's command map directly, with no plugin container involvement -- the primitive behind registerCoreCommand(CommandExecutor), registerExternalCommand(String, CommandExecutor, CmdExecutor), and the unannotated-class fallback in register(UltiToolsPlugin, CommandExecutor).

      Returns the exact PluginCommand instance handed to CommandMap.register(String, Command) so callers that need to unregister it later (e.g. register(UltiToolsPlugin, CommandExecutor, String, String, String...), registerExternalCommand(String, CommandExecutor, CmdExecutor)) can store and unregister THAT instance directly, rather than reconstructing a separate getCommand(String, Plugin) decoy that was never actually registered (Codex review on PR #457, plan 16-02: a name/namespaced-key relookup at unregister time can resolve a DIFFERENT module's command if two modules share both the framework's fallback prefix and the same primary alias).

      Parameters:
      commandExecutor - Command executor instance
      permission - Permission
      description - Description
      aliases - Aliases
      Returns:
      the actual registered PluginCommand instance
    • registerCommandDirect

      private void registerCommandDirect(org.bukkit.command.CommandExecutor commandExecutor)
      Resolves commandExecutor's @CmdExecutor annotation (if present) and dispatches to registerCommandDirect(CommandExecutor, String, String, String...); logs and does nothing otherwise. The registered command is not tracked for later unregistration -- this is the core-command path (registerCoreCommand(CommandExecutor)), whose commands live for the whole plugin lifetime.
      Parameters:
      commandExecutor - Command executor instance
    • registerAll

      public void registerAll(UltiToolsPlugin plugin)
      Register all classes annotated with @CmdExecutor in the specified package. Dependencies will be injected automatically.

      Beans are enumerated by name, so an executor bound under several names (a @Bean(name = {primary, alias}) product) is reached once per name; it is registered with Bukkit once, on the first name (C50).

      Parameters:
      plugin - UltiTools Plugin instance
    • getPluginByCommand

      public UltiToolsPlugin getPluginByCommand(org.bukkit.command.Command command)
      Get the plugin instance by command.
      Parameters:
      command - Command
      Returns:
      UltiTools plugin
    • unregister

      public void unregister(String name)
      Unregisters the live command labelled name from Bukkit, including its entries in the command map's known-commands table.

      This name-only overload is a best-effort public entry point for callers that hold nothing but a label (no Command reference to hand back). It resolves the actual currently-registered Command via its namespaced key ( fallbackPrefix:name) rather than the bare name -- SimpleCommandMap.register lets a later, overridable registration steal the bare-label slot, so getCommand(name) could resolve a different plugin's command after such a takeover, while the namespaced key is put unconditionally regardless of any bare-label conflict.

      That said, the namespaced key is only unique to this framework's own registrations: two different UltiTools modules sharing the same primary alias also share this framework's fallback prefix, so fallbackPrefix:name collides between them too, and this overload cannot tell them apart (Codex review on PR #457, plan 16-02). unregisterAll(UltiToolsPlugin) and unregisterAllExternal(String) do not have this ambiguity -- they hold the actual registered Command instance from commandListMap/externalCommandMap and unregister it directly via unregisterCommand(CommandMap, Command), bypassing name resolution entirely.

      Parameters:
      name - Command name
    • unregisterCommand

      private void unregisterCommand(org.bukkit.command.CommandMap commandMap, org.bukkit.command.Command command)
      Unregisters the exact command instance from Bukkit: purges its entries from the command map's known-commands table, then clears its own registration state.

      This is the airtight form -- no name or key resolution is involved, so it cannot mis-resolve a different module's command the way unregister(String)'s namespaced-key lookup can when two modules share both the framework's fallback prefix and a primary alias.

      Parameters:
      commandMap - the live Bukkit command map
      command - the specific, already-resolved command instance to unregister
    • namespacedKey

      private String namespacedKey(String label)
      Builds the namespaced known-commands key (fallbackPrefix:label) that registerCommandDirect(CommandExecutor, String, String, String...)'s CommandMap.register(String, Command) call always populates for label, regardless of whether the bare label slot was won or lost to a bare-label conflict. The fallback prefix mirrors registerCommandDirect's own: UltiTools.getInstance().getDescription().getName().
      Parameters:
      label - the command's bare label or alias
      Returns:
      the namespaced key that uniquely identifies this framework's own registration
    • removeFromKnownCommands

      private void removeFromKnownCommands(org.bukkit.command.CommandMap commandMap, org.bukkit.command.Command command)
      Removes every entry from commandMap's known-commands table whose value is exactly command (reference identity, not key/label text matching).

      Command.unregister(CommandMap) only clears the command's own commandMap/label/activeAliases fields (verified by reading its bytecode against this Paper version) -- it never touches CommandMap.getKnownCommands(). SimpleCommandMap.register(String, String, Command) puts a command under FOUR kinds of keys pointing at the same instance: the bare label, every bare alias, fallbackPrefix:label, and fallbackPrefix:alias for each alias. Left alone, every one of those keys keeps resolving to the unregistered module's executor after unload (measured on a real Paper 1.21.11 server: /upm uninstall followed by the module's own command still executing -- PR #457 real-machine UAT, plan 16-02).

      Matching by reference identity rather than by key text is deliberate: Bukkit allows a later-registered overridable command to take over an earlier command's bare label slot, in which case that key's VALUE is a different Command instance and must be left untouched -- only entries whose value is command itself are removed.

      Parameters:
      commandMap - the live Bukkit command map
      command - the specific command instance to purge
    • unregisterAll

      public void unregisterAll(UltiToolsPlugin plugin)
      Unregisters every command this framework registered for plugin, using the actual registered Command instances tracked in commandListMap (see register(UltiToolsPlugin, CommandExecutor, String, String, String...)) -- not a name-based relookup, which could mis-resolve a different module's command if two modules share a primary alias under this framework's shared fallback prefix.

      Once those commands are released, plugin is no longer tracked: this manager then references neither the unloaded module nor its command objects, and getPluginByCommand(Command) can no longer return it. A later registration for the same instance starts a new list.

      Parameters:
      plugin - UltiTools Plugin instance
    • close

      public void close()
      Unregister all commands.
    • registerCoreCommand

      public void registerCoreCommand(org.bukkit.command.CommandExecutor commandExecutor)
      Register command for core UltiTools commands that don't belong to a specific plugin module. This method is specifically for commands that are part of the main UltiTools plugin.
      Parameters:
      commandExecutor - Command executor instance
    • registerAllExternal

      public void registerAllExternal(ExternalPluginAdapter adapter)
      Register all @CmdExecutor commands from an external plugin's IoC container. Uses raw description (no i18n) since external plugins don't have UltiTools i18n. An executor bound under several names is registered once (C50), as in registerAll(UltiToolsPlugin).
      Parameters:
      adapter - the external plugin adapter
      Since:
      6.2.2
    • unregisterAllExternal

      public void unregisterAllExternal(String pluginName)
      Unregister all commands for an external plugin, using the actual registered Command instances tracked in externalCommandMap (see registerExternalCommand(String, CommandExecutor, CmdExecutor)) -- not a name-based relookup, for the same reason as unregisterAll(UltiToolsPlugin).
      Parameters:
      pluginName - the external plugin name
      Since:
      6.2.2
    • registerExternalCommand

      private void registerExternalCommand(String pluginName, org.bukkit.command.CommandExecutor executor, CmdExecutor annotation)
    • getCommand

      private org.bukkit.command.PluginCommand getCommand(String name, org.bukkit.plugin.Plugin plugin)
    • getCommandMap

      private org.bukkit.command.CommandMap getCommandMap()