Package dev.polv.taleapi.command
Class Command
java.lang.Object
dev.polv.taleapi.command.Command
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();
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> CommandNode.ArgumentNode<T>argument(String name, ArgumentType<T> type) Creates an argument node for building commands.static Command.BuilderCreates a builder for a command with the given name.booleancanUse(CommandSender sender) Checks if the sender has permission to use this command.execute(CommandSender sender, String input) Executes the command with the given input.Returns the command aliases.Returns the command description.getName()Returns the command name (the root literal).Returns the permission required to use this command.Returns the root node of the command tree.getSuggestions(CommandSender sender, String input) Gets autocompletion suggestions for the given input.getUsage()Returns the usage string for this command.static CommandNode.LiteralNodeCreates a literal node for building commands.toString()
-
Method Details
-
getName
Returns the command name (the root literal).- Returns:
- the command name
-
getRootNode
Returns the root node of the command tree.- Returns:
- the root node
-
getDescription
Returns the command description.- Returns:
- the description, or null if not set
-
getAliases
Returns the command aliases.- Returns:
- an unmodifiable list of aliases
-
getPermission
Returns the permission required to use this command.- Returns:
- the permission, or null if no permission required
-
canUse
Checks if the sender has permission to use this command.- Parameters:
sender- the command sender- Returns:
trueif the sender can use this command
-
execute
Executes the command with the given input.- Parameters:
sender- the command senderinput- the command input (without the leading slash)- Returns:
- the execution result
- Throws:
CommandException- if parsing or execution fails
-
getSuggestions
Gets autocompletion suggestions for the given input.- Parameters:
sender- the command senderinput- the partial input- Returns:
- a future completing with suggestions
-
getUsage
Returns the usage string for this command.- Returns:
- the usage string
-
literal
Creates a literal node for building commands.- Parameters:
name- the literal text- Returns:
- a new literal node
-
argument
Creates an argument node for building commands.- Type Parameters:
T- the argument's parsed type- Parameters:
name- the argument nametype- the argument type- Returns:
- a new argument node
-
builder
Creates a builder for a command with the given name.- Parameters:
name- the command name (the root literal)- Returns:
- a new builder
-
toString
-