Listen to all events easily

Discussion in 'Spigot Plugin Development' started by Proximyst, Apr 25, 2017.

Thread Status:
Not open for further replies.
  1. So, I'm developing a Bukkit wrapper usable for Clojure. I'm wondering if there is any way at all to listen to all events at once?
    I'd much rather to that than make eventhandlers for each and every event there is in the API, naturally..
     
  2. #2 Trigary, Apr 25, 2017
    Last edited: Nov 16, 2018
    • Agree Agree x 1
  3. I'd say this should work:
    Code (Java):
    @EventHandler
    public void onEvent(Event event){
        // should be called for all events
    }
     
  4. If I understand what you mean correctly, you could do
    Code (Text):
    @EventHandler
    public void onEvent(Event e) {

    }
    and then you would need to do some stuff with the handlers. I've done this before and I'll send the code when I get home.
     
    • Like Like x 1
  5. I assume I'll need to scan packages of events and add the listener to the HandlerList after this. I'll try it out when I get home.
     
  6. Ok, I just tested it out of curiosity and this is the error you get:
    Code (Text):
    org.bukkit.plugin.IllegalPluginAccessException: Unable to find handler list for event org.bukkit.event.Event. Static getHandlerList method required!
     
    The next thing I tried was using all the direct known subclasses of event. For some (for example AsyncPlayerPreLoginEvent) that works but others get the same error as above (for example BlockEvent).
     
    • Like Like x 1
  7. Then let's work around the error
     
  8. I have the code to solve this; I'm at school currently but when I get back I'll send it.
     
  9. Only the events that are having no children can be used by the event system. That's also the reason why you can't listen to the PlayerEvent.
     
  10. So you are creating a wrapper for something that is already a wrapper for NMS?
     
    • Funny Funny x 1
  11. Choco

    Moderator

    That's not necessarily true. Take for example, InventoryClickEvent, which has 2 events that extend it, CraftItemEvent and InventoryCreativeEvent. The true statement would be, "You can only listen for events that contain a HandlerList field and a static #getHandlerList() method to retrieve it"

    Events are Bukkit-specific. They are called by patching NMS and injecting code rather than wrapping NMS. If Bukkit were not to exist, events would not exist in vanilla Minecraft
     
    • Like Like x 1
    • Agree Agree x 1
    • Informative Informative x 1
  12. I should correct myself. If you listen to InventoryClickEvent you aren't automatically listen to CraftItemEvent or InventoryCreativeEvent.
     
  13. that's the thing tho, you do
     
  14. Then something changed. In the past it didn't work. Around 1.6/1.7 I tested it with no success.
    Checks like inventoryClickEvent instanceof CraftItemEvent didn't work.
     
  15. Code (Text):
            RegisteredListener registeredListener = new RegisteredListener(this, (listener, event) -> onEvent(event), EventPriority.NORMAL, MAINCLASS, false);
            for (HandlerList handler : HandlerList.getHandlerLists())
                handler.register(registeredListener);
           
    I'll leave it up to you to figure out where to put this. (Will remove the error from the code above)
     
    • Informative Informative x 1
  16. Wow, nice. That works.

    Now I get a feeling for how many events are being fired all the time :D



    Edit: I find it quit interesting which events get called when and in what order. For example on login:
    Code (Text):
    [23:33:08] [User Authenticator #2/INFO]: AsyncPlayerPreLoginEvent
    [23:33:08] [Server thread/INFO]: PlayerPreLoginEvent
    [23:33:08] [User Authenticator #2/INFO]: UUID of player Arvindir is 35b4cd5b-e53f-304d-8f13-0a22d8ea4bf9
    [23:33:08] [Server thread/INFO]: PlayerLoginEvent
    [23:33:08] [Server thread/INFO]: PlayerSpawnLocationEvent
    [23:33:08] [Server thread/INFO]: PlayerJoinEvent
    [23:33:08] [Server thread/INFO]: PlayerTeleportEvent
    [23:33:08] [Server thread/INFO]: Arvindir[/127.0.0.1:59163] logged in with entity id 9060 at ([world]187.45244950975015, 99.0, 247.35947289219186)
    [23:33:08] [Server thread/INFO]: ChunkLoadEvent
    [23:33:08] [Server thread/INFO]: ChunkLoadEvent
    [23:33:08] [Server thread/INFO]: ChunkLoadEvent
     
    #17 Nikl, Apr 25, 2017
    Last edited: Apr 25, 2017
    • Like Like x 1
    • Informative Informative x 1
Thread Status:
Not open for further replies.