Class CommandManager<S>
java.lang.Object
io.github.projectunified.craftcommand.CommandManager<S>
- Type Parameters:
S- sender type
- Direct Known Subclasses:
BukkitCommandManager,PaperCommandManager,StandaloneCommandManager
Base command manager. Handles resolver registration, message formatting, wrapper instantiation, and error handling.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfilterSuggestions(Collection<String> suggestions, String current) Filters suggestions by prefix (case-insensitive).formatMessage(String key, String defaultValue, Object... args) Formats a message by key.getCommandInfo(Object commandInstance) Gets command metadata for a registered command instance.<T> ArgumentResolver<S, T> getResolver(Class<T> type) Gets the resolver for a type.voidhandleException(S sender, Exception exception) Handles an exception thrown by a command.protected <W> WinstantiateWrapper(Object commandInstance, String suffix, Class<W> wrapperClassType) Instantiates a generated wrapper through the command class's own class loader.abstract voidRegisters a command.voidregisterProvider(Function<Class<?>, ArgumentResolver<S, ?>> provider) Registers a dynamic resolver provider.<T> voidregisterResolver(Class<T> type, ArgumentResolver<S, T> resolver) Registers a resolver for a specific type.<T> voidregisterSenderResolver(Class<T> type, Function<S, T> resolver) Registers a global sender resolver.protected voidregisterWrapper(Object commandInstance, BaseCommand wrapper) Associates a command instance with its generated wrapper.<T> TresolveParameter(S sender, Class<T> type, String[] args, int[] indexHolder, String paramName, boolean optional, String defaultValue) Resolves a parameter, advancing the index holder.<T> TresolveSender(Class<T> type, S sender) Resolves the sender to the target type using registered sender resolvers.protected abstract voidsendMessage(S sender, String message) Sends an already formatted message.voidsendMessage(S sender, String key, String defaultMessage, Object... args) Formats a message and sends it through the platform handler.
-
Constructor Details
-
CommandManager
public CommandManager()
-
-
Method Details
-
filterSuggestions
Filters suggestions by prefix (case-insensitive). -
register
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 instancesuffix- 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
Associates a command instance with its generated wrapper.- Parameters:
commandInstance- the original command instancewrapper- the generated command wrapper
-
getCommandInfo
Gets command metadata for a registered command instance.- Parameters:
commandInstance- the original command instance passed toregister(Object)- Returns:
- the command info list, or empty list if not found
-
formatMessage
Formats a message by key. Override to plug in translations.- Parameters:
key- the message keydefaultValue- the fallback templateargs- format arguments- Returns:
- the formatted message
-
sendMessage
Formats a message and sends it through the platform handler.- Parameters:
sender- the message recipientkey- the translation keydefaultMessage- the fallback message, formatted withargsargs- format arguments
-
sendMessage
Sends an already formatted message. Implemented by each platform manager.- Parameters:
sender- the message recipientmessage- the formatted message
-
handleException
Handles an exception thrown by a command. Default: send its message.- Parameters:
sender- the command senderexception- the exception
-
registerResolver
Registers a resolver for a specific type. -
registerProvider
Registers a dynamic resolver provider. -
registerSenderResolver
Registers a global sender resolver.- Parameters:
type- the target sender typeresolver- function that converts the base sender to the target type
-
resolveSender
Resolves the sender to the target type using registered sender resolvers.- Parameters:
type- the target sender typesender- the base sender- Returns:
- the resolved sender, or
nullif 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 missingException
-
getResolver
Gets the resolver for a type. Checks exact match, hierarchy, and providers.- Throws:
IllegalArgumentException- if no resolver is registered for the type
-