-
-
Notifications
You must be signed in to change notification settings - Fork 48
Creating a hello command
In the past tutorials, we set up Gradle and created a plugin class.
To make sure everything works, we will create a simple /hello
command.
For that, we have to create a BukkitCommandHandler
that handle everything command-related.
It's highly recommended to construct it inside onEnable
, and not earlier.
@Override
public void onEnable() {
+ BukkitCommandHandler handler = BukkitCommandHandler.create(this);
}
Now, let's create our command. It will be extremely simple: Simply reply Why, hello there!
when executed.
@Command("hello")
public void sayHello(BukkitCommandActor actor){
actor.reply("Why, hello there!");
}
Finally, we have to register the command in our BukkitCommandHandler. Go back to onEnable(), and add the following:
@Override
public void onEnable() {
BukkitCommandHandler handler = BukkitCommandHandler.create(this);
+ handler.register(this);
}
We're done! Now, let's build the plugin. Since we are using ShadowJar, our build command will be gradlew shadowJar
.
gradlew shadowJar
If you're on Linux or Mac, run the following:
./gradlew shadowJar
Our JARs will be available in build/libs
Place the JAR in your plugins
folder, run the server, and run /hello
:
Congratulations! You have created your first command.
Let's break down what we have done in this tutorial:
- We created a
BukkitCommandHandler
and initialized it in ouronEnable
. Most of our registration will be done using the BukkitCommandHandler. - We have to register our newly created command, by passing an instance of the class that contains it. Because we
created it in the same class as our plugin, we passed
this
.- If we created another command method inside our
SunBans
class, we won't have to register it again, as everything inside the class is going to be registered automatically.
- If we created another command method inside our
- All commands are created using annotations. As you may have noticed, we created a method annotated with
@Command
, with our command name. - Our command takes a
BukkitCommandActor
, which is a wrapper class for a CommandSender.- You can also use Bukkit's
CommandSender
, orPlayer
if you would like to restrict it to players only.
- You can also use Bukkit's
- You may have noticed that, unlike the standard Bukkit API, we do not have to register our command in the
plugin.yml
. Lamp does that automatically for us!
In the next tutorial, we will create a slightly more complicated command.
👋 If you're having trouble, need support, or just feel like chatting, feel free to hop by our Discord server!