I do something a lot different than most people do. Say your class that extends JavaPlugin
is Main.java
, you would add the following code NOT inside of any methods:
public static Main plugin;
then add this in your onEnable()
:
plugin = this
So, your Main
class could look something like this.
public class Main extends JavaPlugin{
public static Main plugin; //create the variable
@Override
public void onEnable(){
plugin = this; //assign plugin to this class
//other onEnable things
}
@Override
public void onDisable(){
//onDisable things
plugin = null; //best to put this AFTER running any methods here
}
}
Then, in any class that you want to use the JavaPlugin
methods in, just use Main.plugin
So, if you wanted to get the config for your plugin in another class, you could do:
YamlConfiguration config = Main.plugin.getConfig();
Just make sure that you do this AFTER you assign plugin
to your Main file. Let's say Handler.setup()
uses YamlConfiguration config = Main.plugin.getConfig();
in it, and you want to run that method when your plugin is enabled. You could do this:
public void onEnable(){
plugin = this;
Handler.setup();
}
But NOT this:
public void onEnable(){
Handler.setup();
plugin = this;
}