Solved Is allowing event to be cancelled from constructor is a good practice?

Discussion in 'Plugin Development' started by mibac138, Jan 30, 2015.

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

    mibac138

    For example

    Code:
    package ....;
    
    import ....;
    
    public final class MyCustomEvent extends Event implements Cancellable {
        private boolean cancelled;
    
        public MyCustomEvent(Player p, World w, Location l, Entity e, boolean cancelled) { .... } //Example arguments
    }
    
    So, is it good?
     
  2. Offline

    mrCookieSlime

    Moved to Plugin Developement.
     
    mibac138 likes this.
  3. Offline

    Lolmewn

    You can do that, but I would then also add a constructor that doesn't have the boolean and simply invokes your constructor with false added.

    So basically public MyCustomEvent(Player p, World w, Location l, Entity e) { this(p, w, l, e, false); }
     
  4. Offline

    mibac138

    @Lolmewn yea, I know, in my actual class I have done it already. Thanks! :)
     
  5. Offline

    Lolmewn

    Logically speaking, when you create an event, you pretty much say 'Something is happening', and others can cancel that. Not sure why you would want to do it when creating the class :)
    Also, isn't having a World and a Location a bit overkill? You can just get the world by doing l.getWorld(), and you can even get the location by doing p.getLocation() (or e.getLocation(), whichever makes more sense).
     
    mibac138 and WinX64 like this.
  6. Offline

    WinX64

    @mibac138 In fact, a similar situantion exists, it being PlayerInteractEvent(which is by default, fired with its state already cancelled if your interact with air).
     
    Konato_K and mibac138 like this.
  7. Offline

    mibac138

    @Lolmewn I know, it's a fast writed exmaple :p

    @WinX64 Thanks, didn't know about it. :)

    @Edit Can I call event only in my plugin? Ex. PreEntityFallOutOfWorldAndSadlyDieEvent?
     
  8. Offline

    WinX64

    If it's a custom event that you made, it's mostly likely only get called by you anyway, since they're never going to fired by craftbukkit itself. Is that what you mean?
     
    mibac138 likes this.
  9. Offline

    mibac138

    Yes, I think so it's it. Thanks again! :)
     
Thread Status:
Not open for further replies.

Share This Page