Skip to content
This repository has been archived by the owner on Sep 1, 2019. It is now read-only.

commands

Kripth edited this page May 19, 2017 · 1 revision

Commands can be used by players to execute particular tasks.

Commands can be registered globally, to the server, only in a world or just to a player.

Command's structure

A command is a delegate called when a CommandSender executes a command. The first argument of the function is always a CommandSender or another class/interface that extends it. The other arguments can be of one of the following types:

  • target

    Target, Entity[], Player[] or Player. Targets are selected using a target selector or a player's name.

  • position

    Position.

  • bool

    A boolean value, either true or false.

  • int

    long, ulong, int, uint, short, ushort, byte or ubyte.

    A integer number.

  • float

    double or float.

    A floating point number.

  • string

  • enum

    The value of an enum. If the enum is not snake case, which Minecraft uses, it can be converted to it, maintaing names and values, using the SnakeCaseEnum template:

     enum Test { porkchop = 0, cookedPorkchop = 1 }
     assert(SnakeCaseEnum!Test.cooked_porkchop == Test.cookedPorkchop);

Command Sender

The interface CommandSender is impemented by classes that can send commands, which are Player, that also implements WorldCommandSender, and Server. The interface provides some methods that can be used to get the sender's informations (position to get the position) and send messages (the method sendMessage).

The @command attribute

To register a command globally or in a world the @command attribute can be used in the plugin's main class or in the world's class:

@command("broadcast") broadcast(CommandSender sender, string message) {
   server.broadcast(message);
}

The command name, which is the only argument of the @command attribute, should be a lowercase string without spaces.

Aliases

A command can have aliases that can be used to call it easily when its name is too long:

@command("verylongcommand") @aliases("vlc", "verylc") vlc(CommandSender sender, string message) {}

Default arguments

@command("default") def(CommandSender sender, long required, long optional=1) {}

Overloads

The same command can have different overloads taking different arguments:

@command("overload") overload0(CommandSender sender) {}

@command("overload") overload1(CommandSender sender, string message) {}

Overloads with the same arguments are fine when the command sender can never be the same:

@command("overload") overload0(WorldCommandSender sender) {
	log("overload called by worldcommandsender");
}

@command("overload") overload1(Server sender) {
	log("overload called by server");
}
Clone this wiki locally