Class CommandManager<S>

java.lang.Object
io.github.projectunified.craftcommand.CommandManager<S>
Type Parameters:
S - sender type
Direct Known Subclasses:
BukkitCommandManager, PaperCommandManager, StandaloneCommandManager

public abstract class CommandManager<S> extends Object
Base command manager. Handles resolver registration, message formatting, wrapper instantiation, and error handling.
  • Constructor Details

    • CommandManager

      public CommandManager()
  • Method Details

    • filterSuggestions

      public static List<String> filterSuggestions(Collection<String> suggestions, String current)
      Filters suggestions by prefix (case-insensitive).
    • register

      public abstract void register(Object command)
      Registers a command.
    • instantiateWrapper

      protected <W> W instantiateWrapper(Object commandInstance, String suffix, Class<W> wrapperClassType) throws Throwable
      Instantiates a generated wrapper through the command class's own class loader.

      Resolving the wrapper with that loader keeps addon commands and wrappers in the same class-loader scope.

      Type Parameters:
      W - the wrapper type
      Parameters:
      commandInstance - the original command instance
      suffix - the wrapper class suffix (e.g. "$StandaloneCommand")
      wrapperClassType - the expected wrapper class or interface
      Returns:
      the instantiated wrapper
      Throws:
      Throwable - if the wrapper cannot be loaded or constructed
    • registerWrapper

      protected void registerWrapper(Object commandInstance, BaseCommand wrapper)
      Associates a command instance with its generated wrapper.
      Parameters:
      commandInstance - the original command instance
      wrapper - the generated command wrapper
    • getCommandInfo

      public List<CommandInfo> getCommandInfo(Object commandInstance)
      Gets command metadata for a registered command instance.
      Parameters:
      commandInstance - the original command instance passed to register(Object)
      Returns:
      the command info list, or empty list if not found
    • formatMessage

      public String formatMessage(String key, String defaultValue, Object... args)
      Formats a message by key. Override to plug in translations.
      Parameters:
      key - the message key
      defaultValue - the fallback template
      args - format arguments
      Returns:
      the formatted message
    • sendMessage

      public void sendMessage(S sender, String key, String defaultMessage, Object... args)
      Formats a message and sends it through the platform handler.
      Parameters:
      sender - the message recipient
      key - the translation key
      defaultMessage - the fallback message, formatted with args
      args - format arguments
    • sendMessage

      protected abstract void sendMessage(S sender, String message)
      Sends an already formatted message. Implemented by each platform manager.
      Parameters:
      sender - the message recipient
      message - the formatted message
    • handleException

      public void handleException(S sender, Exception exception)
      Handles an exception thrown by a command. Default: send its message.
      Parameters:
      sender - the command sender
      exception - the exception
    • registerResolver

      public <T> void registerResolver(Class<T> type, ArgumentResolver<S,T> resolver)
      Registers a resolver for a specific type.
    • registerProvider

      public void registerProvider(Function<Class<?>,ArgumentResolver<S,?>> provider)
      Registers a dynamic resolver provider.
    • registerSenderResolver

      public <T> void registerSenderResolver(Class<T> type, Function<S,T> resolver)
      Registers a global sender resolver.
      Parameters:
      type - the target sender type
      resolver - function that converts the base sender to the target type
    • resolveSender

      public <T> T resolveSender(Class<T> type, S sender)
      Resolves the sender to the target type using registered sender resolvers.
      Parameters:
      type - the target sender type
      sender - the base sender
      Returns:
      the resolved sender, or null if no resolver is registered
    • resolveParameter

      public <T> T resolveParameter(S sender, Class<T> type, String[] args, int[] indexHolder, String paramName, boolean optional, String defaultValue) throws Exception
      Resolves a parameter, advancing the index holder. Used by generated wrappers.
      Throws:
      CommandException - when a required parameter is missing
      Exception
    • getResolver

      public <T> ArgumentResolver<S,T> getResolver(Class<T> type)
      Gets the resolver for a type. Checks exact match, hierarchy, and providers.
      Throws:
      IllegalArgumentException - if no resolver is registered for the type