Solved Apply damage with respect to worn armor?

Discussion in 'Plugin Development' started by CubieX, Nov 19, 2012.

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

    CubieX

    I'm trying to make a player take an amount of damage using the "player.damage(int damage)" method.
    But it seems like this method applies flat damage without taking the worn armor into account.

    Which method do I need, to apply damage that is then beeing reduced according to the armor points the player has?
    Or do I have to calculate it myself by evaluating the armor points first?
     
  2. Offline

    fireblast709

    I don't think that there is a method that takes armor in consideration. So yes, calculating it yourself would probably be the only option left
     
  3. Offline

    CubieX

    I've done it manually now. Thanks.
     
  4. may I see teh code you used, because I never seen a way to proper calculate it (armor enchants and that al kind) whitout using a damage source
     
  5. Offline

    CubieX

    Erm. I found a complete method here with a table of damage reduction factors for all armor parts and types. That's what I am using now.
    But this does not include armor enchantments.

    Here is the Wiki article on how to calculate damage reduction for enchanted armor.
    The "Enchantment Protection Factor" is applied to the damage left after the armor's basic effect. So basicly it's only an additional factor.
    But the plugin I am working on does actually not need this.
     
  6. Offline

    one4me

    I posted this a while back, it uses reflection to calculate the new health. It is designed for Players and some types of attack ignore armor, so you would have to check for those.
    Code:
    public int absoluteHealth(org.bukkit.entity.Player player, int damage, boolean ignoreArmor) {
      int health = player.getHealth();
      org.bukkit.craftbukkit.entity.CraftPlayer cp = (org.bukkit.craftbukkit.entity.CraftPlayer)player;
      net.minecraft.server.EntityPlayer ep = cp.getHandle();
      if((ep.noDamageTicks > ep.maxNoDamageTicks / 2.0F) && (damage <= ep.lastDamage)) {
        return health;
      }
      int aS = 0;
      boolean invulnerable = false;
      try {
        java.lang.reflect.Field faS = net.minecraft.server.EntityLiving.class.getDeclaredField("aS");
        java.lang.reflect.Field fI = net.minecraft.server.EntityLiving.class.getDeclaredField("invulnerable");
        faS.setAccessible(true);
        fI.setAccessible(true);
        if(faS != null) {
          aS = faS.getInt(ep);
        }
        invulnerable = fI.getBoolean(ep);
      }
      catch(Exception e) {
        e.printStackTrace();
      }
      if(invulnerable) {
        return health;
      }
      if (!ignoreArmor) {
        int j = 25 - ep.aU();
        int k = damage * j + aS;
        damage = k / 25;
        aS = k % 25;
      }
      if(ep.hasEffect(net.minecraft.server.MobEffectList.RESISTANCE)) {
        int j = (ep.getEffect(net.minecraft.server.MobEffectList.RESISTANCE).getAmplifier() + 1) * 5;
        int k = 25 - j;
        int l = damage * k + aS;
        damage = l / 25;
      }
      return health -= damage;
    }
    
    I haven't checked if the variables have been changed, so you may have to update it. Also, if anyone knows a way to go from DamageCause to DamageSource, calculating the damage would be a lot easier.
     
  7. Offline

    CubieX

    This would be overkill for my requirements.
    But it may be useful for others, who need a more exact calculation.
     
Thread Status:
Not open for further replies.

Share This Page