Solved What does Cancellable do to an Event?

Discussion in 'Spigot Plugin Development' started by MightyOne, May 30, 2018.

  1. Greetings everyone,

    I was creating some custom events for a while now and I just noticed that I do not know exactly what the Interface Cancellable is for xD

    In every event so far I created a BukkitRunnable to check a tick later if the event got cancelled. So in every case, true and false, I ran a different piece of code. Taking a look into the BukkitAPI I just noticed that no event is doing anything similar at all...

    So how does this cancelling work and how does it prevent some piece of code of running?
    Shouldn't an event fire it's code irrevocably as soon as it exists?

    Thanks for any explanation, I hope I did not miss any anywhere
    MightyOne
     
  2. I don't think there's something special happening there. It just check if the event is canceled, if it is. Then the server will cancel the event
     
    • Like Like x 1
  3. Waiting one tick isn't a good idea because there could be scenarios where you need to wait longer than 1 tick before cancelling the event.
    You should just write the code that is fired after that one tick in the setCancelled Method
     
    #3 CreepyCode, May 30, 2018
    Last edited: May 30, 2018
    • Agree Agree x 1
    • Informative Informative x 1
  4. Let's say you have a custom event called PlayerJoinGameEvent which is fired when a player joins some sort of game. Your code would look a bit like this, to allow cancellation

    PlayerJoinGameEvent e = new PlayerJoinGameEvent(player, game);
    Bukkit.getPluginManager().callEvent(e);
    if(!e.isCancelled())
    {
    game.join(player);
    }

    The idea is that the event is called before the actual implementation is done, so any event handlers can do what they want with it, and then cancel it if necessary. Obviously this is a bit more difficult with Async events, and my example wasn't the best as why would you cancel a player joining a game? Still, hope this helps you to gain some understanding.


    Sent from my SM-G903F using Tapatalk
     
    • Agree Agree x 1
    • Useful Useful x 1