package org.dynmap;

import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.Material;

public class Armor {
    /**
     * http://www.minecraftwiki.net/wiki/Item_Durability#Armor_durability
     * We rely on getArmorContents() to return 4 armor pieces in the order
     * of: boots, pants, chest, helmet
     */
    private static final double armorPoints[] = {1.5, 3.0, 4.0, 1.5};

    public static final int getArmorPoints(Player player) {
        int currentDurability = 0;
        int baseDurability = 0;
        double baseArmorPoints = 0;
        ItemStack inventory[] = player.getInventory().getArmorContents();
        for(int i=0;i<inventory.length;i++) {
            if(inventory[i] == null)
                continue;
            Material m = inventory[i].getType();
            if(m == null)
                continue;
            final short maxDurability = m.getMaxDurability();
            if(maxDurability < 0)
                continue;
            final short durability = inventory[i].getDurability();
            baseDurability += maxDurability;
            currentDurability += maxDurability - durability;
            baseArmorPoints += armorPoints[i];
        }
        return (int)Math.round(2*baseArmorPoints*currentDurability/baseDurability);
    }
}

@@ -86,16 +86,61 @@
 mStack;%0A
+import org.bukkit.inventory.PlayerInventory;%0A
 import o
@@ -410,22 +410,19 @@
 c final 
-double
+int
  armorPo
@@ -435,26 +435,18 @@
  = %7B
-1.5, 3.0, 4.0, 1.5
+3, 6, 8, 3
 %7D;%0A%0A
@@ -584,14 +584,11 @@
     
-double
+int
  bas
@@ -622,26 +622,66 @@
 temStack
- i
+%5B%5D itm = new ItemStack%5B4%5D;%0A        PlayerI
 nventory
 %5B%5D = pla
@@ -672,18 +672,20 @@
 nventory
-%5B%5D
+ inv
  = playe
@@ -704,280 +704,300 @@
 ry()
-.getArmorContents();%0A        for(int i=0;i%3Cinventory.length;i++) %7B%0A            if(inventory%5Bi%5D == null)%0A                continue;%0A            Material m = inventory%5Bi%5D.getType();%0A            if(m == null)%0A                continue;%0A            final short maxDurability = m
+;%0A        itm%5B0%5D = inv.getBoots();%0A        itm%5B1%5D= inv.getLeggings();%0A        itm%5B2%5D = inv.getChestplate();%0A        itm%5B3%5D = inv.getHelmet();%0A        for(int i = 0; i %3C 4; i++) %7B%0A    %09%09if(itm%5Bi%5D == null) continue;%0A        %09int dur = itm%5Bi%5D.getDurability();%0A        %09int max = itm%5Bi%5D.getType()
 .get
@@ -1025,139 +1025,216 @@
     
-    
+%09
 if(max
-Durability
  %3C
+=
  0)
-%0A                continue;%0A            final short durability = inventory%5Bi%5D.getDurability();%0A    
+ continue;%0A        %09if(i == 2)%0A    %09%09%09max = max + 1;%09/* Always 1 too low for chestplate */%0A        %09else%0A        %09%09max = max - 3;%09/* Always 3 too high, versus how client calculates it */%0A
         
 base
@@ -1225,24 +1225,25 @@
  */%0A        
+%09
 baseDurabili
@@ -1251,26 +1251,16 @@
 y += max
-Durability
 ;%0A      
@@ -1261,20 +1261,17 @@
         
-    
+%09
 currentD
@@ -1290,31 +1290,14 @@
  max
-Durability - durability
+ - dur
 ;%0A  
@@ -1302,20 +1302,17 @@
         
-    
+%09
 baseArmo
@@ -1360,33 +1360,64 @@
     
-return (int)Math.round(2*
+int ap = 0;%0A        if(baseDurability %3E 0)%0A    %09%09ap = ((
 base
@@ -1427,17 +1427,24 @@
 orPoints
-*
+ - 1) * 
 currentD
@@ -1452,17 +1452,20 @@
 rability
-/
+) / 
 baseDura
@@ -1470,17 +1470,39 @@
 rability
-)
+ + 1;%0A        return ap
 ;%0A    %7D%0A

Fix armor calculation - bring in line with funky method used in MC UI