Solved Pass owning plugin to Listener class

Discussion in 'Spigot Plugin Development' started by Iwitrag, Jun 30, 2016.

  1. Good day,

    This is my first day of Minecraft plugin development, so sorry for this noob question.
    I'm working with meta, I need to include one double value to fired arrow. I have separated class for my
    onEntityShootBow method (@EventHandler).

    I have tried this:
    Code (Text):
    e.getProjectile().setMetadata("damageMultiplier", new FixedMetadataValue(new Main(), damageMultiplier));
    but it results in Java.lang.IllegalArgumentException: Plugin already initialized!

    So, my question is, how can I pass my Plugin instance into Listener? I have tried to change Listener's method signature (adding Plugin pl parameter) but that resulted in Error: plugin tried to register Listener with invalid signature...

    Any ideas ?
     
  2. You have 2 options:

    * Pass the main instance on the listener constructor (easier when making a small plugin).

    * Save a static instance variable and make a method to get it.
     
    • Informative Informative x 1
  3. Either make a static instance or..

    Code (Text):

    public ListenerClass(Main pl) {
      this.pl = pl;
    }

    // In main class
    new ListenerClass(this);
     
    • Winner Winner x 1
  4. Thank you guys a lot!

    I used constructor parameter.