Registering annotation-based commands
Registering annotation-based commands is really simple. To do this, we use the following method, where className
is the name of a class with a @Command
annotation:
CommandAPI.registerCommand(className)
Example: Registering a Warp command
Say we have a simple command /warp
that is defined as follows:
@Command("warp")
public class WarpCommand {
// List of warp names and their locations
static Map<String, Location> warps = new HashMap<>();
@Default
public static void warp(CommandSender sender) {
sender.sendMessage("--- Warp help ---");
sender.sendMessage("/warp - Show this help");
sender.sendMessage("/warp <warp> - Teleport to <warp>");
sender.sendMessage("/warp create <warpname> - Creates a warp at your current location");
}
@Default
public static void warp(Player player, @AStringArgument String warpName) {
player.teleport(warps.get(warpName));
}
@Subcommand("create")
@Permission("warps.create")
public static void createWarp(Player player, @AStringArgument String warpName) {
warps.put(warpName, player.getLocation());
}
}
We can register this in our onLoad()
method so we can use this command from within Minecraft functions:
public class MyPlugin extends JavaPlugin {
@Override
public void onLoad() {
CommandAPI.registerCommand(WarpCommand.class);
}
}