Skip to content

Command Flags

Redempt edited this page Dec 23, 2020 · 2 revisions

If you're familiar with the Linux command line, you're probably aware that many of them use a different type of argument called flags. Flags can be placed anywhere in a command rather than being locked to a specific order, and are optional.

RedLib's command manager supports these, in two forms: Boolean flags, and argument flags, which are treated differently. To create a flag, make a normal argument, but start its name with a dash:

teleport --silent player:player {
	help Teleports you to another player, optionally without notifying them
	user player
	hook teleport
}

This is an example of a boolean flag. Its type is boolean, but it is implied and omitted, so this is functionally identical to boolean:--silent. The method hook for this command would look like this:

@CommandHook("teleport")
public void teleport(Player sender, boolean silent, Player target) {
	sender.teleport(target);
	if (!silent) {
		target.sendMessage(ChatColor.GOLD + sender.getName() + " teleported to you!");
	}

}

Just like regular command arguments, you take them in the order they appear in the argument list for your method. Boolean flags will be passed as true if the flag was used, and false otherwise. The command using this flag could look like either /teleport Redempt --silent or /teleport --silent Redempt.

In many cases, a flag will need some other information. This is where argument flags come in. They're defined just like boolean ones, but with any other type.

spawn entitytype:type int:--count(1) {
	help Spawns one or several entities
	user player
	hook spawnentity
}

For this example, you'll notice that the argument flag is able to specify a default value in case the flag is not used. The ? modifier can't be used on flags, since they are always optional. If this command were run, assuming the argument type for entitytype is based on the EntityType enum, it would look like this: /spawn ZOMBIE --count 50. 50 would be passed for the parameter in the method hook, and if the flag is not used, the default value of 1 will be passed instead. Similarly to optional arguments, if no value is provided in the command and no default value is specified, it will pass null.

You can also have aliases for flags, similarly to how you would define command aliases:

spawn entitytype:type int:--count,-c(1) {
	help Spawns one or several entities
	user player
	hook spawnentity
}

And now the flag can be used as -c or --count.

Clone this wiki locally