Ignoring Armor with e.setDamage()

Discussion in 'Plugin Development' started by tommyhoogstra, Apr 26, 2014.

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

    tommyhoogstra

    Trying to do a fixed amount of damage to all players, whether they're wearing armor or not.
    e.setDamage in the EntityDamagebyEntityEvent doesn't seem to do the trick (0 damage to protection IV and stuff like that) but works find with armorless patients.

    Tips pls
     
  2. Offline

    Gater12

  3. Use player.setHealth(player.getHealth() - damage);
    Make sure to check if the damage dealth minus the health is greater than 0, if less than or equal to 0, set the health to 0.
    Code:
    if (player.getHealth() - damage <= 0D) player.setHealth(0D);
    else player.setHealth(player.getHealth() - damage);
     
  4. Offline

    tommyhoogstra

    KingFaris11 didn't think of that at all. Thanks!

    Gater12 yeah wasn't thinking properly -.-
     
    KingFaris11 likes this.
  5. Offline

    Gater12

    KingFaris11 likes this.
  6. Offline

    stink123456

    Or you could be a hardcore coder that adds the % of damage reduction back to the damage! :D
     
  7. I was about to say player.damage() but then I thought that this would damage them normally and fire EntityDamageEvent (or if parsed with an entity argument too, EntityDamageByEntityEvent) and cause a loop.
     
  8. Offline

    tommyhoogstra

    KingFaris11
    Gater12
    Code:java
    1. @EventHandler
    2. public void fireballDamage(EntityDamageByEntityEvent e) {
    3. // Player take = (Player) e.getEntity();
    4. Entity att = e.getDamager();
    5. Player p = (Player) e.getEntity();
    6.  
    7. if(att instanceof LargeFireball){
    8. if(p.getHealth() - 5D <= 0D){
    9. p.setHealth(0D);
    10. }
    11. else{
    12. p.setHealth(p.getHealth() - 5D);
    13. }
    14. }
    15. }


    Seems to one shot anyone with full health if its a direct hit rather than doing only 2.5 hearts.

    Any Ideas?
     
  9. You forgot to cancel the damage event, but since you want the damage effect probably, use e.setDamage(0D);
     
  10. Offline

    tommyhoogstra

    I dont see how that stops them from only taking 2.5 hearts.
    Edit: turns out it works anyway, thanks!
     
    KingFaris11 likes this.
  11. Let me explain it to you:
    What you did before was take away the health BUT the damage event wasn't cancelled and so the REAL damage that was meant to be done is dealt. So basically, you're meant to create an alternative to EntityDamageByEntityEvent - therefore must either cancel the event or set the damage to 0.
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page