Entity's health not being updated before events fire

Discussion in 'Plugin Development' started by bwfcwalshy, Dec 21, 2015.

Thread Status:
Not open for further replies.
  1. I was developing a plugin today and it displays the mob health when hit in an action bar. I noticed on the first hit it is 100/100 still (Yes, my mob has 100 health :p Deal with it). I have a feeling the Entity health doesn't update before the event is fired so it is returning the wrong value.

    My code:
    Code:java
    1. ActionBarUtil.sendActionBar(mob.getName() + ChatColor.GRAY + " - " + ChatColor.RED + (int) ent.getHealth() + ChatColor.GRAY + "/" + ChatColor.RED + (int) ent.getMaxHealth(), player);


    This is using the EntityDamageByEntityEvent.
    So I think I would have to actually just work out the damage with e.getDamage() and make it (mob.getHealth() - e.getDamage());

    Just thought I'd share this.
     
    Zombie_Striker and mine-care like this.
  2. Offline

    Zombie_Striker

    @bwfcwalshy
    Well, since it is cancelable and you can change the amount of damaged applied, it has to get triggered before the damage is actually applied. A simple 1 tick delay should fix this problem.
     
  3. @Zombie_Striker Hmm, I didn't think of doing a delay actually. But I think I'll just do health - damage :p
     
  4. Offline

    teej107

    @bwfcwalshy A scheduled task or setting the health in the BossBar in MONITOR'd event would be more accurate if a plugin changes the outcome.
     
  5. Yeah, seems kinda risky using health - damage. Some other plugin may cancel or change the damage, even after you assumed it's been dealt? Unless you're using Monitor priority, obviously.
     
Thread Status:
Not open for further replies.

Share This Page