Skip to content

ZenCommand

youyihj edited this page Nov 12, 2020 · 7 revisions

ZenCommand

ZenUtils allows you to add command via ZenScript.

Importing the package

It might be required to import the class to avoid errors.

import mods.zenutils.command.ZenCommand;

Create the ZenCommand

Before you can add command, you need to create a ZenCommand instance, and set some properities of the command you want to add.

// Return a new ZenCommand instance
ZenCommand.create(String name);

ZenGetters

You can use name ZenGetter to get the name of ZenCommand.

Necessary works

First, you should set getCommandUsage ZenProperity, of which type is IGetCommandUsage, to define the usage information. The return string should be unlocalized name, not literal message!

Second, you should set execute ZenProperity, of which type is ICommandExecute, to define how the command executes.

Optionally, you can set requiredPermissionLevel ZenProperity, of which type is int, to set the permission level to execute the command. The default value is 4.

Optionally, you can set tabCompletionGetters ZenProperity, of which type is IGetTabCompletion array, to define the command's tab completion.

Last and the most important, call register method to register the command in the game, otherwise nothing will happen!

You can use CommandUtils in you command writing.

Example

import mods.zenutils.command.ZenCommand;
import mods.zenutils.command.CommandUtils;
import mods.zenutils.command.IGetTabCompletion;

val statusMessage as ZenCommand = ZenCommand.create("statusmessage");
statusMessage.getCommandUsage = function(sender) {
    return "commands.statusmessage.usage"; // return localization key
};
statusMessage.requiredPermissionLevel = 0; // require no permission, everyone can execute the command.
statusMessage.tabCompletionGetters = [IGetTabCompletion.player()];
statusMessage.execute = function(command, server, sender, args) {
    // the length of command arguments is 1 (for example, /statusmessage hello)
    // Send a status message to player that executes the message
    if (args.length == 1) {
        CommandUtils.getCommandSenderAsPlayer(sender).sendStatusMessage(args[0], true);
    // the length of command arguments is 2 (for example, /statusmessage Notch hello)
    // Send a status message to specific player
    } else if (args.length == 2) {
        CommandUtils.getPlayer(server, sender, args[0]).sendStatusMessage(args[1], true);
    // the length of command arguments is wrong
    // notify command sender wrong using and the proper usage.
    } else {
        CommandUtils.notifyWrongUsage(command, sender);
    }
};
statusMessage.register();
Clone this wiki locally