diff --git a/src/main/java/com/gmail/nossr50/skills/repair/Repair.java b/src/main/java/com/gmail/nossr50/skills/repair/Repair.java index 7af95af10..2ae9daf80 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/Repair.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/Repair.java @@ -2,8 +2,6 @@ package com.gmail.nossr50.skills.repair; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.PlayerInventory; -import org.bukkit.material.MaterialData; import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.config.Config; @@ -53,65 +51,6 @@ public class Repair { return ""; } - /** - * Search the inventory for an item and return the index. - * - * @param inventory PlayerInventory to scan - * @param itemId Item id to look for - * @return index location where the item was found, or -1 if not found - */ - protected static int findInInventory(PlayerInventory inventory, int itemId) { - int location = inventory.first(itemId); - - // VALIDATE - if (inventory.getItem(location).getTypeId() == itemId) { - return location; - } - - return -1; - } - - /** - * Search the inventory for an item and return the index. - * - * @param inventory PlayerInventory to scan - * @param itemId Item id to look for - * @param metadata Metadata to look for - * @return index location where the item was found, or -1 if not found - */ - protected static int findInInventory(PlayerInventory inventory, int itemId, byte metadata) { - int location = -1; - - ItemStack[] contents = inventory.getContents(); - - for (int i = 0; i < contents.length; i++) { - ItemStack item = contents[i]; - - if (item == null) { - continue; - } - - if (item.getData().equals(new MaterialData(itemId, metadata))) { - return i; - } - } - - return location; - } - - /** - * Decrease the amount of items in this slot by one - * - * @param inventory PlayerInventory to work in - * @param index Item index to decrement - */ - protected static void removeOneFrom(PlayerInventory inventory, int index) { - ItemStack item = inventory.getItem(index).clone(); - item.setAmount(1); - - inventory.removeItem(item); - } - protected static Material getSalvagedItem(ItemStack inHand) { if (ItemUtils.isDiamondTool(inHand) || ItemUtils.isDiamondArmor(inHand)) { return Material.DIAMOND; diff --git a/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java b/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java index b01950fb4..0a8afb649 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java @@ -10,6 +10,7 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; +import org.bukkit.material.MaterialData; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; @@ -79,16 +80,14 @@ public class RepairManager extends SkillManager { int repairMaterialId = repairable.getRepairMaterialId(); byte repairMaterialMetadata = repairable.getRepairMaterialMetadata(); + ItemStack toRemove = new MaterialData(repairMaterialId, repairMaterialMetadata).toItemStack(1); // Check if they have the proper material to repair with - if (!inventory.contains(repairMaterialId)) { + if (!inventory.containsAtLeast(toRemove, 1)) { String message = LocaleLoader.getString("Skills.NeedMore", StringUtils.getPrettyItemString(repairMaterialId)); - if (repairMaterialMetadata != (byte) -1) { - // TODO: Do something nicer than append the metadata as a :# ? - if (Repair.findInInventory(inventory, repairMaterialId, repairMaterialMetadata) == -1) { - message += ":" + repairMaterialMetadata; - } + if (repairMaterialMetadata > 0) { + message += ":" + repairMaterialMetadata; } player.sendMessage(message); @@ -117,24 +116,8 @@ public class RepairManager extends SkillManager { int baseRepairAmount = repairable.getBaseRepairDurability(); // Did they send me daughters? short newDurability = repairCalculate(startDurability, baseRepairAmount); // When I asked for sons? - // We're going to hold onto our repair item location - int repairItemLocation; - if (repairable.getRepairMaterialMetadata() == (byte) -1) { - repairItemLocation = Repair.findInInventory(inventory, repairMaterialId); - } - else { - // Special case for when the repairable has metadata that must be addressed - repairItemLocation = Repair.findInInventory(inventory, repairMaterialId, repairMaterialMetadata); - } - - // This should never happen, but if it does we need to complain loudly about it. - if (repairItemLocation == -1) { - player.sendMessage(LocaleLoader.getString("Repair.Error")); - return; - } - // Call event - McMMOPlayerRepairCheckEvent event = new McMMOPlayerRepairCheckEvent(player, (short) (startDurability - newDurability), inventory.getItem(repairItemLocation), item); + McMMOPlayerRepairCheckEvent event = new McMMOPlayerRepairCheckEvent(player, (short) (startDurability - newDurability), toRemove, item); mcMMO.p.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { @@ -147,7 +130,7 @@ public class RepairManager extends SkillManager { } // Remove the item - Repair.removeOneFrom(inventory, repairItemLocation); + inventory.removeItem(toRemove); // Give out XP like candy applyXpGain((float) ((getPercentageRepaired(startDurability, newDurability, repairable.getMaximumDurability()) * repairable.getXpMultiplier()) * ExperienceConfig.getInstance().getRepairXPBase() * ExperienceConfig.getInstance().getRepairXP(repairable.getRepairMaterialType())));