Package dev.polv.taleapi.command
Class CommandContext
java.lang.Object
dev.polv.taleapi.command.CommandContext
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();
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for constructingCommandContextinstances. -
Method Summary
Modifier and TypeMethodDescriptionstatic CommandContext.Builderbuilder(CommandSender sender, String rawInput) Creates a new builder for constructing a CommandContext.<T> TgetArgument(String name, Class<T> clazz) Gets a parsed argument by name.Returns all parsed arguments as an unmodifiable map.Returns the command that is being executed.<T> Optional<T>getOptionalArgument(String name, Class<T> clazz) Gets a parsed argument by name, returning an Optional.Returns the raw input string that was parsed.Returns the command sender who executed this command.booleanhasArgument(String name) Checks if an argument with the given name exists.
-
Method Details
-
getSender
Returns the command sender who executed this command.- Returns:
- the command sender
-
getRawInput
Returns the raw input string that was parsed.- Returns:
- the raw command input
-
getCommand
Returns the command that is being executed.- Returns:
- the command, or
nullif not set
-
getArgument
Gets a parsed argument by name.- Type Parameters:
T- the argument type- Parameters:
name- the argument name as registered in the commandclazz- the expected type of the argument- Returns:
- the parsed argument value
- Throws:
IllegalArgumentException- if no argument exists with that nameClassCastException- if the argument is not of the expected type
-
getOptionalArgument
Gets a parsed argument by name, returning an Optional.- Type Parameters:
T- the argument type- Parameters:
name- the argument name as registered in the commandclazz- the expected type of the argument- Returns:
- an Optional containing the argument value, or empty if not present
-
hasArgument
Checks if an argument with the given name exists.- Parameters:
name- the argument name- Returns:
trueif the argument exists
-
getArguments
Returns all parsed arguments as an unmodifiable map.- Returns:
- the arguments map
-
builder
Creates a new builder for constructing a CommandContext.- Parameters:
sender- the command senderrawInput- the raw command input- Returns:
- a new builder instance
-