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

This commit is contained in:
Mike Primm 2011-08-13 05:35:07 +08:00 committed by mikeprimm
parent d008548306
commit 17c2072b82

View File

@ -2,6 +2,7 @@ package org.dynmap;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.Material; import org.bukkit.Material;
public class Armor { public class Armor {
@ -10,27 +11,34 @@ public class Armor {
* We rely on getArmorContents() to return 4 armor pieces in the order * We rely on getArmorContents() to return 4 armor pieces in the order
* of: boots, pants, chest, helmet * of: boots, pants, chest, helmet
*/ */
private static final double armorPoints[] = {1.5, 3.0, 4.0, 1.5}; private static final int armorPoints[] = {3, 6, 8, 3};
public static final int getArmorPoints(Player player) { public static final int getArmorPoints(Player player) {
int currentDurability = 0; int currentDurability = 0;
int baseDurability = 0; int baseDurability = 0;
double baseArmorPoints = 0; int baseArmorPoints = 0;
ItemStack inventory[] = player.getInventory().getArmorContents(); ItemStack[] itm = new ItemStack[4];
for(int i=0;i<inventory.length;i++) { PlayerInventory inv = player.getInventory();
if(inventory[i] == null) itm[0] = inv.getBoots();
continue; itm[1]= inv.getLeggings();
Material m = inventory[i].getType(); itm[2] = inv.getChestplate();
if(m == null) itm[3] = inv.getHelmet();
continue; for(int i = 0; i < 4; i++) {
final short maxDurability = m.getMaxDurability(); if(itm[i] == null) continue;
if(maxDurability < 0) int dur = itm[i].getDurability();
continue; int max = itm[i].getType().getMaxDurability();
final short durability = inventory[i].getDurability(); if(max <= 0) continue;
baseDurability += maxDurability; if(i == 2)
currentDurability += maxDurability - durability; max = max + 1; /* Always 1 too low for chestplate */
else
max = max - 3; /* Always 3 too high, versus how client calculates it */
baseDurability += max;
currentDurability += max - dur;
baseArmorPoints += armorPoints[i]; baseArmorPoints += armorPoints[i];
} }
return (int)Math.round(2*baseArmorPoints*currentDurability/baseDurability); int ap = 0;
if(baseDurability > 0)
ap = ((baseArmorPoints - 1) * currentDurability) / baseDurability + 1;
return ap;
} }
} }