Commands
PluginLib offers a simple way to create your commands and handles a few errors in the process

Creating a Command

To create a command within your plugin, you can do the following

Add the command to your plugin.yml

1
name: FakePlugin
2
author: Darrionat
3
main: me.darrionat.FakePlugin
4
description: A fake plugin with commands
5
version: 1.0.0
6
api-version: 1.13
7
8
commands:
9
ban:
10
usage: /<command>
Copied!

Create a BaseCommand

1
package me.darrionat.fakeplugin.commands;
2
3
import org.bukkit.command.Command;
4
import org.bukkit.command.CommandSender;
5
6
import me.darrionat.pluginlib.Plugin;
7
import me.darrionat.pluginlib.commands.BaseCommand;
8
9
public class FakeCommand extends BaseCommand {
10
11
public FakeCommand(Plugin plugin) {
12
super(plugin);
13
}
14
15
@Override
16
public String getCommandLabel() {
17
return "ban";
18
}
19
20
@Override
21
protected void runNoArgs(CommandSender sender, Command command, String label, String[] args) {
22
/*
23
* Do stuff
24
*/
25
}
26
}
Copied!
Since the class above has no subcommands defined, it will always simply just run the runNoArgsmethod.

SubCommands

A SubCommand is defined as a command that uses the same label as its parent BaseCommand but it has a different first argument.
For example, your plugin has the base command /ban. You can add subcommands such as /ban list or /ban player.

Adding a SubCommand to your BaseCommand

To add a SubCommand to a BaseCommand, you can use BaseCommand#addSubCommand(SubCommand).
1
public FakeCommand(Plugin plugin) {
2
super(plugin);
3
addSubCommand(new FakeSubCommand(this, plugin));
4
}
Copied!

Example of a SubCommand Class

1
package me.darrionat.fakeplugin.commands;
2
3
import org.bukkit.command.CommandSender;
4
5
import me.darrionat.pluginlib.Plugin;
6
import me.darrionat.pluginlib.commands.BaseCommand;
7
import me.darrionat.pluginlib.commands.SubCommand;
8
9
public class FakeSubCommand extends SubCommand {
10
11
public FakeSubCommand(BaseCommand parentCommand, Plugin plugin) {
12
super(parentCommand, plugin);
13
}
14
15
@Override
16
public String getSubCommand() {
17
// /ban list
18
return "list";
19
}
20
21
@Override
22
public int getRequiredArgs() {
23
/*
24
* /ban list <- one argument
25
* This should always be greater than or equal to 1
26
*/
27
return 1;
28
}
29
30
@Override
31
public boolean onlyPlayers() {
32
return false;
33
}
34
35
@Override
36
protected void runCommand(CommandSender sender, String[] args) {
37
/*
38
* Do stuff
39
*/
40
}
41
}
Copied!
Last modified 9mo ago