Class Command

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

public final class Command extends Object
Represents a complete command with its tree structure.

Commands are built using a fluent builder API similar to Brigadier. The command tree consists of literal nodes (fixed text) and argument nodes (typed parameters).

Example Usage


 Command gamemode = Command.literal("gamemode")
     .requires("server.gamemode")
     .then(Command.literal("survival")
         .executes(ctx -> {
             ctx.getSender().sendMessage("Switching to survival mode");
             return CommandResult.SUCCESS;
         }))
     .then(Command.literal("creative")
         .executes(ctx -> {
             ctx.getSender().sendMessage("Switching to creative mode");
             return CommandResult.SUCCESS;
         }))
     .then(Command.argument("mode", StringArgumentType.word())
         .then(Command.argument("player", StringArgumentType.word())
             .executes(ctx -> {
                 String mode = ctx.getArgument("mode", String.class);
                 String player = ctx.getArgument("player", String.class);
                 ctx.getSender().sendMessage("Setting " + player + " to " + mode);
                 return CommandResult.SUCCESS;
             })))
     .build();

 // Execute the command
 CommandResult result = command.execute(sender, "gamemode creative");

 // Get suggestions
 Suggestions suggestions = command.getSuggestions(sender, "gamemode c").join();
 
  • Method Details

    • getName

      public String getName()
      Returns the command name (the root literal).
      Returns:
      the command name
    • getRootNode

      public CommandNode.LiteralNode getRootNode()
      Returns the root node of the command tree.
      Returns:
      the root node
    • getDescription

      public String getDescription()
      Returns the command description.
      Returns:
      the description, or null if not set
    • getAliases

      public List<String> getAliases()
      Returns the command aliases.
      Returns:
      an unmodifiable list of aliases
    • getPermission

      public String getPermission()
      Returns the permission required to use this command.
      Returns:
      the permission, or null if no permission required
    • canUse

      public boolean canUse(CommandSender sender)
      Checks if the sender has permission to use this command.
      Parameters:
      sender - the command sender
      Returns:
      true if the sender can use this command
    • execute

      public CommandResult execute(CommandSender sender, String input) throws CommandException
      Executes the command with the given input.
      Parameters:
      sender - the command sender
      input - the command input (without the leading slash)
      Returns:
      the execution 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
    • getUsage

      public String getUsage()
      Returns the usage string for this command.
      Returns:
      the usage string
    • literal

      public static CommandNode.LiteralNode literal(String name)
      Creates a literal node for building commands.
      Parameters:
      name - the literal text
      Returns:
      a new literal node
    • argument

      public static <T> CommandNode.ArgumentNode<T> argument(String name, ArgumentType<T> type)
      Creates an argument node for building commands.
      Type Parameters:
      T - the argument's parsed type
      Parameters:
      name - the argument name
      type - the argument type
      Returns:
      a new argument node
    • builder

      public static Command.Builder builder(String name)
      Creates a builder for a command with the given name.
      Parameters:
      name - the command name (the root literal)
      Returns:
      a new builder
    • toString

      public String toString()
      Overrides:
      toString in class Object