Skip to main content

Create a Command

What is this?

With this feature, you no longer need the plugin.yml file.

Simply type the command name in the @Command annotation, and BasePlugin will handle everything for you. It also provides automatic tab completion for subcommands without any additional effort.

Example

Here is an example of a command with subcommands and automatic tab completion:

@Command(aliases = { "aliasexample" }, name = "examplecommand", usage = "/examplecommand", description = "This is an example command.")
public class ExampleCommand {

// Main command with no arguments
@CommandHandle(permission = "needed.permission", permissionMessage = "&cThis permission is required: needed.permission")
public void mainCommand(CommandSender sender, String[] args) {
sender.sendMessage("You used the main command with no arguments! You can also use /aliasexample to call this command.");
}

// Command to check if a player is online or offline
@CommandHandle(permission = "online.check", args = {"<%player%>"})
public void checkOnlineCommand(CommandSender sender, String[] args) {
boolean isOnline = Bukkit.getOfflinePlayer(args[0]).isOnline();
sender.sendMessage("The player is currently " + (isOnline ? "online" : "offline"));
}

// Tab completion for players (using a placeholder for player names)
@Placeholder(name = "player")
public List<String> getPlayers() {
return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());
}
}

Then, to register the command in the plugin:

BasePlugin.registerCommands(new ExampleCommand());

Explanation of Annotations:

@Command Annotation

The @Command annotation is used to define the main command. You can set several properties such as:

  • name: The name of the command (required).
  • description: A description of the command.
  • usage: How the command should be used, displayed in help messages.
  • aliases: Aliases for the command (optional).
  • tabComplete: Whether the command supports tab completion. By default, it's enabled.

@CommandHandle Annotation

The @CommandHandle annotation is used to define the behavior of subcommands. Each method annotated with @CommandHandle will be called based on the arguments passed in the main command.

  • args: The arguments expected for the subcommand (e.g., player name).
  • permission: The permission required to execute the subcommand.
  • permissionMessage: The message sent to the player if they lack the required permission.

@Placeholder Annotation

The @Placeholder annotation is used to define placeholders for automatic tab completion. In the example, a placeholder called "player" is used to list online players for tab completion. This allows dynamic completion of commands where a specific placeholder (like player names) is needed.

  • name: The name of the placeholder (e.g., "player").
  • permission: An optional permission required to use this placeholder (if any). If no permission is set, it’s accessible by all users.

The @Placeholder annotation can be used to make tab completions more flexible and customizable, particularly for commands that involve specific arguments such as player names, item types, or locations.