Skip to content

Listening to Events

Devan-Kerman edited this page Jul 30, 2020 · 5 revisions

Listening is much simpler than making events, Listeners are declared in a properties file, same protocol as events

  ...
  "depends": { // example
    "fabricloader": ">=0.8.2+build.194",
    "minecraft": "1.15.2"
  }, // example
  "custom": {
    ...
    "nano:lst": "listener.ini" // this can be an array as well
    ...
  }
  ...
}

And in listener.ini

; MySumListener is the class that has the listener in it, the part after the # is the method name
; the signatures are automatically matched.
; the default event apply manager takes 1 parameter `listeners`, so that's what we're using here
[mymod:event]
listeners=net.mymod.listeners.MySumListener::listen

We'll be continuing on the event example here MySumListener.java

public class MySumListener {
   // as of now, all listeners must be declared public
   // and they will always have to be static
   public static int listen(int sum) { // the method signature must match the invokers signature, or bad things will happen
      return sum + 4;
   }
}

And that's it.

Clone this wiki locally