Using Multiple Classes In Your Plugins
There's been a copious amount of threads in the Spigot Plugin Development category where people have requested help with coding their plugins, and because it's all in one class, we get spammed with unnecessary and irrelevant code. This can become pretty annoying, especially when you're actually trying to help. Today, I'm going to show you how to use multiple classes within your plugins; when it comes to Commands and Events (Listeners).
Using multiple classes for commands:
- Make sure your class extends the JavaPlugin super class.
- For this to work, you need to create the onEnable method.
- The getCommand() method is one within the JavaPlugin super class, and because your class extends it, you have access to the method. You can either do this.getCommand() or just getCommand().
- Inside of the parameters of the getCommand method, you'll put your command without the slash, and without any arguments (Example: getCommand("heal")).
- You've now gotten the command, and you now have to create a new instance of the class containing your command with the setExecutor method (Example: .setExecutor(new Class())).
- If your class is in another package, you will have to import it to get rid of the error
- For this to work, you'll have to make sure your class implements the CommandExecutor interface.
- The moment you import CommandExecutor, you'll see an error under the name of your class.
- To fix this, you'd want to hover over it, and add the unimplemented methods, which would be the onCommand method. You can also just write it out.
- The method is now exactly the same as how it would be in the main class.
- Don't forget to register them in the plugin.yml file.