How to getConfig() in another class?

Discussion in 'Plugin Development' started by Haribo98, Sep 22, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Haribo98

    Hey there,
    I'm trying to make my plugin, but I need to getConfig in my ArrowListener class.

    I have looked around. But none work.
    I have attempted to do EIA.this.getConfig() but I get errors.

    How would I go about fetching the config from a different class?
     
  2. Give the arrow listener a constructor to give it the plugins instance while creating it - store it in a global variable then. Looks like this:
    Code:java
    1. public class ArrowListener
    2. {
    3. private final YourPlugin plugin; // The global variable.
    4.  
    5. public ArrowListener(YourPlugin plugin) // A method without a return type and the classes name is a constructor
    6. {
    7. this.plugin = plugin; //Save the given reference in our global variable.
    8. }
    9.  
    10. public void something()
    11. {
    12. plugin.getConfig()...
    13. }
    14. }

    Then, in your main class, you need to use that constructor, so change your
    new ArrowListener();
    to
    new ArrowListener(this);
     
    legostarwarszach likes this.
  3. Offline

    kroltan

    Or, create a static variable in your plugin class and set it to this on onEnable, then in your ArrowListener, use EIA.instance.getConfig();
     
  4. Offline

    Sushi

    This is what I do.
     
  5. kroltan Sushi don't forget to set all static variables to null in onDisable() or you will create memory leaks with /reload (that's why I avoid statics in bukkit whenever I can).
     
  6. Offline

    Sushi

    Using /reload is a pretty bad practice though.
     
  7. Offline

    kroltan

    oooh, I'll keep that in mind.
     
  8. Offline

    Haribo98

    Thanks guys :D
    I'm using V10lator's method. Works fine, so THANKS!
     
Thread Status:
Not open for further replies.

Share This Page