Solved How does listener works

Discussion in 'Plugin Development' started by Ragnazar, Jul 22, 2013.

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


    As the listener is running?
    How Bukkit detects an event methods and the type of event?
  2. Offline


    I believe CraftBukkit gets a list of all methods marked "@EventHandler". When an event is triggered (e.g. moving vehicle) CraftBukkit checks the list for a method that has the right parameter for the right event(e.g. VehicleMoveEvent), and if they match, CraftBukkit executes.

    For example:
    1. @EventHandler
    2. public void event1(VehicleMoveEvent e)
    3. { ...}
    5. @EventHandler
    6. public void event2(EntityDamageByEntityEvent e)
    7. {...}

    CraftBukkit gets the list of all methods marked "@EventHandler". That would get us event1 and event2 methods.

    Now, let's say a player hits a cow. Then, CraftBukkit would check the list. The first method, event1, has a parameter "VehicleMoveEvent", which isn't the event, so Craftbukkit moves onto the next. Is the hitting an "EntityDamageByEntityEvent"? Yes, it is. CraftBukkit executes event2.

    This is a basic rundown on events.

    Disclaimer: I am no expert on CraftBukkit, so there may be errors. Please correct if there are.
  3. Offline


    So, how to get list of all methods marked "@EventHandler"
    And how to get parameters of the method?
  4. Offline


    You don't manually determine what method gets executed, CraftBukkit does.

    The parameter of the method will be determined on what you want to do. If you wanted to check for possible griefing, then your parameter would be BlockBreakEvent (org.bukkit.event.block.BlockBreakEvent).

    Take a look at the org.bukkit.event.* classes in the API. They will tell what each event is for.
  5. Offline


    I know it. It just interesting how does listener works.
    How bukkit finds listeners and annotations and add these methods in some sort of list
    But more interesting is how bukkit detects event type? How does he know that event1(BlockBreakEvent event) is BlockBreakEvent ?
  6. Offline


  7. Offline


    Okay. I found way to get parameters and EventHandler annotation but how bukkit gets classes in my package that implements Listener?
  8. Offline


    When you register your listener class.
  9. Offline


    But Listener interface is completely empty
    1. package org.bukkit.event;
    3. /**
    4.  * Simple interface for tagging all EventListeners
    5.  */
    6. public interface Listener {}
  10. Offline


    1. You create your Listener class with all the @EventHandler Methods
    2. You register your Listener with PluginManager#registerEvents(Listener, Plugin);
    3. Within the method it looks for all Methods in the Listener class, if they have a @EventHandler Annotation and exactly 1 Parameter which's type extends Event
    4. It maps every Method to the type of the parameter
    5. it registers every method to the event's HandlerList

    When it comes to an event it goes through the HandlerList and executes every saved method. (The order should be defined by the EventPriority, when 2 methods have the same priority it's either who came first, or just random - don't know^^)

    Pretty simple explained, not complete and maybe not entirly correct.

    If you understand Java, you should take a look into the Bukkit Source. Pretty interesting and a good way to improve your Java skills.
    Ragnazar likes this.
  11. Offline


    RegisterEvents! That makes sense...
  12. Offline


    Moved to Plugin Development.
Thread Status:
Not open for further replies.

Share This Page