neoforged / Bus

Event firing and listening framework, based on the event bus concept
GNU Lesser General Public License v2.1
3 stars 8 forks source link

Remove ability to receive event subclasses in a parent listener #7

Closed Technici4n closed 10 months ago

Technici4n commented 1 year ago

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

Shadows-of-Fire commented 11 months ago

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.

Technici4n commented 11 months ago

Yes, @Unlistenable seems to be the solution.

Technici4n commented 10 months ago

See #21.