Can't setCancelled() a custom event?

Discussion in 'Plugin Development' started by Mr Burkes, Jun 28, 2012.

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

    Mr Burkes

    Hi, I'm building a custom event with the following code:



    Code:
    package me.MrBurkes.KapipFeather.Event;
     
    import org.bukkit.Location;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Cancellable;
    import org.bukkit.event.Event;
    import org.bukkit.event.HandlerList;
     
    public class KapipTeleportEvent extends Event implements Cancellable {
        private static final HandlerList handlers = new HandlerList();
        private Player p;
        private Location b;
        private boolean cancel;
     
        public KapipTeleportEvent(Player player, Location b, Location to) {
            p = player;
            this.b = b;
         
            if(!isCancelled()) player.teleport(to);
        }
     
        public KapipTeleportEvent(Player player, Location b, Entity to) {
            p = player;
            this.b = b;
         
            if(!isCancelled()) player.teleport(to);
        }
     
        public Player getPlayer() {
            return p;
        }
     
        public Location getFrom(){
            return b;
        }
     
        public HandlerList getHandlers() {
            return handlers;
        }
     
        public static HandlerList getHandlerList() {
            return handlers;
        }
     
        @Override
        public boolean isCancelled() {
            return cancel;
        }
     
        @Override
        public void setCancelled(boolean arg) {
            this.setCancelled(arg);
            cancel = arg;
        }
    }
    


    But when I use e.setCancelled(true); I get the following stackTrace:

    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)
    at me.MrBurkes.KapipFeather.Event.KapipTeleportEvent.setCancelled(KapipTeleportEvent.java:51)

    What gives?
     
  2. Offline

    Sagacious_Zed Bukkit Docs

  3. public void setCancelled(boolean arg) {
    this.setCancelled(arg);
     
  4. Offline

    Mr Burkes

    But that creates the loop because its recursive O. o. So the question is: what do I put in the setCancelled() method?
     
  5. Offline

    nisovin

    Just remove that recursive call. There's no need for it.
     
  6. Offline

    Mr Burkes

    That means that nothing is in my setCancelled() method! What goes there?
     
  7. Offline

    Sagacious_Zed Bukkit Docs

    what you had
    Code:
        @Override
        public void setCancelled(boolean arg) {
            this.setCancelled(arg);
            cancel = arg;
        }
    if you remove the recursive call what do you get?
     
    ferrybig likes this.
  8. Offline

    Mr Burkes

    Nothing happens
     
  9. Offline

    Sagacious_Zed Bukkit Docs

    Isn't that the point? if something happened and you cancel it, nothing happens ......
     
    ferrybig likes this.
  10. Offline

    aPandaification

    Mind = Blown
     
  11. Offline

    CorrieKay

    You have two lines of code. The first line calls the method. which will call the method. Which will call the method. Which will call the method.

    Your method technically is recursive, but you dont ever END it.

    You just need this:

    Code:
        @Override
        public void setCancelled(boolean arg) {
            cancel = arg;
        }
     
  12. Offline

    Mr Burkes

    No, what I meant is that the event still happens.
     
  13. Of course it does... that's the reason for the event...

    I don't think you fully understand what custom events are for... I noticed you used a teleport command on your constructor... that will *always* happen because that triggers when you create a new instance of your event, therefore other plugins can't even get a chance to cancel it.

    The .callEvent() triggers the event on all other plugins, gathers data and sets it in your event... an example:
    Code:
    YourEvent callEvent = new YourEvent(...); // create event instance
    Bukkit.getPluginManager().callEvent(callEvent); // send it to all plugins, after this, all plugins have processed the event and you can use callEvent to get stuff from your event that other plugins might have changed.
    
    if(callEvent.isCancelled()) // some plugin cancelled the event, do something to prevent actions
    {
    }
    You can get any of the created methods afterwards, they will be the ones modified by other plugins and you can act accordingly to them.... this system provides alot of options.
     
    CorrieKay likes this.
  14. Offline

    Mr Burkes

    Thanks, nobody bothered to explain that to me, so now I know :D
     
  15. Offline

    rominos2

    Think about writing solved in the Thread name.
    To avoid people to search a solution and then see at your last message
     
Thread Status:
Not open for further replies.

Share This Page