Solved Accessing "plugin" in another class

Discussion in 'Plugin Development' started by Pink__Slime, May 16, 2013.

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

    Pink__Slime

    This is the ONE thing that I am yet to know that I really do NEED to know...

    So I have my main class where I don't have to use plugin. at the start of everything. But I have schedulers in other classes that need to use plugin and I can't find the right way of getting it.

    I use
    Code:
    static private Main plugin;
    in my methods class but for a listener I don't know what to use.

    I have tried
    Code:java
    1. private final Main plugin;
    2.  
    3. public MenuListener(Main instance){
    4. plugin = instance;
    5. }

    and that doesn't work either.
    So can someone tell me:
    1. Do you need to use a different method of getting plugin for different uses?
    2. How do you get it for just a listener?
    3. Is the first code block I used the right way if I'm just using it in Methods?


    The only way I really know how to counter this is by putting everything in my Main class which I really don't want to do since my Main class is already 1685 lines.
     
  2. Offline

    thecrystalflame

    Here is just something I whipped up nice and quick for you to link the 2 classes.
    in the Main class:

    Code:JAVA
    1.  
    2. private MenuListener ml = new MenuListener(this);
    3.  
    4. public MenuListener getML() {
    5. return ml;
    6. }
    7.  


    in MenuListener class:
    Code:JAVA
    1.  
    2. private Main plugin;
    3.  
    4. public MenuListener (Main plugin) {
    5. this.plugin = plugin;
    6. }
    7.  


    the getML function will also allow you to call MenuListener from another class also linked to main via plugin.getML.method();

    hope this helps
     
    Pink__Slime likes this.
  3. Offline

    Tzeentchful

    Pink__Slime
    I prefer passing the main class in the constructor.
    Here is an example.
    Code:java
    1. public class Listener implements Listener {
    2. private Main plugin;
    3. public Listener(Main plugin) {
    4. this.plugin = plugin;
    5. }
    6. }


    and in your main class.
    Code:java
    1. ...
    2. Listener listener = new Listener(this);
    3. ...
     
    Pink__Slime likes this.
  4. Offline

    Pink__Slime

    thecrystalflame
    Using that I don't understand how to call MenuListener in another class.
    My four classes are: Main, MenuListener, BM_Handler, Methods.
    I need to be able to call MenuListener in BM_Handler and BM_Handler in Menu_Listener.
     
  5. Offline

    Me4502

    Or you could use in Main class.

    Code:
    private static Main instance;
     
    public void onEnable() {
      instance = this;
    }
     
    public static Main inst() {
      return instance;
    }
    And in the listener when you need the instance,

    Code:
    Main.inst()
     
  6. Offline

    thecrystalflame

    Pink__Slime
    For you to do this
    in BM_Handler you want:
    Code:JAVA
    1.  
    2. private Main plugin;
    3.  
    4. public BM_Handler(Main plugin) {
    5. this.plugin = plugin;
    6. }
    7.  


    and in your main you want:
    Code:JAVA
    1.  
    2. BM_Handler bmh = new BM_Handler(this);
    3.  
    4. public BM_Handler getBMH() {
    5. return bmh;
    6. }
    7.  


    from there you will be able to call your MenuListener function from BM_Handler by using
    Code:JAVA
    1.  
    2. plugin.getML()
    3.  


    and call BM_Handler functions from MenuListener via
    Code:JAVA
    1.  
    2. plugin.getBMH()
    3.  
     
  7. Offline

    desht

    Me4502 If you're doing it that way, you should really also be sure to do instance = null; in your onDisable().
     
  8. Offline

    Me4502


    Yes, especially for if the user does /reload.
     
  9. Offline

    Pink__Slime

    desht Me4502
    I'm currently using the method to get plugin posted in the second comment:
    Code:java
    1. private static Main plugin;
    2.  
    3. public Methods (Main plugin) {
    4. this.plugin = plugin;
    5. }


    But I need to use it in a static sense and that doesn't let me. I get these errors:

    Main.java:103 is just Methods.createSelectMenu()
    I know it's a problem with the plugin because createMenu() doesn't spark any errors and that is accessed in the exact same way.
     
  10. Offline

    Me4502

    Can you please pastebin that section of code?
     
  11. Offline

    Pink__Slime

  12. Offline

    Me4502


    Can you please tell me which line is line 116?
     
  13. Offline

    Pink__Slime

    Oh sorry, it's
    Code:
    turtleLore.add(ChatColor.GREEN + "" + ChatColor.BOLD + "$" + plugin.getConfig().getString("turtle"));
    It's in createSelectMenu(), all of those blocks there were going to retrieve a string from the config.
     
  14. Offline

    Me4502


    How do you register the event listener?
     
  15. Offline

    Pink__Slime

    Me4502
    There are no events in the class. Just methods.
     
  16. Offline

    Me4502

    Err, how are you creating the methods class then? What is its constructor, and how is it called?
     
  17. Offline

    Pink__Slime

    When using the methods, I'll use Methods.method(); (as an example). It works fine, just stopped working with the use of plugin.
     
  18. Offline

    Me4502


    Err... How are you setting plugin?

    Maybe try and use the method of accessing plugin I stated above in my post.
     
  19. Offline

    Pink__Slime

    Me4502
    Main: http://pastebin.com/vAWNAmjb
    Methods: http://pastebin.com/utpFGsDQ
    This is all you should need from them.

    Basically, I changed a few things and createMenu() completes itself. But createSelectMenu() doesn't.

    I get an error saying NPE at Main.onEnable(Main.java:102), line 102 is Methods.meth().createSelectMenu();

    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  20. Offline

    kreashenz

    Pink__Slime I don't see any problem. To do plugin.getConfig() or something, I will use
    Code:java
    1. private <MainClass> plugin;
    2. public <Class>(<MainClass> plugin){
    3. this.plugin = plugin;
    4. }
     
  21. Offline

    Pink__Slime

    kreashenz
    I figured it out. I was cleaning my plugin out while waiting for a reply but when I finished I decided to try one last thing and that worked. I think I just completely blanked because it was mentioned here as well...
    Thanks everyone for the help!
     
  22. Offline

    microgeek

    Just make a wrapper class with a static instance?
     
Thread Status:
Not open for further replies.

Share This Page