Use the same same instance

Discussion in 'Plugin Development' started by edocsyl, May 8, 2012.

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

    edocsyl

    Hi

    I wanna execute some sql query when a player joins and quits.
    Now is my problem i can't use the instance who i made in the Main class.

    Code:
    public class CraftingTerritory extends JavaPlugin {
    Logger log;
    private GSMCommandExecutor mycExecutor;
    public String logPrefix = "[CraftingTerritory] ";
    public GSMMysql gsmMysql;
    ...
    @Override
    public void onEnable() {
    ...
    /*Listener*/
    getServer().getPluginManager().registerEvents(new GSMListener(), this);
     
    .....
    /*MySql*/
    gsmMysql = new GSMMysql("localhost", "3306", "root", "", "gsm");
    }
    }
    
    Now i wanna use the gsmMysql for my listener.
    Code:
    public class GSMListener implements Listener {
    private CraftingTerritory plugin;
    private funcs fu = new funcs();
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerJoin(PlayerJoinEvent e){
    Player p = e.getPlayer();
     
    if(plugin.gsmMysql.playerExists(p)){
    plugin.gsmMysql.exStatemen("UPDATE spieler SET lastlogin_mc = '" + fu.getTime() + "' WHERE mcname = '" + p.getName() + "'");
    }
     
        }
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerQuit(PlayerQuitEvent e){
    Player p = e.getPlayer();
        }
    }
    
     
  2. Offline

    Double0negative

    Either make the methods static or send a instance of the main class to the subclass in the constructor
     
  3. Offline

    edocsyl

    You mean this so?
    Code:
    private static CraftingTerritory plugin;
    
     
  4. Offline

    ZachBora

    When you create GSMListener, are you assigning the plugin variable?
     
  5. Offline

    edocsyl

  6. Offline

    ZachBora

    add this to the listener
    Code:
    public GSMListener(CraftingTerritory plugin) {this.plugin = plugin;}
    Change this in CraftingTerritory :
    Code:
    getServer().getPluginManager().registerEvents(new GSMListener(this), this);
    This is what it looks like in my listener
    Code:
    public class CloneBlockListener implements Listener {
     
        private CloneMe plugin;
     
        public CloneBlockListener(CloneMe plugin) {this.plugin = plugin;}
     
  7. Offline

    edocsyl

    Thx alot. This i alrdy tryd but i had:
    private CraftingTerritory plugin;

    i miss the static.

    Thx. :)
     
Thread Status:
Not open for further replies.

Share This Page