diff --git a/src/main/java/net/apunch/blacksmith/BlacksmithPlugin.java b/src/main/java/net/apunch/blacksmith/BlacksmithPlugin.java index 5bcbbaa..87d8380 100644 --- a/src/main/java/net/apunch/blacksmith/BlacksmithPlugin.java +++ b/src/main/java/net/apunch/blacksmith/BlacksmithPlugin.java @@ -174,7 +174,7 @@ public class BlacksmithPlugin extends JavaPlugin { hyperPrice = to.getSellPrice(1); } double hyperPricePerDurability = hyperPrice / item.getType().getMaxDurability(); - price += (((Damageable) item).getDamage() * hyperPricePerDurability); + price += getDurability(item) * hyperPricePerDurability; double enchantmentModifier = Setting.ENCHANTMENT_MODIFIER.asDouble(); for (Enchantment enchantment : item2.getEnchantments().keySet()) { @@ -189,9 +189,9 @@ public class BlacksmithPlugin extends JavaPlugin { return price; } else { if (root.keyExists("price-per-durability-point." + item.getType().name().toLowerCase().replace('_', '-'))) { - price += ((Damageable) item).getDamage() * root.getDouble("price-per-durability-point." + item.getType().name().toLowerCase().replace('_', '-')); + price += getDurability(item) * root.getDouble("price-per-durability-point." + item.getType().name().toLowerCase().replace('_', '-')); } else { - price += (((Damageable) item).getDamage() * Setting.PRICE_PER_DURABILITY_POINT.asDouble()); + price += getDurability(item) * Setting.PRICE_PER_DURABILITY_POINT.asDouble(); } } @@ -205,4 +205,16 @@ public class BlacksmithPlugin extends JavaPlugin { } return price; } + + /** + * Gets the durability of the given item + * + * @param itemStack
The item to get the durability of
+ * @returnThe durability of the item
+ */ + static short getDurability(ItemStack itemStack) { + Damageable damageable = (Damageable) itemStack.getItemMeta(); + return (short) (itemStack.getType().getMaxDurability() - damageable.getDamage()); + } + } diff --git a/src/main/java/net/apunch/blacksmith/BlacksmithTrait.java b/src/main/java/net/apunch/blacksmith/BlacksmithTrait.java index ba1ec5d..27fd2dc 100644 --- a/src/main/java/net/apunch/blacksmith/BlacksmithTrait.java +++ b/src/main/java/net/apunch/blacksmith/BlacksmithTrait.java @@ -291,19 +291,19 @@ public class BlacksmithTrait extends Trait { } } // Damage the item - short durability = (short) (((Damageable) reforge).getDamage() + ((Damageable) reforge).getDamage() * - random.nextInt(8)); + short reforgeDurability = BlacksmithPlugin.getDurability(reforge); + short durability = (short) (reforgeDurability + reforgeDurability * random.nextInt(8)); short maxDurability = reforge.getType().getMaxDurability(); if (durability <= 0) { durability = (short) (maxDurability / 3); - } else if (((Damageable) reforge).getDamage() + durability > maxDurability) { + } else if (reforgeDurability + durability > maxDurability) { durability = (short) (maxDurability - random.nextInt(maxDurability - 25)); } - ((Damageable) reforge).setDamage(durability); + ((Damageable) reforge.getItemMeta()).setDamage(durability); return false; } - ((Damageable) reforge).setDamage((short) 0); + ((Damageable) reforge.getItemMeta()).setDamage((short) 0); // Add random enchantments