Want a better Minecraft server? Read about SpigotMC here!
Separate names with a comma.
Discussion in 'Spigot Plugin Development' started by TesPlayz, Jul 6, 2019.
I can't call my config from any class other than the main class can someone help?
What's your current code?
The method getConfig() is a method from JavaPlugin class, so you should use a instance of your Main class to call the method
Using Singleton or Dependency Injector
I don't understand what you mean could you explain more?
Yes, learn basic java before diving to coding on bukkit
The method getConfig() only exists in JavaPlugin, because your main class extends JavaPlugin you can use it with that class as well. You need to get your main class somehow and then call getConfig() from that.
you'd need to use
getConfig() is not static, you would need an instance of your main class instead.
I'm assuming Main in your code is not a variable because it doesn't start with lower case.
I wish it's a joke xD
As @MrMaurice211 said, learn java and OOP before creating plugins.
Main as a class
It is a joke
Then it's wrong, as i said:
Nothing in your post hinted at a joke.
If it wasn't I would've explained and made it actual code
But then what was the point of the post?
This thread has turned to cancer. @TesPlayz:
Singleton injection is when you create a static field which embodies the instance of your main plugin so that you could call your main file, lets call Main, and then return the instance of Main by doing Main.instance, or even use a getter like Main.getInstance()
This is my main plugin that I work on. Check out lines 33 where I initialize an object type the same as my class name and call it Instance, 53 where I set the instance field in the onEnable method to "this" which represents the current instance, and then line 47 where I create a getInstance method.
From there I then on other files (line 83) create another method which returns RedstoneProximitySensor.getInstance(); and I am able to then grab the instance of my main plugin class. From there I could do RedstoneProximitySensor.getInstance().getConfig()
Dependency Injection is when you feed the plugins instance into the classes constructor. For example,
On line 30 you see I create a new instance of CommandTrade and inside the parameters I put in (this). That is the instance of my JavaPlugin class.
If you go to line 39 you see that in the constructor of CommandTrade I set the instance of the plugin to a new field for that class called "plugin", so that I can use that field whenever I want inside of CommandTrade.