Class CommandRegistry

java.lang.Object
dev.polv.taleapi.command.CommandRegistry

public final class CommandRegistry extends Object
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 Details

    • CommandRegistry

      public CommandRegistry()
      Creates a new empty command registry.
  • Method Details

    • register

      public void register(Command command)
      Registers a command.
      Parameters:
      command - the command to register
      Throws:
      IllegalArgumentException - if a command with the same name or alias is already registered
    • unregister

      public boolean unregister(String name)
      Unregisters a command by name.
      Parameters:
      name - the command name
      Returns:
      true if the command was found and removed
    • getCommand

      public Optional<Command> getCommand(String name)
      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

      public Collection<Command> getCommands()
      Returns all registered commands.
      Returns:
      an unmodifiable collection of commands
    • getCommandNames

      public Collection<String> getCommandNames()
      Returns all registered command names.
      Returns:
      an unmodifiable set of command names
    • hasCommand

      public boolean hasCommand(String name)
      Checks if a command is registered.
      Parameters:
      name - the command name or alias
      Returns:
      true if the command exists
    • dispatch

      public CommandResult dispatch(CommandSender sender, String input) throws CommandException
      Dispatches a command from the given input.

      This method fires the CommandExecuteCallback event before executing the command. If the event is cancelled, the command will not be executed and this method returns CommandResult.FAILURE.

      Parameters:
      sender - the command sender
      input - the full command input (with or without leading slash)
      Returns:
      the command result
      Throws:
      CommandException - if parsing or execution fails
    • getSuggestions

      public CompletableFuture<Suggestions> getSuggestions(CommandSender sender, String input)
      Gets autocompletion suggestions for the given input.
      Parameters:
      sender - the command sender
      input - 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

      public List<Command> getAvailableCommands(CommandSender sender)
      Returns a list of available commands for the sender.
      Parameters:
      sender - the command sender
      Returns:
      a list of commands the sender can use