List of EventHandlers

Discussion in 'Plugin Development' started by handsomedonkey, Aug 3, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    handsomedonkey

    Newb question but the Javadocs website is down (http://jd.bukkit.org/) and on the bukkit API page, I could only find classes but not the methods under the Listener interface. Is there any other documentation I can look through to find a list of all EventHanders are in the API or am I just blind?


    P.S.
    Sorry, first time coding a plugin.
     
  2. Offline

    Shqep

    @handsomedonkey
    Don't have to be sorry.

    1. The Javadocs website has been down for quite a long time already, you would want to use a third party site. For example, spigot hub's javadocs.
    2. Listener is an interface for you to implement. It's an empty interface. If you meant you wanted to find a list of all events that you can listen to:
    Show Spoiler

    Here's how you use the Listener interface:
    PHP:
    class YourHandlerClass implements Listener {
        @
    EventHandler // This is mandatory
        
    public void whateverNameWorks(Event event) {

        }
    }

    // In your onEnable() method or somewhere:
    getServer().getPluginManager().registerEvents(new YourHandlerClass(), pluginInstance);
    For the Event event, you can use any event instance available. For example, if you wanted to listen when a player breaks a block, there's BlockBreakEvent.


    If I interpreted it wrong, just say so.
     
  3. Offline

    handsomedonkey

    Ah thanks. I think my question wasn't worded clearly. I meant to ask if there was some list of certain keywords you must use where you have written 'whateverNameWorks' (The functions name)? Or can you just make any name up? If you make it up how does Bukkit know when to trigger that method?
     
  4. Offline

    Shqep

    @handsomedonkey
    You can use any name for the method, they use reflections to look up that method to invoke it later. Here's a checklist for you to see whether Bukkit knows that your method exists:

    1. The function must have 1 parameter, this parameter type must be an instance of Event (BlockPlaceEvent, PlayerDeathEvent, etc.)
    2. The function must be annotated with @EventHandler
    3. The function must reside within a class or an instance that implements the Listener interface.
    4. You must register that Listener instance when your plugin enables.
     
  5. Offline

    handsomedonkey

    ohh. Thanks a lot.

    Just out of curiosity, how do they use reflections to look up methods?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 5, 2021
  6. Offline

    Shqep

    When you register the listener instance, they look up all of the methods using getDeclaredMethods. Then they iterate through that list of methods, and only pick the ones with EventHandler annotation and only takes 1 parameter of type Event. Probably some more sorting.
    Roughly like that. And when the event happens, they just look up the handlers that take in the happening event, and invoke all methods registered.

    Don't really know all of their internals, but I'm assuming something like that.
     
  7. Offline

    handsomedonkey

    oh interesting. Thanks
     
Thread Status:
Not open for further replies.

Share This Page