Custom Events

Discussion in 'Spigot Plugin Development' started by Hunky524, May 26, 2016.

  1. I am wondering how Spigot and Custom Events know when they should and when they should not fire. Let me go in more detail. I was reading on Custom Events in the Bukkit wiki and creating a custom event seems fairly straight forward except for one part; how does the event know what it should and shouldn't deal with. For example, if you had two custom events, one was dealing players attacking each other in a minigame, while the other was dealing with players clicking specific blocks in the minigame. How does minigameAttackEvent know not to fire when the player does not attack anything, and how does miniGameClick event know not to fire if the player has not clicked a block. The wiki seems to give a very generic example and I haven't found many (if any) custom event guides that go into more detail except for one on the bukkit forums. Or maybe I am thinking about this all wrong and every time something happens in the game, every single registered event gets fired and it's up to the customEvent class to filter out what it needs. If anyone could maybe elaborate on the wiki example or explain how events differentiate themselves I would be very grateful :)

    A slightly different example might be, if you have a PlayerDeathEvent and an InventoryClickEvent, how does the death event know to fire on a player death while InventoryClick does nothing and vice-versa?

    Here is the wiki link:
  2. Imagine that you're in the situation and read the wiki carefully and understand what it does and what it says.
  3. clip


    If you make a custom event you have to call the event yourself when you want it to fire.
    • Agree Agree x 1
    • Friendly Friendly x 1
  4. You (the event creator) call the events yourself. See this section

    For example in a plugin I was working on I had a MineResetEvent. Inside the method for resetting a mine I would build a new MineResetEvent and call it with the method above. If you don't run the callEvent method nothing is going to happen. There is no magic that takes a look at the name of your class and decides when things should happen.
  5. Think of events as a person that screams out "Hey! This just happened, or is about to happen!" You need to create the event and call it. Then, depending on what your event allows modification of, you need to update what's going on in your code using the values from the event after its been called.

    The call process actually stops your code right where it was called. All listening plugins will get a chance to listen to it on all priority levels, and then finally your code will continue like normal.

    Take this for example. Lets say you are teleporting a player somewhere.
    Code (Text):

    Location loc = new Location(world, 100, 64, 150);
    Simple enough. But lets add an event to it to notify other plugins about this and allow them to cancel or even manipulate the teleport location:

    Code (Text):

    Location loc = new Location(world, 100, 64, 150);

    // create the event and populate with data
    CustomEvent myEvent = new CustomEvent(player, loc);

    // call the event (notify all other plugins)

    // check if event was cancelled (if event is cancellable)
    if (myEvent.isCancelled()) {
        return; // some plugin cancelled the event, take no action.

    // load new data from the event (in case any plugins changed the event)
    loc = myEvent.getLocation();

    // finally teleport the player
    Make sense?
    #5 BillyGalbreath, May 26, 2016
    Last edited: May 26, 2016
    • Agree x 2
    • Informative x 2
    • Like x 1
    • Winner x 1
    • Useful x 1
  6. From what I understand, you're trying to understand how Bukkit knows when to call an event and your custom event. Basically, the way this works is it is only called when you specifically call it . As mentioned on the wiki you linked,

    You are in control of creating and calling your events, where you call it is completely up to you. Here's an example

    // Create the event here
    CustomEvent event = new CustomEvent("Sample Message");
    // Call the event
  7. Magical minecraft faries watch over what you do and call the event of you do somthing :)
    • Funny Funny x 6

  8. That would be to manually call the event though, what if I wanted to make a custom InventoryClickEvent for whatever reason. How would Spigot know when to call my event versus any other event?

    Lets say I had this class with this code:
    Code (Text):
    public class CustomEvent implements Listener {
         public void customClick(CustomClickEvent event) {
              //do stuff
    What lines of code in the CustomEvent class make this specific event fire on an InventoryClick and not when a player dies, or breaks a block, or anything else?
  9. Please read my previous post. I edited it with an example of how to call an event.
  10. Ohhhhhh, I think I understand. So I'll use the InventoryClickEvent as an example. So somewhere in the spigot code, they detect using some sort of magic at a much deeper level when a player clicks an inventory slot, and then manually call the event using #callEvent(event). Then all Listeners can pickup on that call and perform actions from that callEvent.

    So, for example, if I wanted to make a custom event that fired from a player clicking an inventory (for whatever reason). I would make an InventoryClickEvent and call my custom event within the InventoryClickEvent. If you wanted to make a custom PlayerMoveEvent you could have some sort of runnable that detects changes in player positions and then call your custom event if a player has moved. I'm not saying either of those are good ideas just examples of things you could do.

    I understood that events could be called manually with #callEvent, but it just seemed like some of the Spigot events just got called automatically somehow, but they actually just manually detect those at a much deeper level then call the events.

  11. I wasn't saying I wanted to call the InventoryClickEvent myself, I was saying if you wanted to have your own CustomInventoryClickEvent, you would have to make a InventoryClickEvent Listener class and call your own CustomInventoryClickEvent within the method that uses the InventoryClickEvent as its parameter. Like this:
    Code (Text):
    public void onInvClick(InventoryClickEvent e) {
         //call CustomInventoryClick
         //do other sutff
  12. Yes, this is what you would want to do.
  13. just telling If,