Custom Commands

Creating Custom Commands

Allows you to register and handle custom commands in scripts


Method

addCommand(commandName: String, commandHandler: Object, permission?: String)

Parameters:

  • commandName (String) The name of the command (without /)

  • commandHandler (Object) Contains methods like onCommand and optionally onTabComplete

  • permission (optional) (String) The permission string required to execute or tab-complete this command. If there is no permission given, the command is available to all players (and ops by default)


commandHandler Methods

  • onCommand(sender, args) (required) Called when the command is executed.

    • sender: The player or console who executed the command.

    • args: A Java array of arguments.

  • onTabComplete(sender, args) (optional) Called to provide tab-completion suggestions. Must return a Java List.


Example Without Permissions
addCommand("hello", {
    onCommand: function(sender, args) {
        args = toArray(args); // Convert Java array to JS array
        sender.sendMessage("Hello, " + sender.getName() + "!");
    },
    onTabComplete: function(sender, args) {
        args = toArray(args);
        if (args.length === 1) {
            return toJavaList(["world", "everyone"]);
        }
        return toJavaList([]);
    }
});

Example With Permissions
addCommand("adminhello", {
    onCommand: function(sender, args) {
        sender.sendMessage("Hello from the admin command!");
    },
    onTabComplete: function(sender, args) {
        return toJavaList(["secret", "adminzone"]);
    }
}, "openjs.command.adminhello"); // <-- Optional permission

Players without openjs.command.adminhello will not see the command in suggestions or tab-complete. Ops do have all permission by default, unless restricted by permission plugins.


Notes

  • You can manage permissions via /lp editor in LuckPerms or other permission systems

  • Permissions must be valid strings (e.g., pluginname.command.commandname)

  • If onTabComplete is missing, tab completion is disabled for this command

Last updated