SpongeAPI icon indicating copy to clipboard operation
SpongeAPI copied to clipboard

Listener annotations on classes implementing EventListener?

Open ryantheleach opened this issue 8 years ago • 3 comments

Listener annotations / filtering annotations should ideally have an API exposed method of accomplishing the same goals as annotating classes with @Listener

At the moment it doesn't seem possible to filter cancellation status of classes implementing EventHandler.

Not sure if this is an API design issue, or an implementation issue at this stage.

@wolfizen

ryantheleach avatar Jul 07 '17 01:07 ryantheleach

Yea. I have a use case where I want to both use Event Filters (@IsCancelled specifically) and dynamic registration registerListener(plugin, T.class, EventListener<T>). This should of course be extended to any filtering annotation, not just cancellation.

An example of how I think this would work:

class MyEventListener<T extends Event> implements EventListener<T> {
    @Override
    @IsCancelled(Tristate.UNDEFINED)
    public void handle(T event) throws Exception {
        // do something, like log the event
        Sponge.getServer().getConsole().sendMessage(Text.of(event.getCause()));
    }
}

Sponge.getEventManager().registerListener(plugin, SomeEvent.class, new MyEventListener<>())
Sponge.getEventManager().registerListener(plugin, AnotherEvent.class, new MyEventListener<>())
...

Any annotation on the implemented interface method handle() would be treated the same as an annotation on a @Listener method.

Wolfizen avatar Jul 07 '17 01:07 Wolfizen

We could definitely support the method level annotations like @IsCancelled but I'm not sure how we could/should support parameter annotations in a clean way?

Deamon5550 avatar Jul 26 '17 17:07 Deamon5550

I see the point. Since the goal of an EventListener is to provide an interface, supporting arbitrary method parameters would prevent that. Looks like if this does get implemented it will have to be for method annotations only.

Wolfizen avatar Jul 27 '17 02:07 Wolfizen