Event System
The event system is a core part of Flourish housing almost all functionality.
As such, the code is as optimized as possible as it is possible it gets called a lot.
Creating Events
Creating events means creating a new class with the parameters required, no annotation is needed for this class.
Here is an example event class:
public class PacketSendEvent {
private final Packet<?> packet;
private boolean cancelled;
public PacketSendEvent(Packet<?> packet) {
this.packet = packet;
}
public Packet<?> getPacket() {
return packet;
}
public boolean isCancelled() {
return cancelled;
}
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}
As you can see, the class name is in the format of EventName(Event), this should be done for every event.
Another thing you can notice is we make a constructor with the incoming parameters of the event. While this may use lombok later, this is currently the way to do it.
Calling your event
Now you need to call your event from the place where it needs to be sent to the listeners, this is commonly in a mixin.
Heres an example:
@Inject(at = @At("HEAD"), method = "doSendPacket", cancellable = true)
public void doSendPacket(Packet<?> packet, PacketSendListener packetSendListener, boolean bl, CallbackInfo ci) {
PacketSendEvent sendEvent = EventManager.getInstance().call(new PacketSendEvent(packet));
if (sendEvent.isCancelled()) {
ci.cancel();
}
}
Listening for events
Some things such as Modules are automatically subscribed to events when they are enabled, but for other things, you need to subscribe a class manually:
If you want to do a specific instance of a class (non-static methods):
MyEventListener listener = new MyEventListener();
EventManager.getInstance().registerClass(listener);
If you want to subscribe static methods:
EventManager.getInstance().registerClass(MyEventListener.class);
and now, create a @EventHandler method.
public class MyEventListener {
@EventHandler
public void onPacketSend(PacketSendEvent event) {
System.out.println(event.getPacket());
}
}