Fetch the damage the player auctally received

Discussion in 'Resources' started by libraryaddict, Sep 23, 2012.

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

    libraryaddict

    You wanted to get the damage a player auctally received right?

    This will do the trick..

    Code:
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
     
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.inventory.ItemStack;
     
    public class DamageApi {
     
      Random random = new Random();
      boolean randomness = true;
     
      private int returnChance(int aStart, int aEnd) {
        if (aStart > aEnd) {
          aEnd = aStart;
        }
        long range = (long) aEnd - (long) aStart + 1;
        long fraction = (long) (range * random.nextDouble());
        int randomNumber = (int) (fraction + aStart);
        return randomNumber;
      }
     
      public void toggleRandom(boolean toggle) {
        randomness = toggle;
      }
     
      public int getDamage(ItemStack helmet, ItemStack chestplate,
          ItemStack leggings, ItemStack boots, Integer dmg, DamageCause cause, boolean blocking) {
        ItemStack[] items = { helmet, chestplate, leggings, boots };
        int[] armReduce = getArmorReduce(items, cause);
        int[] encReduce = getEnchantmentsReducement(items, cause);
        double armour = 0;
        double damage = dmg;
        double enc = 0;
        for (int in : armReduce)
          armour = armour + in;
        for (int in : encReduce)
          enc = enc + in;
        if (enc > 25)
          enc = 25;
        damage = damage - (damage * armour / 25);
        if (randomness == true)
          enc = Math.ceil(returnChance((int) ((enc) / 2) * 10, (int) (enc) * 10));
        if (enc > 20)
          enc = 20;
        enc = enc * 4;
        damage = damage - (damage * enc / 100);
        if (blocking == true)
          damage = damage/2;
        return (int) Math.round(damage);
      }
     
      public int getDamage(Player player, Integer damage, DamageCause cause) {
        return getDamage(player.getInventory().getHelmet(), player.getInventory()
            .getChestplate(), player.getInventory().getLeggings(), player
            .getInventory().getBoots(), damage, cause, player.isBlocking());
      }
     
      private int[] getArmorReduce(ItemStack[] item, DamageCause dmg) {
        int[] damage = { 0, 0, 0, 0 };
        if (dmg.equals(DamageCause.ENTITY_ATTACK) || dmg.equals(DamageCause.FIRE)
            || dmg.equals(DamageCause.ENTITY_EXPLOSION)
            || dmg.equals(DamageCause.BLOCK_EXPLOSION)
            || dmg.equals(DamageCause.CONTACT) || dmg.equals(DamageCause.LAVA)
            || dmg.equals(DamageCause.PROJECTILE)) {
          int cur = 0;
          for (ItemStack stack : item) {
            if (stack != null)
              damage[cur] = getArmorValue(stack);
            cur = cur + 1;
          }
        }
        return damage;
      }
     
      private static int[] getEnchantmentsReducement(ItemStack[] item,
          DamageCause dmg) {
        int[] damage = { 0, 0, 0, 0 };
        int cur = 0;
        for (ItemStack stack : item) {
          if (stack != null) {
            Map<Enchantment, Integer> enchants = new HashMap<Enchantment, Integer>(
                stack.getEnchantments());
            for (Enchantment enchant : enchants.keySet()) {
              if ((dmg.equals(DamageCause.BLOCK_EXPLOSION) || dmg
                  .equals(DamageCause.ENTITY_EXPLOSION)
                  && enchant.equals(Enchantment.PROTECTION_EXPLOSIONS)))
                damage[cur] = damage[cur]
                    + (int) Math.floor((6 + enchants.get(enchant)
                        * enchants.get(enchant)) / 2);
              if (dmg.equals(DamageCause.FALL)
                  && enchant.equals(Enchantment.PROTECTION_FALL))
                damage[cur] = damage[cur] + enchants.get(enchant) * 2;
              if (dmg.equals(DamageCause.PROJECTILE)
                  && enchant.equals(Enchantment.PROTECTION_PROJECTILE))
                damage[cur] = damage[cur]
                    + (int) Math.floor((6 + enchants.get(enchant)
                        * enchants.get(enchant)) / 2);
              if ((dmg.equals(DamageCause.LAVA)
                  || dmg.equals(DamageCause.LIGHTNING)
                  || dmg.equals(DamageCause.FIRE) || dmg.equals(DamageCause.LAVA))
                  && enchant.equals(Enchantment.PROTECTION_FIRE))
                damage[cur] = damage[cur]
                    + (int) Math.floor((6 + enchants.get(enchant)
                        * enchants.get(enchant)) / 2);
              if (enchant.equals(Enchantment.PROTECTION_ENVIRONMENTAL))
                damage[cur] = damage[cur]
                    + (int) Math.floor((6 + enchants.get(enchant)
                        * enchants.get(enchant)) / 2);
            }
          }
        }
        return damage;
      }
     
      private int getArmorValue(ItemStack armor) {
        Material mat = armor.getType();
        if (mat == Material.LEATHER_HELMET || mat == Material.LEATHER_BOOTS
            || mat == Material.GOLD_BOOTS || mat == Material.CHAINMAIL_BOOTS)
          return 1;
        if (mat == Material.LEATHER_LEGGINGS || mat == Material.GOLD_HELMET
            || mat == Material.CHAINMAIL_HELMET || mat == Material.IRON_HELMET
            || mat == Material.IRON_BOOTS)
          return 2;
        if (mat == Material.LEATHER_CHESTPLATE || mat == Material.GOLD_LEGGINGS
            || mat == Material.DIAMOND_BOOTS || mat == Material.DIAMOND_HELMET)
          return 3;
        if (mat == Material.CHAINMAIL_LEGGINGS)
          return 4;
        if (mat == Material.GOLD_CHESTPLATE || mat == Material.CHAINMAIL_CHESTPLATE
            || mat == Material.IRON_LEGGINGS)
          return 5;
        if (mat == Material.IRON_LEGGINGS || mat == Material.DIAMOND_LEGGINGS)
          return 6;
        if (mat == Material.DIAMOND_CHESTPLATE)
          return 8;
        return 0;
      }
     
    }
    
    Code:
    public int getDamage(ItemStack helmet, ItemStack chestplate,
          ItemStack leggings, ItemStack boots, Integer dmg, DamageCause cause, boolean blocking)
     
    public int getDamage(Player player, Integer damage, DamageCause cause)
     
    public void toggleRandom(boolean toggle)
    The 3 things you only have to worry about.

    The 2nd method in this just calls on the main method, Easier to use ^^

    toggleRandom toggles if you get a random calculation applied or not.
    This is true by default.

    This is part of the damage calculating system to apply a randomness to the damage.
    As I can't get this I made my own.
    If you set it to false then it will have the damage returned at max damage they could have received.

    Best to leave it on if you plan to use the API to hurt players realistically.

    This is public domain
     
    _Robert likes this.
  2. Offline

    libraryaddict

    For one I do not know how to use that.
    Secondly I have never suffered problems with this.

    If you would like to offer to fix this, By all means, Feel free.
     
    Minken likes this.
  3. Offline

    gregthegeek

    Simply change the Integer return type to int.
     
    Minken likes this.
Thread Status:
Not open for further replies.

Share This Page