OpenJS-docs
Download plugin
  • Overview
    • Welcome
  • Getting Started
    • Installing the plugin
    • Configuration file
    • Commands
    • Creating scripts
  • OpenJS Components
    • Feature Flags
    • Managing scripts within scripts
    • Sharing variables between scripts
    • Custom Commands
    • Using PlaceHolderApi
    • Listening and cancelling events
    • Custom events
    • Loading and saving data
    • Logging in console
    • Scheduling
    • Java imports
  • Example Scripts
    • spawn hit stand command
    • Knockbackstick command
    • No hit-cooldown script
    • NoBlockPlacing script
    • Saving Player data script
    • running code asynchronously
    • WhileTrue do script
Powered by GitBook
On this page
  1. OpenJS Components

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

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

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

PreviousSharing variables between scriptsNextUsing PlaceHolderApi

Last updated 17 hours ago

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

LuckPerms