Commands

To create a command you have to extend your VCommand class. This abstract class will allow you to manage your command very easily

public class CommandExample extends VCommand {

	public CommandExample(Template plugin) {
		super(plugin);
	}

	@Override
	protected CommandType perform(Template plugin) {
		return null;
	}
}

The perform function will execute the command, you must return the type of the command, this enumeration will determine if the command has been executed correctly or not.

public class CommandExample extends VCommand {

	public CommandExample(Template plugin) {
		super(plugin);
		this.setPermission(Permission.TEMPLATE_EXAMPLE);
	}

	@Override
	protected CommandType perform(Template plugin) {
		message(this.sender, "Hello world");
		return CommandType.SUCCESS;
	}
}

So here you have a simple command that will send the message Hello world to the user who will execute the command and which has the template.example permission. Permissions are managed with an enumeration.

You can add subcommands very easily with the addSubCommand method.

public class CommandExampleTest extends VCommand {

	public CommandExampleTest(Template plugin) {
		super(plugin);
		this.addSubCommand("sub");	
	}

	@Override
	protected CommandType perform(Template plugin) {
		message(player, "Hey %s", player.getName());
		return CommandType.SUCCESS;
	}
}

Order the construtor of your command you must add this:

this.addSubCommand(new CommandExampleTest(plugin));

This gives us the command /example sub.

Register

Now that you have created your order you need to register it. For this you have two different ways to do it. The classic way, where you need to register your order in the plugin.yml. Or you can directly register your order from your plugin.

Register plugin.yml

So you have to add the command in the plugin.yml and then you have to call the method registerCommand(string, command) of the class CommandManager.

this.commandManager.registerCommand("example", new CommandExample(this));

Register bukkit injection

The fastest way to add a command is to directly add your command with the injection in bukkit. For this you have the method registerCommand(string, command, aliases).

You can call this method from your main class, a function with the same arguments exists in the ZUtils class.

this.registerCommand("example", new CommandExample(this), "exemple");

Constructor

In the builder you can put all the information of your command

Description

You can give a description to your command with the method setDescription.

this.setDescription("Beautiful command");
this.setDescription(Message.EXAMPLE_DESCRIPTION);

So you have a description directly in string and a description with the Message enumeration.

Permission

You can give a permission to your command with the method setPermission.

this.setPermission(Permission.TEMPLATE_EXAMPLE);
this.setPermission("template.example");

So you have a permission directly in string and a description with the Permission enumeration.

Console

You can disable access to the command for the console, by default the value is true.

this.setConsoleCanUse(false);

Required arguments

You can tell your command that arguments are required for the execution of the command.

this.addRequireArg("arg description");

You have to give the name of the argument as a parameter, the name will be used for the generation of the command syntax.

Optional arguments

You can tell your command that arguments are optional for the execution of the command.

this.addOptionalArg("arg descrition")

You have to give the name of the argument as a parameter, the name will be used for the generation of the command syntax.

Sub commands

The subCommand method will add an alias to your current command and will also allow you to add a subcommand.

this.addSubCommand("test"); // aliases
this.addSubCommand("test2");

this.addSubCommand(new CommandExampleTest3(plugin)); // sub commands
this.addSubCommand(new CommandExampleTest4(plugin));

Syntax

By default, the syntax will be generated automatically, but you can give the syntax with the method setSyntax.

this.setSyntax("/example <arg> [<sub>]");

Tab

You can also manage the tab completion. The plugin will automatically manage the tab completion with the sub commands. But for your arguments you have to manage it yourself.

this.setTabCompletor();

Ignore args / parents

You can ignore the argument and parent limit, this allows you to put as many arguments as possible in your command. Perfect for a broadcast command for example.

this.setIgnoreArgs(true);
this.setIgnoreParent(true);

Arguments

Maintenant que le constructeur de votre commande est terminé, vous devez gérer l'exécution de votre commande. Si votre commande contient des arguments obligatoires ou optionnels, vous pourrez les récupérer très facilement. Vous pouvez directement donner un type à vos arguments avec la classe Arguments.

For each type you can directly get a default value.

String str = this.argAsString(index);
String str = this.argAsString(index, defaultValue);

int value = this.argAsInteger(index);
int value = this.argAsInteger(index, defaultValue);

long value = this.argAsLong(index);
long value = this.argAsLong(index, defaultValue);

double value = this.argAsDouble(index);
double value = this.argAsDouble(index, defaultValue);

Player player = this.argAsPlayer(index);
Player player = this.argAsPlayer(index, defaultValue);

OfflinePlayer offlinePlayer = this.argAsOfflinePlayer(index);
OfflinePlayer offlinePlayer = this.argAsOfflinePlayer(index, defaultValue);

Location location = this.argAsLocation(index);
Location location = this.argAsLocation(index, defaultValue);

EntityType entityType = this.argAsEntityType(index);
EntityType entityType = this.argAsEntityType(index, defaultValue);

World world = this.argAsWorld(index);
World world = this.argAsWorld(index, defaultValue);

Be careful, the index always starts at 0 no matter where your command is. If you have the command /example test sub <need> [<optional>]. The need argument will be 0 and the optional argument will be 1.

Tab

To enable tab completion you need to add setTabCompletor(); to the constructor of your command.

Then you have to override the toTab function.

@Override
public List<String> toTab(Template plugin, CommandSender sender, String[] args) {
		return null;
}

You have to check the content of the array and return a string list.

Last updated