Config & @EventHandler Help

Discussion in 'Spigot Plugin Development' started by ItsWill, Jun 13, 2015.

  1. The player listener is in a different Package called Listener
    The error message is
    The constructor PlayerListener(main) is undefined
     
  2. I hate to be that guy, but you honestly should learn at least basic java and object programming before you do something like this.

    Just saying.
     
    • Friendly Friendly x 1
  3. Haha, I'm learning. I know I should get a little bit more knowledgeable with Java. Thanks Though, however I do have many Java books that I'm 'reading'.
     
  4. Create a constructor that accepts an instance of Main or Plugin.
    Also, Effective Java by Joshua Bloch is a fantastic book to read once you understand a decent amount of Java. There are some PDFs floating around on a certain .edu website if you don't want to buy the book, though I highly recommend you do.
     
  5. Thanks! Could you just like spoonfed me this time? Like surely it's not the hard, or is it? If you could it would mean a lot, it's dependant on my plugin.
     
  6. Code (Text):
    private final Main main;
    public PlayerListener(Main main) {
        this.main = main;
        // do whatever else needs to be done
    }
    The rest is on you :p
     
  7. Thanks I'll try it :D
     
  8. Wait up, I know how to do that. But how would I register the events?
    Code (Text):
    pm.registerEvents(new PlayerListener, (this));
    Tried this, but it doesn't work.
     
  9. I won't tell you the correct answer but I'll tell you why it's not working; you're not actually passing an instance of Main (or whatever this is) to the constructor of PlayerListener.
     
  10. Lol Hi?
     
  11. Code (Text):
    pm.registerEvents(new PlayerListener(this), this);
    So now for an explanation of why this works the way it does and what you did wrong
    If you check the PluginManager documentation, it takes two arguments, an instance of the class that's handling the events, and the plugin to register it as.

    Since your constructor for PlayerListener takes an instance of your plugin's main, you need to pass one to it, so new PlayerListener(this) is your handler, and the second argument, 'this' is your main plugin classes local instance
     
    • Like Like x 1
    • Agree Agree x 1
  12. Traitor ಠ_ಠ
    So many people looking to be spoonfed nowadays.
    Have a like for referencing the documentation though.