Commands

Commands allow you to execute specific actions in response to user input. Here's an example of how to create them step by step:

function build(build) {
    build.registerCommand(CommandInfo.builder()
        .name("command")
        .aliases(["c", "cmd"])
        .description("Simple example command in MCJS")
        .permission("commands.command")
        .target(CommandTarget.ALL)
        .async(false)
        .build(), function (context) {
            API.message(context.getSender(), "&aHello World!");
            return true;
        });
}

Explanation of the code:

Registering and Creating a Command:

build.registerCommand(CommandInfo.builder()...

You use the registerCommand method to register a new command. CommandInfo.builder() creates a command information object.

Command Configuration:

.name("command")
.aliases(["c", "cmd"])
.description("Simple example command in MCJS")
.permission("commands.command")
.target(CommandTarget.ALL)
.async(false)
  • .name("command"): Sets the command name as "command".

  • .aliases(["c", "cmd"]): Assigns aliases to the command, in this case, "c" and "cmd".

  • .description("Simple example command in MCJS"): Provides a description for the command.

  • .permission("commands.command"): Defines the permission required to execute the command.

  • .target(CommandTarget.ALL): Specifies the command target (ALL, PLAYER, or CONSOLE).

  • .async(false): Indicates whether the command runs asynchronously or not. In this case, it is set to false, meaning it is synchronous.

Command Function:

function (context) {
    API.message(context.getSender(), "&aHello World!");
    return true;
}

This is the function that will be executed when the command is invoked. In this example, it uses the API.message function to send a message to the command sender with the content "Hello World!". The function returns true to indicate that the command was executed successfully.

Last updated