How does Bukkit's event system work?

Discussion in 'Plugin Development' started by tommykins20, Apr 12, 2013.

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

    tommykins20

    Sorry this isn't really a plugin development question but it was the closest relevant forum. Anyway I was wondering how Bukkit's event system worked. I looked into the craftbukkit source but I never seemed to find how it actually called the events. If anyone knows any tips, sharing would be greatly appreciated. :)

    Happy crafting,
    -tommykins20
     
  2. Offline

    Reteckz

  3. tommykins20
    Basically, when the Minecraft code reaches some specific point it triggers the listeners from all plugins and gathers the data then applies the effects to the game code or prevents the action from happening if it was canceled.
    Most events are triggered before the action is happened so you can cancel them.

    An example of an event being called in the game code: https://github.com/Bukkit/CraftBukk...n/java/net/minecraft/server/Entity.java#L1409

    You can call events too by creating the event object and using Bukkit.getPluginManager().callEvent(), which sends your object instance to all listening plugins and alters it, then you can see if it was canceled, if something was changed and so on.
    But calling events won't trigger the game code, it will only trigger the events on other plugins, so make sure you're sending them with good reason and valid arguments.

    You can also create custom events, but it requires your plugin to be running and other plugins to add your jar as reference to be able to use your events, just like adding Bukkit jar to the build path, for more details see http://wiki.bukkit.org/Event_API_Reference#Creating_Custom_Events

    People already posted how to use it, I've answered the title question.
     
    Codisimus likes this.
  4. Offline

    tommykins20

    Thank you all for your replies, this extended my understanding however I think I mislead you by wording the question incorrect. What I meant was how does Bukkit call your onEvent() methods in your listeners with the EventHandler annotation. I apologise for any anger caused ;(
     
  5. Offline

    molenzwiebel

    The system uses reflection to determine if it should call a method by looking if it has the EventHandler annotation and the correct argument. It knows to look at your listener class because you registered it
     
  6. Offline

    tommykins20

    Thank you very much! :) I'm reading about it right now.
     
Thread Status:
Not open for further replies.

Share This Page