How To Register A Command Bukkit
Spigot/Bukkit plugin programming: Commands
This is part 3 of my Minecraft plugin serial. In this post I volition show you how to handle player commands with issue listeners.
Creating the Command Listener class
The name of this course is not very important. Though I would name information technology something specific to what it does. The name of my class will be FlyCommand. It is also best practice to add together a Listener to the cease of your class name, then that y'all can identify which classes are listeners. So my grade name will be: FlyCommandListener.grade.
If you forgot how to brand a class, it is very uncomplicated. Right click on the name of your package (under src) and select New -> Coffee Form.
Now nosotros have to import everything the class needs. Which is the event (CommandExecutor), and the types (CommandSender and Command):
import org.bukkit.control.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; Then implement the CommandExecutor form to your class so that your class becomes:
public course FlyCommandListener implements CommandExecutor{ Handling the command
By now you might of received an error from your IDE telling y'all must implement the method onCommand. That is fine, considering that is exactly what we will practice now.
Create a public boolean labeled onCommand. Structure it like this:
public boolean onCommand(CommandSender sender, Control command, String firstArg, String[] args){ }
I volition explain all of the methods in the function:
-
CommandSender senderis the player who executed the command. We can cast this to a thespian type later on. -
Command commandis the actual command that is being sent (east.thou theflyin/fly) -
String firstArgis the first argument of our command (theplayerin/fly actor) -
String[] argsare the remainder of the arguments later/fly thespianin an array format.
Though the final 2 parts might not exist needed in your instance, they are required for the CommandExecutor to work.
At present that we take the sender, we can practise whatsoever nosotros desire to it.For now, I but want to bulletin them a argument. To do that, I will add this line:
sender.sendMessage("How-do-you-do Earth!");
That will send the sender a message "Hello World". To end off the command, nosotros have to return a value from our Boolean office. Merely add a render argument at the bottom:
render true;
Registering the command
You might notice how nosotros didn't actually mention /fly anywhere in our file. That'due south considering we have to register information technology in our main class. Get to your main class, and in the onEnable function, register the control by adding this line:
this.getCommand("fly").setExecutor(new FlyCommandListener()); Replace "wing" with the command you want to listen for, and replace FlyCommandListener() with the name of your command listener grade.
There is also ane more than step to calculation your command. We have to add your command to the plugin.yml file. Open up the file, and add this:
commands:
wing:
description: Allows you to fly
usage: /fly This tells our plugin that we have a command, and the plugin and then assumes nosotros register it ourselves.
Testing the control
Go ahead and build your plugin to a .jar file like before. Kickoff up your server with the plugin. Since we added the command to our plugin.yml file, nosotros can do something pretty keen. Nosotros can type: /help FlyPlugin . This volition output all of the commands your plugin has:
[thirteen:50:37 INFO]: --------- Help: FlyPlugin -----------------------
[13:50:37 INFO]: Below is a listing of all FlyPlugin commands:
[13:l:37 INFO]: /fly: Allows you to wing Now, if I run the /fly command, it will show me the text I specified:
Since the sender blazon accepts the .sendCommand function, we can send the bulletin to anything that runs our command (both console and thespian).
That'south all for this part 3 of the Minecraft plugin making series. I hope y'all learned something new! If you need any help, leave a comment below and I can help you out.
How To Register A Command Bukkit,
Source: https://ethr.me/spigot-bukkit-plugin-programming-commands-6fd8bdd5220f
Posted by: stewartproughat.blogspot.com

0 Response to "How To Register A Command Bukkit"
Post a Comment