neoforged / Bus

Event firing and listening framework, based on the event bus concept

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove ability to receive event subclasses in a parent listener

Technici4n opened this issue · comments

I don't see the use case, and it makes the implementation of ListenerList a lot more painful.

Relaying from discord:

There exists a valid usecase for reception of subclass listeners in NeoForge itself: EntityTeleportEvent
It can be fired as itself, with no additional context, or as a subclass providing additional context. Listeners often opt to receive the supertype instead of a subclass to receive all teleports.

This can be remedied by having an arbitrary context object attached to the teleport event, but is more gracefully solved through subclassing the event.

That said a means to prevent a class from being listened to (so that consumers do not accidentally listen to a parent event where this is not the intention) should be invented.
To that end, only two solutions are viable:

  1. Make abstract classes unlistenable
  2. Add an @Unlistenable annotation to mark unlistenable classes.

The second approach is most flexible. Other approaches are not viable as the unlistenable property must be attached to the class, and introducing a static class-keyed data structure is inadvisable.

Yes, @Unlistenable seems to be the solution.