Config

Discussion in 'Spigot Plugin Development' started by TesPlayz, Jul 6, 2019.

  1. I can't call my config from any class other than the main class can someone help?
     
  2. What's your current code?
     
  3. 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
     
  4. @MrMaurice211
    I don't understand what you mean could you explain more?
     
  5. Yes, learn basic java before diving to coding on bukkit
     
    • Agree Agree x 1
  6. 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.
     
  7. you'd need to use
    Code (Text):
    Main.getConfig()
     
    • Funny Funny x 2
    • Optimistic Optimistic x 1
  8. 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.
     
  9. I wish it's a joke xD

    As @MrMaurice211 said, learn java and OOP before creating plugins.
     
  10. Main as a class
     
  11. It is a joke
     
    • Informative Informative x 1
  12. Then it's wrong, as i said:
    Nothing in your post hinted at a joke.
     
  13. If it wasn't I would've explained and made it actual code
     
  14. But then what was the point of the post?
     
  15. 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()

    https://github.com/Ugleh/RedstonePr...eproximitysensor/RedstoneProximitySensor.java

    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.


    https://github.com/Ugleh/RedstonePr...proximitysensor/listeners/SensorListener.java
    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,
    https://github.com/Ugleh/TradeMe/blob/master/src/com/ugleh/trademe/TradeMe.java
    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.
    https://github.com/Ugleh/TradeMe/blob/master/src/com/ugleh/trademe/commands/CommandTrade.java
    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.
     
    • Like Like x 1