Improving our greet command
In this page, we will make our greet more customizable and robust.
Right now, our /greet command does not do much. Also, it has the following problems:
The command has no description that appears in /help
The command is executable by everyone
The welcome message is not customized
Also, we would like to make the command work with no arguments. If a target was not specified, we want to default to the command sender.
Setting a description
This can be easily set using the @Description annotation
public class GreetCommands {
@Command("greet")
+ @Description("Greets the specified player")
public void greet(BukkitCommandActor actor, Player target) {
target.sendMessage("Welcome, " + target.getName() + "!");
}
}class GreetCommands {
@Command("greet")
+ @Description("Greets the specified player")
fun greet(actor: BukkitCommandActor, target: Player) {
target.sendMessage("Welcome, " + target.name + "!")
}
}That's it! We can see the description appear in /help:

Setting a permission
Setting a permission is equivalently simple. It can be set using the @CommandPermission annotation, which is functionally equal to Bukkit's Permission class.
It takes two parameters: value, which is the permission node, and defaultAccess, which defines who can access the command by default. This is set to PermissionDefault.OP, and can be changed explicitly.
public class GreetCommands {
@Command("greet")
@Description("Greets the specified player")
+ @CommandPermission("test.plugin.greet")
public void greet(BukkitCommandActor actor, Player target) {
target.sendMessage("Welcome, " + target.getName() + "!");
}
}class GreetCommands {
@Command("greet")
@Description("Greets the specified player")
+ @CommandPermission("test.plugin.greet")
fun greet(actor: BukkitCommandActor, target: Player) {
target.sendMessage("Welcome, ${target.name}!")
}
}That's it! Now, only people with the test.plugin.greet permission can use the command.
Setting default values
We would like to make our target argument optional. If it is not specified, it should default to the sender.
This can be done using the @Default annotation, which specifies a default value for the parameter.
Because this is very common in plugins (default Players to the sender), Lamp accepts special values for the Player type: me, @s and self. If these are supplied to a Player parameter, it will receive the sender as a value.
public class GreetCommands {
@Command("greet")
@Description("Greets the specified player")
@CommandPermission("test.plugin.greet")
public void greet(BukkitCommandActor actor, @Default("me") Player target) {
target.sendMessage("Welcome, " + target.getName() + "!");
}
}class GreetCommands {
@Command("greet")
@Description("Greets the specified player")
@CommandPermission("test.plugin.greet")
fun greet(actor: BukkitCommandActor, @Default("me") target: Player) {
target.sendMessage("Welcome, " + target.name + "!")
}
}🎉 Good news, Kotlin users!
Lamp supports Kotlin's default arguments. You can use Kotlin's natural syntax for specifying the default parameters. The only caveat is that you need to explicitly mark these parameters with the
@Optionalannotation:class GreetCommands { @Command("greet") @Description("Greets the specified player") @CommandPermission("test.plugin.greet") fun greet( actor: BukkitCommandActor, @Optional target: Player = actor.requirePlayer() ) { target.sendMessage("Welcome, " + target.name + "!") } }
Seeing it in action:

Last updated
Was this helpful?