Accessing main class from event listener

Discussion in 'Plugin Development' started by Cowboys1919, Jul 12, 2012.

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

    Cowboys1919

    Is the best was to do this to add, say in the constructer, a reference to your main class and have it set a variable in the listener class So you'd do
    Code:
    getServer().getPluginManager().registerEvents(new EventListener(this), this);
    With EventListener looking like


    Code:
    package com.cowboys.game
     
    import org.bukkit.event.Listener;
     
    public class EventListener implements Listener {
        Main main;
        EventListener(Main m)
        {
            main = m;
        }
    }
    
     
  2. Offline

    Darq

    Yeah that's about the best way you'd pass an instance of a class.

    What's wrong with it?
     
  3. Offline

    MrTwiggy

    I remember hearing people talk about using a Static getMain() type of function, but I couldn't figure out how to make it work.
     
  4. Offline

    EnvisionRed

    In my main class I just declare the plugin as static and equal to this. Then in other classes I declare plugin as equal to MainClassName.plugin.

    Main class:
    Code:
    public static yourpluginname plugin = this;
    Other class:
    Code:
    private yourpluginname plugin = MainClassName.plugin;
     
Thread Status:
Not open for further replies.

Share This Page