-
Notifications
You must be signed in to change notification settings - Fork 31
Post Argument Subcommands
In most cases, commands are similar to functions - you specify the command, followed by all of its arguments. This is not always the case, however. There are times where, for the sake of clarity or simplicity, it makes more sense to place subcommands in between arguments.
To do this, you only need a few things. Let's imagine you need a command which can specify various operations to apply to a player, and you want to specify the player before the operation. Here's how that might be implemented:
player player:player {
kill {
postarg
hook kill
help Kills a player
}
message string:message {
postarg
hook message
help Messages a player
}
}
The postarg
tag is used to indicate that a subcommand should appear after the arguments of its parent command, and should take its parent's arguments in its method hook as well. When defining a post-argument subcommand, your method hook must take the CommandSender as usual, followed by arguments and context for the parent command, followed by its own arguments and context. Here's what that would look like for these two methods:
@CommandHook("kill")
public void kill(CommandSender sender, Player player) {
player.damage(99999);
}
@CommandHook("message")
public void message(CommandSender sender, Player player, String message) {
player.sendMessage(message);
}
These commands could now be run like /player Redempt kill
and /player Redempt message hi!
respectively.
Post-argument subcommands may also have argument subtypes as their first argument if and only if the parent command has the parent argument type as its last argument. An example would be if you have a home system, where the home
argument type is a subtype of player
. You could have a setup like this:
home player:player {
tp home:home {
postarg
hook tp
help Teleports to a player's home
}
}
This would work, and the command could be run like /home Redempt tp test
.