Reduce damage from Iron armor

Discussion in 'Plugin Development' started by BloodShura, Feb 7, 2012.

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

    BloodShura

    Hi!

    This code isn't working. It doesn't give any errors, simply don't reduce the damage.
    (PS: Ignore the '//'!)

    OnEntityDamage:

    if (e.getEntity() instanceof Player) {
    //if ((player.getInventory().getHelmet().getType() == Material.CHAINMAIL_HELMET)
    //&& (player.getInventory().getChestplate().getType() == Material.CHAINMAIL_CHESTPLATE)
    //&& (player.getInventory().getLeggings().getType() == Material.CHAINMAIL_LEGGINGS)
    //&& (player.getInventory().getBoots().getType() == Material.CHAINMAIL_BOOTS)) {
    //e.setDamage(e.getDamage() / 2);
    //}

    //if (player.getInventory().getHelmet().getType() == Material.IRON_HELMET) {
    //e.setDamage(e.getDamage() * ((int) 1.1));
    //}

    //if (player.getInventory().getChestplate().getType() == Material.IRON_CHESTPLATE) {
    // e.setDamage(e.getDamage() * ((int) 1.1));
    //}

    //if (player.getInventory().getLeggings().getType() == Material.IRON_LEGGINGS) {
    //e.setDamage(e.getDamage() * ((int) 1.1));
    //}

    //if (player.getInventory().getHelmet().getType() == Material.GOLD_HELMET) {
    //e.setDamage(e.getDamage() / ((int) 1.1));
    //}

    //if (player.getInventory().getChestplate().getType() == Material.GOLD_CHESTPLATE) {
    //e.setDamage(e.getDamage() / ((int) 1.1));
    //}

    //if (player.getInventory().getLeggings().getType() == Material.GOLD_LEGGINGS) {
    //e.setDamage(e.getDamage() / ((int) 1.1));
    //}
    }

    PS: The error isn't with 'e.getEntity() instanceof Player', because I have other codes inside this and works perfectly.
     
  2. Offline

    Njol

    The damage isn't changed because ((int) 1.1) is rounded to 1.
    you actually want this:
    Code:java
    1. e.setDamage((int) (e.getDamage()/1.1));
    But this won't work either, since it will also get rounded.
    I suggest to create a new variable at the beginning of onEntityDamage:
    Code:java
    1. double damage = e.getDamage();
    which you then change like
    Code:java
    1. if (player.getInventory().getLeggings().getType() == Material.IRON_LEGGINGS)
    2. damage = damage/1.1
    for every armour piece, and at the end of onEntityDamage you set the event's damage:
    Code:java
    1. e.setDamage((int)Math.round(damage));

    There are further ways of improving the code, but this should be enough for the moment.
     
  3. Offline

    BloodShura

    Oh, thank you very much! :)

    I'll try it. :D
     
Thread Status:
Not open for further replies.

Share This Page