description | icon |
---|---|
In this page, we will make our greet more customizable and robust. |
handshake |
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.
This can be easily set using the @Description
annotation
{% tabs %} {% tab title="Java" %}
public class GreetCommands {
@Command("greet")
+ @Description("Greets the specified player")
public void greet(BukkitCommandActor actor, Player target) {
target.sendMessage("Welcome, " + target.getName() + "!");
}
}
{% endtab %}
{% tab title="Kotlin" %}
class GreetCommands {
@Command("greet")
+ @Description("Greets the specified player")
fun greet(actor: BukkitCommandActor, target: Player) {
target.sendMessage("Welcome, " + target.name + "!")
}
}
{% endtab %} {% endtabs %}
That's it! We can see the description appear in /help:
The command now has a description
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.
{% tabs %} {% tab title="Java" %}
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() + "!");
}
}
{% endtab %}
{% tab title="Kotlin" %}
class GreetCommands {
@Command("greet")
@Description("Greets the specified player")
+ @CommandPermission("test.plugin.greet")
fun greet(actor: BukkitCommandActor, target: Player) {
target.sendMessage("Welcome, ${target.name}!")
}
}
{% endtab %} {% endtabs %}
That's it! Now, only people with the test.plugin.greet
permission can use the command.
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 Player
s 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.
{% tabs %} {% tab title="Java" %}
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() + "!");
}
}
{% endtab %}
{% tab title="Kotlin" %}
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 + "!")
}
}
{% endtab %} {% endtabs %}
🎉 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
@Optional
annotation: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: