Class CommandContext

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

public final class CommandContext extends Object
Contains the context for a command execution.

This includes the command sender, the raw input, and all parsed arguments. Arguments are accessed by their registered names and are type-safe.

Example Usage


 Command.literal("teleport")
     .then(Command.argument("player", StringArgumentType.word()))
     .then(Command.argument("x", IntegerArgumentType.integer()))
     .then(Command.argument("y", IntegerArgumentType.integer()))
     .then(Command.argument("z", IntegerArgumentType.integer()))
     .executes(ctx -> {
         String playerName = ctx.getArgument("player", String.class);
         int x = ctx.getArgument("x", Integer.class);
         int y = ctx.getArgument("y", Integer.class);
         int z = ctx.getArgument("z", Integer.class);
         ctx.getSender().sendMessage("Teleporting " + playerName + " to " + x + ", " + y + ", " + z);
         return CommandResult.SUCCESS;
     })
     .build();
 
  • Method Details

    • getSender

      public CommandSender getSender()
      Returns the command sender who executed this command.
      Returns:
      the command sender
    • getRawInput

      public String getRawInput()
      Returns the raw input string that was parsed.
      Returns:
      the raw command input
    • getCommand

      public Command getCommand()
      Returns the command that is being executed.
      Returns:
      the command, or null if not set
    • getArgument

      public <T> T getArgument(String name, Class<T> clazz)
      Gets a parsed argument by name.
      Type Parameters:
      T - the argument type
      Parameters:
      name - the argument name as registered in the command
      clazz - the expected type of the argument
      Returns:
      the parsed argument value
      Throws:
      IllegalArgumentException - if no argument exists with that name
      ClassCastException - if the argument is not of the expected type
    • getOptionalArgument

      public <T> Optional<T> getOptionalArgument(String name, Class<T> clazz)
      Gets a parsed argument by name, returning an Optional.
      Type Parameters:
      T - the argument type
      Parameters:
      name - the argument name as registered in the command
      clazz - the expected type of the argument
      Returns:
      an Optional containing the argument value, or empty if not present
    • hasArgument

      public boolean hasArgument(String name)
      Checks if an argument with the given name exists.
      Parameters:
      name - the argument name
      Returns:
      true if the argument exists
    • getArguments

      public Map<String,Object> getArguments()
      Returns all parsed arguments as an unmodifiable map.
      Returns:
      the arguments map
    • builder

      public static CommandContext.Builder builder(CommandSender sender, String rawInput)
      Creates a new builder for constructing a CommandContext.
      Parameters:
      sender - the command sender
      rawInput - the raw command input
      Returns:
      a new builder instance