-
Notifications
You must be signed in to change notification settings - Fork 4
commands
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.
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[]
orPlayer
. Targets are selected using a target selector or a player's name. -
position
Position
. -
bool
A boolean value, either
true
orfalse
. -
int
long
,ulong
,int
,uint
,short
,ushort
,byte
orubyte
.A integer number.
-
float
double
orfloat
.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 theSnakeCaseEnum
template:enum Test { porkchop = 0, cookedPorkchop = 1 } assert(SnakeCaseEnum!Test.cooked_porkchop == Test.cookedPorkchop);
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
).
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.
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) {}
@command("default") def(CommandSender sender, long required, long optional=1) {}
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");
}