Package dev.polv.taleapi.command
Class CommandRegistry
java.lang.Object
dev.polv.taleapi.command.CommandRegistry
Central registry for all commands.
Commands are registered through the CommandRegisterCallback event
and dispatched through this registry.
Example Usage
// Register during the command registration event
CommandRegisterCallback.EVENT.register(registry -> {
registry.register(Command.builder("hello")
.executes(ctx -> {
ctx.getSender().sendMessage("Hello, World!");
return CommandResult.SUCCESS;
})
.build());
});
// Execute a command
CommandResult result = registry.dispatch(sender, "hello");
// Get suggestions
Suggestions suggestions = registry.getSuggestions(sender, "hel").join();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all registered commands.dispatch(CommandSender sender, String input) Dispatches a command from the given input.getAvailableCommands(CommandSender sender) Returns a list of available commands for the sender.getCommand(String name) Gets a command by name or alias.Returns all registered command names.Returns all registered commands.getSuggestions(CommandSender sender, String input) Gets autocompletion suggestions for the given input.booleanhasCommand(String name) Checks if a command is registered.voidRegisters a command.intsize()Returns the number of registered commands.booleanunregister(String name) Unregisters a command by name.
-
Constructor Details
-
CommandRegistry
public CommandRegistry()Creates a new empty command registry.
-
-
Method Details
-
register
Registers a command.- Parameters:
command- the command to register- Throws:
IllegalArgumentException- if a command with the same name or alias is already registered
-
unregister
Unregisters a command by name.- Parameters:
name- the command name- Returns:
trueif the command was found and removed
-
getCommand
Gets a command by name or alias.- Parameters:
name- the command name or alias- Returns:
- an Optional containing the command, or empty if not found
-
getCommands
Returns all registered commands.- Returns:
- an unmodifiable collection of commands
-
getCommandNames
Returns all registered command names.- Returns:
- an unmodifiable set of command names
-
hasCommand
Checks if a command is registered.- Parameters:
name- the command name or alias- Returns:
trueif the command exists
-
dispatch
Dispatches a command from the given input.This method fires the
CommandExecuteCallbackevent before executing the command. If the event is cancelled, the command will not be executed and this method returnsCommandResult.FAILURE.- Parameters:
sender- the command senderinput- the full command input (with or without leading slash)- Returns:
- the command result
- Throws:
CommandException- if parsing or execution fails
-
getSuggestions
Gets autocompletion suggestions for the given input.- Parameters:
sender- the command senderinput- the partial input- Returns:
- a future completing with suggestions
-
size
public int size()Returns the number of registered commands.- Returns:
- the command count
-
clear
public void clear()Removes all registered commands. -
getAvailableCommands
Returns a list of available commands for the sender.- Parameters:
sender- the command sender- Returns:
- a list of commands the sender can use
-