Skip to content

F | Creating Custom Commands

Sartaj Singh edited this page May 28, 2025 · 2 revisions

Creating Custom Commands (Step-by-Step)

Learn how to add player/server commands, subcommands, and tab completion.


Step 1: Create the Command Class

Recommended location: com.noctify.Custom.Commands

Example: GreetCommand

public class GreetCommand implements CommandExecutor, TabCompleter {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage("Only players can use this command.");
            return true;
        }
        if (args.length == 0) {
            player.sendMessage("Usage: /greet <name>");
            return true;
        }
        player.sendMessage("Hello, " + args[0] + "!");
        return true;
    }
    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
        if (args.length == 1) {
            return Bukkit.getOnlinePlayers().stream().map(Player::getName).toList();
        }
        return Collections.emptyList();
    }
}

Step 2: Register the Command

In CommandRegistry:

registerCommand(plugin, "greet", GreetCommand.class);

Step 3: Subcommands

Add subcommands as needed:

if (args[0].equalsIgnoreCase("help")) {
    player.sendMessage("/greet <name> - Greets a player.");
    return true;
}

Step 4: Permissions

Check permissions in your command:

if (!player.hasPermission("fxitems.greet")) {
    player.sendMessage(ChatColor.RED + "No permission.");
    return true;
}

Step 5: Testing

  1. Register and reload server.
  2. Use /greet <name> and test tab completion.

Full Checklist for Custom Commands

  • Command class with onCommand()
  • Optional: TabCompleter for suggestions
  • Register in CommandRegistry
  • (Optional) Permissions, subcommands

Next: Creating Custom Events & Behaviors

Clone this wiki locally