Skip to content

Creating Events

Devan-Kerman edited this page Aug 23, 2020 · 3 revisions

Contributor Docs

Creating Events

Iridis uses NanoEvents for throwing events, along with it's annotation processor To create an event first create or find the event invoker category class of the event you want to make, this is where you should put invokers.

Player

Blocks

Entity

Server

This is the format of an invoker

import net.devtech.nanoevents.api.annotations.Invoker;

public class EventCategory {
   @Invoker(
      value = "iridis:eventid", // the id of the event
      args = <annotation arg type>) // the annotation arg type for this invoker, the method must have an annotation of this type
   <annotation args>
   public static <return type> eventName(<parameters...>) {
      <pre logic>
      Logic.start();
      // everything between here
      <event logic>
      eventName(<parameters...>);
      // and here, will be copy-pasted for every listener, and the recursive call is substituted for an invocation of the listener method
      Logic.end();
      return <default return value>;
   }
}

Examples

Events implemented in a single mixin

import net.devtech.nanoevents.api.annotations.Invoker;
import io.github.iridis.internal.events.annotations.SingleMixin;

public class EventCategory {
   @Invoker(
      value = "iridis:eventid", // the id of the event
      args = SingleMixin.class) // the annotation arg type for this invoker, the method must have an annotation of this type
   @SingleMixin(TargetClassMixin_EventName.class)
   public static <return type> eventName(<parameters...>) {
      <pre logic>
      Logic.start();
      <event logic>
      eventName(parameters...);
      Logic.end();
      return <default return value>;
   }
}

Cancellable events implemented in a single mixin

NanoEvents doesn't have priorities (intentionally) So all cancellable events should have post events: non-cancellable events that exist for viewing the outcome or result of an event.

import net.devtech.nanoevents.api.annotations.Invoker;
import io.github.iridis.internal.events.annotations.SingleMixin;

public class EventCategory {
   @Invoker(
      value = "iridis:preeventid",
      args = SingleMixin.class)
   @SingleMixin(TargetClassMixin_PreEventName.class)
   public static boolean preEventName(<parameters...>) {
      Logic.start();
      if(eventName(parameters...)) return true;
      Logic.end();
      return false;
   }

   @Invoker(
      value = "iridis:posteventid",
      args = SingleMixin.class)
   @SingleMixin(TargetClassMixin_PostEventName.class)
   public static void postEventName(<parameters...>) {
      Logic.start();
      eventName(parameters...);
      Logic.end();
   }
}

Invoking your Invoker

@Mixin(TargetClass.class)
public class TargetClassMixin_EventName {
   @Inject(...)
   public void myHandler(...) {
      EventCategory.eventName(...);
   }
}

Examples

cancellable pre-event implementation

@Mixin(TargetClass.class)
public class TargetClassMixin_EventName {
   @Inject(...)
   public void myHandler(...) {
      if(EventCategory.preEventName(...)) ci.cancel();
   }
}

Fabric API Compatibility

on the rare occasion fabric api has an event, we should prioritize using their handler over our own mixin.

  1. go to the FabricApi event delegation class, and add your invoker
  2. change your invoker to the following format
@Invoker (value = "iridis:server_start", args = {
	ModPresent.class,
	SingleMixin.class
}, applyManager = ModEventApplyManager.class)
@ModPresent ({
		"<fabric-api-module>",
})
@SingleMixin (<mixin class>)
public static void <eventName>(...) {
	...
}