To create commands, use thecommand.create method. There are 2 commonly used syntaxes for command.create, which is directly dependent on if you would like to pass any options to the command, such as a prefix.
Creating a command with no options: command.create(name, invokeFunction)
Creating a command with options: command.create(name, options, invokeFunction)
Valid options
The following are properties that can be declared in the options parameter.
{ prefix: string, // A prefix for the command. For example, `prefix: "cool"` on a command named test will register /test and /cool:test
description: string,// A description for the command, which will appear in the Bukkit /help command. usage: string,// A usage for the command, which will appear in the Bukkit /help command. permission: string | boolean, // If set to true, the command will require OP to execute. If set as a string, the command will require the given permission node to execute.
permissionMessage: string, // If declared, this is the message that will be sent to players that don't have permission to execute this command.
aliases: string[] // A set of aliases for this command. For example, a message command could have aliases ["msg", "w", "whisper"]
}
Examples
Let's start off by creating a command without any options, that just greets the command sender.
command.create("hello", (sender, args) => {sender.sendMessage("Hello there! Are you enjoying learning how to use Drupi?");});
Now, let's try making a command that's a little more useful, like a /fly command. We'll create a command here that requires OP to execute.
command.create("fly", { permission:true}, sender => { // Side note, if you don't intend on using the command arguments, you don't need to reference it in your command declaration.
constplayer=cast.asPlayer(sender);if(player.getAllowFlight()){// The player currently has fly enabled, so we'll disable it.player.setAllowFlight(false);// The color() function is required for using & color codes in text.player.sendMessage(color("&7Your flight has been &cdisabled&7.")); } else {// The player currently has fly disabled, so we'll enable it.player.setAllowFlight(true);// The color() function is required for using & color codes in text.player.sendMessage(color("&7Your flight has been &aenabled&7.")); }});
However, this code is prone to error. Specifically, if the console executes this command, cast.asPlayer(sender) will throw an error, because the server console cannot be turned into a player. So, let's fix that.
Different behavior for the console
The command variable provides 2 additional methods we can use to achieve this, command.isConsoleSender and command.isPlayerSender. I'll opt to show isConsoleSender because it's more simple for this example, however isPlayerSender can also be used.
command.create("fly", { permission:true}, sender => { // Side note, if you don't intend on using the command arguments, you don't need to reference it in your command declaration.
if(command.isConsoleSender(sender)){// Using a return statement here instead of an else block allows for the rest of the code to appear more clean,// as it's not inside another code block. This is called a Guard Clause.returnsender.sendMessage(color("&cThe console cannot fly. Sorry! Maybe some day...")); }constplayer=cast.asPlayer(sender);if(player.getAllowFlight()){// The player currently has fly enabled, so we'll disable it.player.setAllowFlight(false);// The color() function is required for using & color codes in text.player.sendMessage(color("&7Your flight has been &cdisabled&7.")); } else {// The player currently has fly disabled, so we'll enable it.player.setAllowFlight(true);// The color() function is required for using & color codes in text.player.sendMessage(color("&7Your flight has been &aenabled&7.")); }});
Now, this command is safe to use, without any errors being thrown if the console executes the command.