Skip to main content
The Context class represents the execution context of a command, providing access to the command sender, arguments, and utilities for parsing arguments with custom providers.

Overview

Every command method and provider receives a Context instance, which contains:
  • The command sender who executed the command
  • Raw string arguments passed to the command
  • The command label that was used
  • Access to the Blade instance
  • Utilities for parsing arguments at specific indices

Class Definition

public final class Context
Package: me.vaperion.blade.context

Core Methods

Accessing Arguments

arguments()
String[]
Returns all provided arguments as a string array.
String[] args = context.arguments();
// args = ["player123", "10", "true"]
argument(int index)
String
Returns the argument at the specified index, or null if not present.
index
int
required
The zero-based index of the argument
String firstArg = context.argument(0); // "player123"
String missing = context.argument(10);  // null

Parsing Arguments

The Context class provides several overloaded methods for parsing arguments with custom providers.
parseArgument(int index, Class<T> argumentClass)
T
Parses the argument at the specified index using the registered provider for the given class.
index
int
required
The zero-based index of the argument to parse
argumentClass
Class<T>
required
The target type to parse the argument into
Returns: The parsed argument, or null if not presentThrows:
  • IllegalArgumentException - if no provider is registered for the class
  • BladeParseError - if parsing fails
  • BladeImplementationError - if the provider has implementation issues
// Parse the first argument as a Player
Player target = context.parseArgument(0, Player.class);

// Parse the second argument as an Integer
Integer amount = context.parseArgument(1, Integer.class);
parseArgument(int index, Class<T> argumentClass, String defaultValue)
T
Parses the argument at the specified index with a default value if not present.
index
int
required
The zero-based index of the argument to parse
argumentClass
Class<T>
required
The target type to parse the argument into
defaultValue
String
required
The default value to use if the argument is not present
Returns: The parsed argument, or the parsed default value if not present
// Use "10" as default if argument not provided
Integer amount = context.parseArgument(1, Integer.class, "10");
parseArgument(int index, ArgumentProvider<T> provider)
T
Parses the argument using a specific provider instance.
index
int
required
The zero-based index of the argument to parse
provider
ArgumentProvider<T>
required
The provider to use for parsing
Returns: The parsed argument, or null if not present
ArgumentProvider<Player> customProvider = new CustomPlayerProvider();
Player target = context.parseArgument(0, customProvider);
parseArgument(int index, ArgumentProvider<T> provider, String defaultValue)
T
Parses the argument using a specific provider with a default value.
index
int
required
The zero-based index of the argument to parse
provider
ArgumentProvider<T>
required
The provider to use for parsing
defaultValue
String
required
The default value to use if the argument is not present
Returns: The parsed argument, or the parsed default value if not present

Accessing Context Properties

sender()
Sender<?>
Returns the sender who executed the command.
Sender<?> sender = context.sender();
String senderName = sender.name();
boolean hasPermission = sender.hasPermission("admin.use");

// Parse to platform-specific type
Player player = sender.parseAs(Player.class);
if (player != null) {
    player.sendMessage("Hello!");
}
See Sender for more details.
label()
String
Returns the command label that was used to invoke the command.
String label = context.label();
// If command was invoked as "/tp" instead of "/teleport"
// label would be "tp"
blade()
Blade
Returns the Blade instance managing this command.
Blade blade = context.blade();

Sender Interface

The Sender<T> interface wraps platform-specific sender types.

Sender Methods

rawSender()
T
Returns the raw platform instance of the sender (e.g., Player, CommandSource).
Object raw = context.sender().rawSender();
name()
String
Returns the name of the sender.
String name = context.sender().name();
hasPermission(String permission)
boolean
Checks if the sender has a specific permission.
permission
String
required
The permission node to check
if (context.sender().hasPermission("mycommand.admin")) {
    // Execute admin logic
}
parseAs(Class<S> clazz)
S
Attempts to convert the sender to the specified type.
clazz
Class<S>
required
The target class to convert to
Returns: The converted sender if successful, null otherwise
Player player = context.sender().parseAs(Player.class);
if (player != null) {
    // Sender is a player
}

ConsoleCommandSender console = context.sender().parseAs(ConsoleCommandSender.class);
if (console != null) {
    // Sender is console
}

Usage in Providers

The Context is commonly used in argument providers to access the command context:
public class PlayerProvider implements ArgumentProvider<Player> {
    @Override
    public Player provide(@NotNull Context ctx, @NotNull InputArgument arg) {
        String value = arg.requireValue();
        
        // Access the sender
        Sender<?> sender = ctx.sender();
        
        // Use context to find player
        Player target = Bukkit.getPlayer(value);
        if (target == null) {
            throw BladeParseError.recoverable(
                "Player '" + value + "' not found."
            );
        }
        
        return target;
    }
    
    @Override
    public void suggest(@NotNull Context ctx,
                       @NotNull InputArgument arg,
                       @NotNull SuggestionsBuilder suggestions) {
        // Use context to get online players
        for (Player player : Bukkit.getOnlinePlayers()) {
            suggestions.suggest(player.getName());
        }
    }
}

Usage in Commands

Every command method receives a Context parameter:
@Command("teleport")
public void teleportCommand(Context context, Player target) {
    // Access the sender
    Player sender = context.sender().parseAs(Player.class);
    if (sender == null) {
        return; // Not a player
    }
    
    // Get the command label used
    String label = context.label(); // "tp" or "teleport"
    
    // Teleport
    sender.teleport(target.getLocation());
    sender.sendMessage("Teleported to " + target.getName());
}

Advanced: Manual Argument Parsing

You can manually parse arguments in commands using the parse methods:
@Command("custom")
public void customCommand(Context context) {
    // Get raw arguments
    String[] args = context.arguments();
    
    if (args.length < 2) {
        context.sender().parseAs(Player.class)
            .sendMessage("Usage: /custom <player> <amount>");
        return;
    }
    
    // Parse arguments manually
    Player target = context.parseArgument(0, Player.class);
    Integer amount = context.parseArgument(1, Integer.class, "10");
    
    if (target == null) {
        // Parsing failed
        return;
    }
    
    // Use parsed values
    target.getInventory().addItem(new ItemStack(Material.DIAMOND, amount));
}

See Also

Build docs developers (and LLMs) love