How to register events in parent class and current class?

Discussion in 'Plugin Development' started by TigerHix, Mar 21, 2014.

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

    TigerHix

    Strangely, even though I added Bukkit.getPluginManager().registerEvents(this, Plugin.INSTANCE) into both constructors in both classes (parent class and subclass), only events in subclass will be triggered. How can I solve this problem?
     
  2. Offline

    nlthijs48

    TigerHix Why do you use Plugin.INSTANCE? just use this or the variable containing the referenc to your main plugin class.

    TigerHix To clarify; the first param of registerEvents() is the class that has the listener function, the second is your main plugin class, so be sure to use it correctly.

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

    TigerHix

    nlthijs48 Well of course I know that.. Plugin.INSTANCE is just a reference to the main plugin class, no problem here. What I am actually asking is, how do I register events in parent class?

    It's like this:

    public class Item implements Listener {

    public Item() {
    Bukkit.getPluginManager().registerEvents(this, Plugin.INSTANCE);
    }

    // Events here with @EventHandler

    }

    If I create a new instance of Item, the events in Item class will be successfully registered and get fired later.

    But if I extend this class, like:

    public class SpecialItem implements Listener extends Item {

    public SpecialItem() {
    super();
    Bukkit.getPluginManager().registerEvents(this, Plugin.INSTANCE);
    }

    // Events here with @EventHandler

    }

    Then events in Item class will not be registered, but only events in SpecialItem class. So how can I register the parent class, Item, like what should I code instead in the constructor?
     
  4. Offline

    SkyleTyler1337

    Add into the main class

    getServer().getPluginManager().registerEvents(new ZealPVPListener(this), this);

    and add into the Listener

    Import me.skylertyler.main.MainClass

    public MainClass class;

    public ZealPVPListener(MainClass class){
    this.class = class;

    }
     
  5. Offline

    nlthijs48

    TigerHix I think the super(); call is not needed, because that registers the Item class as the eventhandler, but you want to register the SpecialItem to handle your events. The registerEvents in the constructor of the SpecialItem class should register all of them because it inherits the methods from Item.
     
  6. Offline

    drtshock

    Your class doesn't extend another class so how were you doing super() without it giving you an error? O_O

    Pass an instance of your main class to your other class, don't access it statically like that.
     
  7. Offline

    TigerHix

    drtshock Well thx, but once again its just an pseudo code *facepalm*.. The SpecialItem class extends Item class as code above.
     
  8. Offline

    drtshock

    Oh, didn't see that. You should try pasting code in the syntax=java tags :)

    Are you still having issues with registering your listener in another class?
     
    TigerHix likes this.
  9. Offline

    TigerHix

    SkyleTyler1337 I am not asking how to register events, thanks though...
    nlthijs48 I just test it out, and all events won't be registered.

    Yep.. can't figure out wts going on :p

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

    drtshock

    Try putting your classes in a pastebin or hastebin or even a gist to make it easier to read. Then let me know what you're still having issues with and I'll see what I can do :)
     
Thread Status:
Not open for further replies.

Share This Page