Solved Get armor/defense points from a player?

Discussion in 'Spigot Plugin Development' started by danablend, Jun 8, 2016.

  1. I want to get the armor/defense points from a player, just like health but i just want it so i can get the armor points instead (the ones displayed next to health points when wearing armor) Is it possible? I need it to get a proper lightning strike formular going for a lightning strike damage effect (I don't want to use the normal lightning strike because it actually damages everything nearby)
     
  2. As far as I know, there isn't a method in Spigot that can return the armor points a player has. What you'll probably have to do is use getEquipment#getArmorContents. That will return a list you can loop through to manually find the player's armor value based on the items in the list.
     
  3. MiniDigger

    Supporter

    I am afraid you need to calculate it by yourself. you also could take a look on the nms or nmc to find out how mojang calculates the points. I am sure that there are wiki pages on that too, just search for the armor values.
     
  4. Ok thanks, I made a decent formular myself for the lightning anyways and got a method to get a double number of how much armor % they have. If someone in the future needs help this is what I use:
    The method for getting % of reduced damage:
    Code (Text):
        public static double getDamageReduced(Player player) {
            PlayerInventory inv = player.getInventory();
            ItemStack helmet = inv.getHelmet();
            ItemStack chest = inv.getChestplate();
            ItemStack boots = inv.getBoots();
            ItemStack pants = inv.getLeggings();
            double red = 0.0;
            //
            if(helmet != null) {
                if (helmet.getType() == Material.LEATHER_HELMET) red = red + 0.04;
                else if (helmet.getType() == Material.GOLD_HELMET) red = red + 0.08;
                else if (helmet.getType() == Material.CHAINMAIL_HELMET) red = red + 0.08;
                else if (helmet.getType() == Material.IRON_HELMET) red = red + 0.08;
                else if (helmet.getType() == Material.DIAMOND_HELMET) red = red + 0.12;
            }
            //
            if(chest != null) {
                if (chest.getType() == Material.LEATHER_CHESTPLATE)    red = red + 0.12;
                else if (chest.getType() == Material.GOLD_CHESTPLATE)red = red + 0.20;
                else if (chest.getType() == Material.CHAINMAIL_CHESTPLATE) red = red + 0.20;
                else if (chest.getType() == Material.IRON_CHESTPLATE) red = red + 0.24;
                else if (chest.getType() == Material.DIAMOND_CHESTPLATE) red = red + 0.32;
            }
            //
            if(pants != null) {
                if (pants.getType() == Material.LEATHER_LEGGINGS) red = red + 0.08;
                else if (pants.getType() == Material.GOLD_LEGGINGS)    red = red + 0.12;
                else if (pants.getType() == Material.CHAINMAIL_LEGGINGS) red = red + 0.16;
                else if (pants.getType() == Material.IRON_LEGGINGS)    red = red + 0.20;
                else if (pants.getType() == Material.DIAMOND_LEGGINGS) red = red + 0.24;
            }
            //
            if(boots != null) {
                if (boots.getType() == Material.LEATHER_BOOTS) red = red + 0.04;
                else if (boots.getType() == Material.GOLD_BOOTS) red = red + 0.04;
                else if (boots.getType() == Material.CHAINMAIL_BOOTS) red = red + 0.04;
                else if (boots.getType() == Material.IRON_BOOTS) red = red + 0.08;
                else if (boots.getType() == Material.DIAMOND_BOOTS)    red = red + 0.12;
            }
            //
            return red;
        }
    And this is the formular I use for lightning:
    Code (Text):
        public static void lightningStrikeWithArmor(LivingEntity entity, double damage) {
            Player player = null;
            if(entity instanceof Player) {
                player = (Player) entity;
            }
           
            if(player == null) {
                entity.damage(damage / 2);
            } else {
                double decimalReduced = getDamageReduced(player);
                damage = (damage - (decimalReduced * damage) * 0.5) / 1.5;
                player.getWorld().strikeLightningEffect(player.getLocation());
                player.damage(damage);
            }
        }
     
  5. This has a huge flaw, Enchantments and the new NBT tag can add armor points to armor. If you are only using this to calculate the amount of damage that you would take while wearing armor the Entity Damage Event has this already inside of it, getDamage(EntityDamageEvent.DamageModifier type)
    and
    ARMOR
    This represents the damage reduction caused by wearing armor.
     
  6. I know this is an older topic but maybe it helps someone out!

    Here is an easy way of getting armorpoints:
    Code (Text):
    double armor = p.getAttribute(Attribute.GENERIC_ARMOR).getValue();
    p is your player ;)
     
    • Like Like x 6