Commands
To create a command you have to extend your VCommand class. This abstract class will allow you to manage your command very easily
1
public class CommandExample extends VCommand {
2
3
public CommandExample(Template plugin) {
4
super(plugin);
5
}
6
7
@Override
8
protected CommandType perform(Template plugin) {
9
return null;
10
}
11
}
Copied!
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.
1
public class CommandExample extends VCommand {
2
3
public CommandExample(Template plugin) {
4
super(plugin);
5
this.setPermission(Permission.TEMPLATE_EXAMPLE);
6
}
7
8
@Override
9
protected CommandType perform(Template plugin) {
10
message(this.sender, "Hello world");
11
return CommandType.SUCCESS;
12
}
13
}
Copied!
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.
1
public class CommandExampleTest extends VCommand {
2
3
public CommandExampleTest(Template plugin) {
4
super(plugin);
5
this.addSubCommand("sub");
6
}
7
8
@Override
9
protected CommandType perform(Template plugin) {
10
message(player, "Hey %s", player.getName());
11
return CommandType.SUCCESS;
12
}
13
}
Copied!
Order the construtor of your command you must add this:
1
this.addSubCommand(new CommandExampleTest(plugin));
Copied!
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.
1
this.commandManager.registerCommand("example", new CommandExample(this));
Copied!

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.
1
this.registerCommand("example", new CommandExample(this), "exemple");
Copied!

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.
1
this.setDescription("Beautiful command");
2
this.setDescription(Message.EXAMPLE_DESCRIPTION);
Copied!
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.
1
this.setPermission(Permission.TEMPLATE_EXAMPLE);
2
this.setPermission("template.example");
Copied!
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.
1
this.setConsoleCanUse(false);
Copied!

Required arguments

You can tell your command that arguments are required for the execution of the command.
1
this.addRequireArg("arg description");
Copied!
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.
1
this.addOptionalArg("arg descrition")
Copied!
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.
1
this.addSubCommand("test"); // aliases
2
this.addSubCommand("test2");
3
4
this.addSubCommand(new CommandExampleTest3(plugin)); // sub commands
5
this.addSubCommand(new CommandExampleTest4(plugin));
Copied!

Syntax

By default, the syntax will be generated automatically, but you can give the syntax with the method setSyntax.
1
this.setSyntax("/example <arg> [<sub>]");
Copied!

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.
1
this.setTabCompletor();
Copied!

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.
1
this.setIgnoreArgs(true);
2
this.setIgnoreParent(true);
Copied!

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.
1
String str = this.argAsString(index);
2
String str = this.argAsString(index, defaultValue);
3
4
int value = this.argAsInteger(index);
5
int value = this.argAsInteger(index, defaultValue);
6
7
long value = this.argAsLong(index);
8
long value = this.argAsLong(index, defaultValue);
9
10
double value = this.argAsDouble(index);
11
double value = this.argAsDouble(index, defaultValue);
12
13
Player player = this.argAsPlayer(index);
14
Player player = this.argAsPlayer(index, defaultValue);
15
16
OfflinePlayer offlinePlayer = this.argAsOfflinePlayer(index);
17
OfflinePlayer offlinePlayer = this.argAsOfflinePlayer(index, defaultValue);
18
19
Location location = this.argAsLocation(index);
20
Location location = this.argAsLocation(index, defaultValue);
21
22
EntityType entityType = this.argAsEntityType(index);
23
EntityType entityType = this.argAsEntityType(index, defaultValue);
24
25
World world = this.argAsWorld(index);
26
World world = this.argAsWorld(index, defaultValue);
Copied!
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.
1
@Override
2
public List<String> toTab(Template plugin, CommandSender sender, String[] args) {
3
return null;
4
}
Copied!
You have to check the content of the array and return a string list.
Last modified 4mo ago