Solved Executing listeners using Java Generics

Discussion in 'Spigot Plugin Development' started by jusjus112, Oct 19, 2020.

  1. Hi all,

    I'm trying to make custom mechanics for something with java generics.
    This is my current code, nothing advanced:
    Code (Java):
    public abstract class ObjectiveEvent<E> implements Listener {

      @EventHandler(priority = EventPriority.NORMAL)
      public void onEvent(E e){

      public abstract void execute(E e);

    And using it like this:
    Code (Java):
    addListener(new ObjectiveEvent<BlockBreakEvent>(){
          public void execute(BlockBreakEvent event) {
    But now I got this error:
    Code (Text):
    attempted to register an invalid EventHandler method signature
    I have looked it up but only came up with people using the listener api wrong.
    I was just wondering if someone else had this same problem or knows what's wrong.
    What I was thinking of is that the event api doesn't support generics.

  2. The method that is being called is (after compilation)
    Code (Java):
    public void onEvent(Object e)
    which is not a legal method (The event-system doesn't know which event to pass).
    Unfortunately, I'm not aware of any satisfying solution that gets the job done with generics (thou I and many others have tried).
  3. IntellectualSites


    You can use #registerEvent instead. Something like this should work:

    Code (Java):

    ObjectiveEvent<YourEvent> event = new ObjectiveEvent<>();
    pluginManager.registerEvent(YourEvent.class, event, EventPriority.HIGHEST, event, yourPluginInstance);

    public abstract class ObjectiveEvent<E extends Event> implements EventExecutor, Listener {

      public void execute(@NotNull Listener listener, @NotNull Event event) {
        this.execute((E) event);

      public abstract void execute(E event);

  4. Thanks, that worked. Interesting workaround tho.