[SOLVED] Implements Listener

In Bukkit we could dedicate a class to MULTIPLE events. Currently as I see it, if I would like an EventHandler to be registered from another class I cannot use @Subscribe. I’m curious if the only way to have multiple handlers in one class is to make multiple inner classes?

The documentation shows how to do both ways. Great! Although… I’d MUCH rather be able to separate my handlers by sections (PlayerEventHandlers.java etc) without having to do something such as:

EventHandler<PlayerJoinEvent> playerJoinEventHandler = new PlayerEventHandlers.Join();
game.getEventManager().register(this, PlayerJoinEvent.class, playerJoinEventHandler);
EventHandler<PlayerQuitEvent> playerQuitEventHandler = new PlayerEventHandlers.Quit();
game.getEventManager().register(this, PlayerQuitEvent.class, playerQuitEventHandler);

In the above I would have my separate class PlayerEventHandlers and in it subclasses such as Join and Quit that handle their respective events by implements EventHandler<SomeEventClass>. I have to register so many classes, whereas I would much more prefer to register just 1 for each section (PlayerEventHandlers etc).

Am I not seeing something obvious, or is this by design?

I don’t get why you’re using EventHandler in this case. EventHandler is for dynamically registering handlers not known at compile time. The standard way is to just create your class with @Subscribe on it.

public class PlayerEventHandlers {

    @Subscribe
    public void onPlayerJoin(PlayerJoinEvent event) {
    }

    @Subscribe
    public void onPlayerQuit(PlayerQuitEvent event) {
    }
}

Then in your main class

public void someRegisterHandlerMethod() {
    game.getEventManager().register(getPluginInstance(), new PlayerEventHandlers());
}
3 Likes

I was under the impression that @Subscribe could only be used under classes annotated with @Plugin. The more ya know! Thanks :smile: