🛖
Fox Hut
  • Introduction
    • Setting up
    • CommandActor, @Command and @Subcommand
    • Creating your first command
    • Improving our greet command
  • Platforms
    • Bukkit / Spigot / Paper
    • BungeeCord
    • Velocity
    • Sponge
    • Fabric
    • Brigadier
    • Minestom
    • JDA
    • Command line
  • How-to
    • Creating variants of /teleport
    • Custom parameter types
    • Suggestions and auto-completion
    • Context parameters
    • Command permissions
    • Parameter validators
    • Command conditions
    • Response handlers
    • Cooldowns
    • Help commands
    • Annotation replacers
    • Orphan command
    • Exception handling
    • Hooks
    • Dependency injection
    • Visitors
    • Customizing the dispatcher and failure behavior
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Introduction

Improving our greet command

In this page, we will make our greet more customizable and robust.

PreviousCreating your first commandNextBukkit / Spigot / Paper

Last updated 9 months ago

Was this helpful?

Right now, our /greet command does not do much. Also, it has the following problems:

  1. The command has no description that appears in /help

  2. The command is executable by everyone

  3. 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 + "!")
    }
}

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:

Good news, Kotlin users!

🎉
The command now has a description