banner



How To Not Register Commands In Plugin.yml Spigot

This is role 3 of my Minecraft plugin series. In this post I will show you how to handle player commands with issue listeners.

Creating the Command Listener course

The name of this class is not very important. Though I would name it something specific to what information technology does. The name of my form volition be FlyCommand. Information technology is besides best practice to add a Listener to the end of your class name, so that you tin can identify which classes are listeners. And then my class proper noun volition exist: FlyCommandListener.class.

If you forgot how to make a class, it is very simple. Correct click on the name of your package (under src) and select New -> Java Grade.

Now we 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.control.CommandExecutor;
import org.bukkit.command.CommandSender;

And so implement the CommandExecutor class to your class and so that your course becomes:

          public form FlyCommandListener implements CommandExecutor{        

Handling the command

By now you might of received an error from your IDE telling you must implement the method onCommand. That is fine, because that is exactly what nosotros will do at present.

Create a public boolean labeled onCommand. Structure it like this:

          public boolean onCommand(CommandSender sender, Command command, String firstArg, String[] args){

}

I will explain all of the methods in the role:

  • CommandSender sender is the player who executed the control. Nosotros tin bandage this to a player type later on on.
  • Control command is the actual control that is being sent (east.g the wing in /fly )
  • Cord firstArg is the first argument of our control (the player in /fly player )
  • String[] args are the rest of the arguments after /wing player in an array format.

Though the concluding 2 parts might not exist needed in your case, they are required for the CommandExecutor to piece of work.

At present that we have the sender, we can practise whatsoever we want to it.For now, I merely want to message them a statement. To practise that, I will add this line:

sender.sendMessage("How-do-you-do World!");

That will send the sender a bulletin "Hello Globe". To finish off the control, we have to render a value from our Boolean function. Just add together a render statement at the bottom:

return true;

Registering the command

You might notice how nosotros didn't actually mention /fly anywhere in our file. That'southward because we have to annals it in our main class. Become to your main course, and in the onEnable function, register the command past calculation this line:

          this.getCommand("wing").setExecutor(new FlyCommandListener());        

Replace "wing" with the command you desire to listen for, and supervene upon FlyCommandListener() with the name of your command listener class.

There is besides one more than step to adding your command. We accept to add your command to the plugin.yml file. Open the file, and add this:

          commands:
fly:
description: Allows you to wing
usage: /fly

This tells our plugin that we have a command, and the plugin and so assumes we register it ourselves.

Testing the command

Get alee and build your plugin to a .jar file similar before. Start up your server with the plugin. Since we added the command to our plugin.yml file, we tin do something pretty neat. We can type: /help FlyPlugin . This will output all of the commands your plugin has:

          [13:l:37 INFO]: --------- Assistance: FlyPlugin -----------------------
[13:l:37 INFO]: Below is a listing of all FlyPlugin commands:
[thirteen:50:37 INFO]: /fly: Allows you to fly

Now, if I run the /fly command, it will bear witness me the text I specified:

Since the sender type accepts the .sendCommand function, we can ship the message to anything that runs our command (both console and player).

That's all for this part three of the Minecraft plugin making series. I promise yous learned something new! If you lot need whatsoever help, leave a comment below and I can help you out.

How To Not Register Commands In Plugin.yml Spigot,

Source: https://ethr.me/spigot-bukkit-plugin-programming-commands-6fd8bdd5220f

Posted by: griggssatesing.blogspot.com

0 Response to "How To Not Register Commands In Plugin.yml Spigot"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel