Tutorial EventPriorities

Discussion in 'Resources' started by CraftBang, May 3, 2015.

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

    CraftBang

    Little explanation in EventPriority.
    I wanted to share this with everyone because I've seen a lot of things about EventPriorities etc.
    If the priority is HIGHER it will be run later, as you can see below I have 2 of the same events.
    One will modify the damage and the other one will also print the modified damage.
    So in fact, a "HIGHER" priority doesn't means it'll be run as first, it does the opposite. I think a lot of people are thinking that a HIGHER priority means that the code will be run as first.

    Code:
     
        @EventHandler(priority = EventPriority.NORMAL)
        public void entityDamageE1(EntityDamageEvent e){
            if(e.getEntity() instanceof Player){
                if(((Player) e.getEntity()).getName().equals("ThomasssMC")){
                    Bukkit.broadcastMessage("E1 Original damage: " + e.getDamage());
                    e.setDamage(e.getDamage() - 1);
                    Bukkit.broadcastMessage("E1 Modified damage: " + e.getDamage());
                }
            }
        }
       
        @EventHandler(priority = EventPriority.HIGH)
        public void entityDamageE2(EntityDamageEvent e){
            if(e.getEntity() instanceof Player){
                if(((Player) e.getEntity()).getName().equals("ThomasssMC")){
                    Bukkit.broadcastMessage("E2 damage: " + e.getDamage()); // Should be the same as Modified damage in E1
                }
            }
        }
    
    Proof:
    http://gyazo.com/4e31addcc3a1305a9648f05a708912ce

    If you have anything to share about priorities please say so in the comments :).
     
    Last edited: May 3, 2015
  2. Offline

    chasechocolate

    You may want to make your code broadcast "E1" so it matches your screenshot.
     
  3. Offline

    teej107

  4. Offline

    CraftBang

    @chasechocolate my bad, I edited it and didn't copy paste it
    @teej107 oww.. but still I think a lot of people think with this kind of logic:
    Higher priority = code will be run before lower priority.
    But it's exactly the opposite right :p.
     
    chasechocolate likes this.
Thread Status:
Not open for further replies.

Share This Page