From 77cdd6328d22b0df28374041fea4019e8e5c60d1 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Tue, 19 Nov 2013 01:41:38 -0700 Subject: [PATCH] Make Enchantment merges --- .../src/me/kaZep/Base/PlayerListener.java | 535 ++++++++++++++++-- 1 file changed, 485 insertions(+), 50 deletions(-) diff --git a/BankEconomyMod/src/me/kaZep/Base/PlayerListener.java b/BankEconomyMod/src/me/kaZep/Base/PlayerListener.java index ad82403..c0cb27c 100644 --- a/BankEconomyMod/src/me/kaZep/Base/PlayerListener.java +++ b/BankEconomyMod/src/me/kaZep/Base/PlayerListener.java @@ -12861,41 +12861,71 @@ class updateInventoryTask implements Runnable { final int LEVELS = 22; + // Redundant validation code for verification - - if (anvilInv.getItem(INPUT) == null || anvilInv.getItem(MATERIALS) == null) { - // No valid combo, set XP orb to stack size 1 and remove output. - anvilInv.getItem(LEVELS).setAmount(1); - anvilInv.setItem(OUTPUT, new ItemStack(Material.AIR)); - } - else - { - // Redundant validation code for verification - - - if (!((anvilInv.getItem(INPUT).toString().toUpperCase().contains("HELMET") || anvilInv.getItem(INPUT).toString().toUpperCase().contains("CHESTPLATE") || - anvilInv.getItem(INPUT).toString().toUpperCase().contains("LEGGINGS") || anvilInv.getItem(INPUT).toString().toUpperCase().contains("BOOTS") || - anvilInv.getItem(INPUT).toString().toUpperCase().contains("PICKAXE") || anvilInv.getItem(INPUT).toString().toUpperCase().contains("SPADE") || - anvilInv.getItem(INPUT).toString().toUpperCase().contains("HOE") || anvilInv.getItem(INPUT).toString().toUpperCase().contains("AXE") || - anvilInv.getItem(INPUT).toString().toUpperCase().contains("SWORD") || anvilInv.getItem(INPUT).toString().toUpperCase().contains("FISHING") || - anvilInv.getItem(INPUT).toString().toUpperCase().contentEquals("BOW")) && anvilInv.getItem(INPUT).getDurability() != 0)) { - - sendToInventory(anvilInv.getItem(INPUT), anvilInv.getItem(INPUT).getAmount(), player, player.getInventory()); + if (anvilInv.getItem(INPUT) != null) { + if (!(anvilInv.getItem(INPUT).getType().toString().toUpperCase().contains("HELMET") || anvilInv.getItem(INPUT).getType().toString().toUpperCase().contains("CHESTPLATE") || + anvilInv.getItem(INPUT).getType().toString().toUpperCase().contains("LEGGINGS") || anvilInv.getItem(INPUT).getType().toString().toUpperCase().contains("BOOTS") || + anvilInv.getItem(INPUT).getType().toString().toUpperCase().contains("PICKAXE") || anvilInv.getItem(INPUT).getType().toString().toUpperCase().contains("SPADE") || + anvilInv.getItem(INPUT).getType().toString().toUpperCase().contains("HOE") || anvilInv.getItem(INPUT).getType().toString().toUpperCase().contains("AXE") || + anvilInv.getItem(INPUT).getType().toString().toUpperCase().contains("SWORD") || anvilInv.getItem(INPUT).getType().toString().toUpperCase().contains("FISHING") || + anvilInv.getItem(INPUT).getType().toString().toUpperCase().contentEquals("BOW"))) { + + sendToInventory(INPUT, anvilInv.getItem(INPUT).getAmount(), player); Bukkit.getLogger().info("Invalid input!"); + } else { + Bukkit.getLogger().info("Valid input, " + anvilInv.getItem(INPUT).toString().toUpperCase() + " with durability " + anvilInv.getItem(INPUT).getDurability()); } + } - + if (anvilInv.getItem(MATERIALS) != null) { if (!(anvilInv.getItem(MATERIALS).getType() == Material.LEATHER || anvilInv.getItem(MATERIALS).getType() == Material.IRON_INGOT || anvilInv.getItem(MATERIALS).getType() == Material.GOLD_INGOT || anvilInv.getItem(MATERIALS).getType() == Material.IRON_BLOCK || anvilInv.getItem(MATERIALS).getType() == Material.DIAMOND_BLOCK || anvilInv.getItem(MATERIALS).getType() == Material.DIAMOND || - anvilInv.getItem(MATERIALS).getType() == Material.WOOD || anvilInv.getItem(MATERIALS).getType() == Material.COBBLESTONE || + anvilInv.getItem(MATERIALS).getType() == Material.WOOD || anvilInv.getItem(MATERIALS).getType() == Material.COBBLESTONE || + anvilInv.getItem(MATERIALS).getType() == Material.LOG || anvilInv.getItem(MATERIALS).getType() == Material.STONE || anvilInv.getItem(MATERIALS).getType() == Material.STRING)) { - - sendToInventory(anvilInv.getItem(MATERIALS), anvilInv.getItem(MATERIALS).getAmount(), player, player.getInventory()); + + sendToInventory(MATERIALS, anvilInv.getItem(MATERIALS).getAmount(), player); Bukkit.getLogger().info("Invalid materials!"); + } else { + Bukkit.getLogger().info("Valid materials, " + anvilInv.getItem(MATERIALS).toString().toUpperCase() + " with durability " + anvilInv.getItem(MATERIALS).getDurability()); + } + } + + if (anvilInv.getItem(MAGIC) != null) { + if (!(anvilInv.getItem(MAGIC).getType() == Material.ENCHANTED_BOOK)) { + + sendToInventory(MAGIC, anvilInv.getItem(MAGIC).getAmount(), player); + + Bukkit.getLogger().info("Invalid magic!"); + } else { + Bukkit.getLogger().info("Valid magic, " + anvilInv.getItem(MAGIC).toString().toUpperCase()); } - + } + + if ((anvilInv.getItem(INPUT) == null) || // No input, or material/magic both empty, or all three filled, or input and materials filled but item full + (anvilInv.getItem(MAGIC) == null && anvilInv.getItem(MATERIALS) == null) || + (anvilInv.getItem(MAGIC) != null && anvilInv.getItem(MATERIALS) != null && anvilInv.getItem(INPUT) != null) || + (anvilInv.getItem(INPUT) != null && anvilInv.getItem(MATERIALS) != null && anvilInv.getItem(INPUT).getDurability() == 0)) { + // No valid combo, set XP orb to stack size 1 and remove output. + anvilInv.setItem(OUTPUT, new ItemStack(Material.AIR)); + ItemStack orbs = new ItemStack(Material.SLIME_BALL); + ItemMeta temp_meta = orbs.getItemMeta(); + temp_meta.setDisplayName(ChatColor.YELLOW + "Experience Cost"); + List temp_meta_lore = new ArrayList(); + temp_meta_lore.add(ChatColor.ITALIC + "Experience Cost of Enchanting."); + temp_meta.setLore(temp_meta_lore); + orbs.setItemMeta(temp_meta); + anvilInv.setItem(LEVELS, orbs); + + // TRY EVERYTHING + player.getInventory().setContents(player.getInventory().getContents()); + anvilInv.setContents(anvilInv.getContents()); + player.updateInventory(); + } else if (anvilInv.getItem(INPUT) != null && anvilInv.getItem(MATERIALS) != null && anvilInv.getItem(INPUT).getDurability() != 0) { + // Both repair slots are populated, and the item is damaged. // Verify the right material is combined with the source item. boolean validCombo = false; double multiplier = 0; @@ -12908,6 +12938,14 @@ class updateInventoryTask implements Runnable { multiplier = 0.1; } + if (anvilInv.getItem(MATERIALS).getType() == Material.LOG && (anvilInv.getItem(INPUT).getType() == Material.WOOD_AXE || + anvilInv.getItem(INPUT).getType() == Material.WOOD_SWORD || anvilInv.getItem(INPUT).getType() == Material.WOOD_HOE || + anvilInv.getItem(INPUT).getType() == Material.WOOD_PICKAXE || anvilInv.getItem(INPUT).getType() == Material.WOOD_SPADE)) { + + validCombo = true; + multiplier = 0; + } + if (anvilInv.getItem(MATERIALS).getType() == Material.STRING && (anvilInv.getItem(INPUT).getType() == Material.FISHING_ROD || anvilInv.getItem(INPUT).getType() == Material.BOW)) { @@ -12923,6 +12961,14 @@ class updateInventoryTask implements Runnable { multiplier = 0.2; } + if (anvilInv.getItem(MATERIALS).getType() == Material.STONE && (anvilInv.getItem(INPUT).getType() == Material.STONE_AXE || + anvilInv.getItem(INPUT).getType() == Material.STONE_SWORD || anvilInv.getItem(INPUT).getType() == Material.STONE_HOE || + anvilInv.getItem(INPUT).getType() == Material.STONE_PICKAXE || anvilInv.getItem(INPUT).getType() == Material.STONE_SPADE)) { + + validCombo = true; + multiplier = 0; + } + if (anvilInv.getItem(MATERIALS).getType() == Material.LEATHER && (anvilInv.getItem(INPUT).getType() == Material.LEATHER_BOOTS || anvilInv.getItem(INPUT).getType() == Material.LEATHER_CHESTPLATE || anvilInv.getItem(INPUT).getType() == Material.LEATHER_HELMET || anvilInv.getItem(INPUT).getType() == Material.LEATHER_LEGGINGS)) { @@ -12994,15 +13040,15 @@ class updateInventoryTask implements Runnable { // in the player's inventory, or if that fails, drop it on the ground. // Limit to 5 just in case math doesn't work. int maxItemsNeeded = Math.min(5, 1 + (5 * anvilInv.getItem(INPUT).getDurability() / anvilInv.getItem(INPUT).getType().getMaxDurability())); - + if (anvilInv.getItem(MATERIALS).getAmount() > maxItemsNeeded) { // Bukkit.getLogger().info("Materials exceed " + maxItemsNeeded + " stack. Dropping " + (anvilInv.getItem(MATERIALS).getAmount() - maxItemsNeeded) + " of " + anvilInv.getItem(MATERIALS).getType()); - - sendToInventory(anvilInv.getItem(MATERIALS), anvilInv.getItem(MATERIALS).getAmount() - maxItemsNeeded, player, player.getInventory()); + + sendToInventory(MATERIALS, anvilInv.getItem(MATERIALS).getAmount() - maxItemsNeeded, player); // anvilInv.getItem(MATERIALS).setAmount(maxItemsNeeded); - } - + } + int cost = anvilInv.getContents()[MATERIALS].getAmount() * 2; int bonus_cost = 0; ItemStack item = anvilInv.getContents()[INPUT]; @@ -13027,7 +13073,7 @@ class updateInventoryTask implements Runnable { Bukkit.getLogger().info("Cost: " + cost); ItemStack orbs = new ItemStack(Material.SLIME_BALL); - + ItemMeta temp_meta = orbs.getItemMeta(); temp_meta.setDisplayName(ChatColor.YELLOW + "Experience Cost"); List temp_meta_lore = new ArrayList(); @@ -13043,49 +13089,438 @@ class updateInventoryTask implements Runnable { temp_meta_lore.add(ChatColor.GREEN + "Completing the operation will"); temp_meta_lore.add(ChatColor.GREEN + "bring you to " + (player.getLevel() - cost) + " levels."); } - + temp_meta.setLore(temp_meta_lore); orbs.setItemMeta(temp_meta); - + orbs.setAmount(cost); anvilInv.setItem(LEVELS, orbs); // anvilInv.getContents()[OUTPUT].setType(item.getType()); // anvilInv.getContents()[OUTPUT].setData(item.getData()); - + anvilInv.setItem(OUTPUT, item.clone()); anvilInv.getContents()[OUTPUT].setDurability((short) (item.getDurability() - item.getType().getMaxDurability() * 0.2 * anvilInv.getContents()[MATERIALS].getAmount())); } - - + + // TRY EVERYTHING player.getInventory().setContents(player.getInventory().getContents()); anvilInv.setContents(anvilInv.getContents()); player.updateInventory(); + } else if (anvilInv.getItem(INPUT) != null && anvilInv.getItem(MAGIC) != null) { + // Both Magic slots are populated. + // Get the list of enchantments from both items. + Map itemEnchantments = anvilInv.getItem(INPUT).getEnchantments(); + Map bookEnchantments = new java.util.HashMap(); + + // Get enchanted book "enchantment" and enchantments. + bookEnchantments.putAll(anvilInv.getItem(MAGIC).getEnchantments()); + bookEnchantments.putAll(((EnchantmentStorageMeta)(anvilInv.getItem(MAGIC).getItemMeta())).getStoredEnchants()); + + // Get the list of bonuses from both items. + Map itemBonuses = new java.util.HashMap(); + Map bookBonuses = new java.util.HashMap(); + + if (anvilInv.getItem(INPUT).getItemMeta().hasLore()) { + for (int i = 0; i < anvilInv.getItem(INPUT).getItemMeta().getLore().size(); i++) { + // Put enchantments together for the input. Also stacks identical enchantments. + Bukkit.getLogger().info("Iterating bonus: " + anvilInv.getItem(INPUT).getItemMeta().getLore().get(i)); + + if (getEnchantmentName(anvilInv.getItem(INPUT).getItemMeta().getLore().get(i)) != null) { + if (itemBonuses.get(getEnchantmentName(anvilInv.getItem(INPUT).getItemMeta().getLore().get(i))) == null) { + + Bukkit.getLogger().info("New bonus: " + getEnchantmentName(anvilInv.getItem(INPUT).getItemMeta().getLore().get(i))); + itemBonuses.put(getEnchantmentName(anvilInv.getItem(INPUT).getItemMeta().getLore().get(i)), getEnchantmentNumb(anvilInv.getItem(INPUT).getItemMeta().getLore().get(i))); + } else { + + Bukkit.getLogger().info("Stacking bonus: " + itemBonuses.get(getEnchantmentName(anvilInv.getItem(INPUT).getItemMeta().getLore().get(i))) + " + " + getEnchantmentNumb(anvilInv.getItem(INPUT).getItemMeta().getLore().get(i))); + itemBonuses.put(getEnchantmentName(anvilInv.getItem(INPUT).getItemMeta().getLore().get(i)), itemBonuses.get(getEnchantmentName(anvilInv.getItem(INPUT).getItemMeta().getLore().get(i))) + getEnchantmentNumb(anvilInv.getItem(INPUT).getItemMeta().getLore().get(i))); + } + } + } + } + + if (anvilInv.getItem(MAGIC).getItemMeta().hasLore()) { + for (int i = 0; i < anvilInv.getItem(MAGIC).getItemMeta().getLore().size(); i++) { + // Put enchantments together for the MAGIC. Also stacks identical enchantments. + Bukkit.getLogger().info("Iterating bonus: " + anvilInv.getItem(MAGIC).getItemMeta().getLore().get(i)); + if (bookBonuses.get(getEnchantmentName(anvilInv.getItem(MAGIC).getItemMeta().getLore().get(i))) == null) { + + Bukkit.getLogger().info("New bonus: " + getEnchantmentName(anvilInv.getItem(MAGIC).getItemMeta().getLore().get(i))); + bookBonuses.put(getEnchantmentName(anvilInv.getItem(MAGIC).getItemMeta().getLore().get(i)), getEnchantmentNumb(anvilInv.getItem(MAGIC).getItemMeta().getLore().get(i))); + } else { + + Bukkit.getLogger().info("Stacking bonus: " + bookBonuses.get(getEnchantmentName(anvilInv.getItem(MAGIC).getItemMeta().getLore().get(i))) + " + " + getEnchantmentNumb(anvilInv.getItem(MAGIC).getItemMeta().getLore().get(i))); + bookBonuses.put(getEnchantmentName(anvilInv.getItem(MAGIC).getItemMeta().getLore().get(i)), bookBonuses.get(getEnchantmentName(anvilInv.getItem(MAGIC).getItemMeta().getLore().get(i))) + getEnchantmentNumb(anvilInv.getItem(MAGIC).getItemMeta().getLore().get(i))); + } + } + } + + // Generate appropriate possible enchantments + Map probableEnchantments = new java.util.HashMap(); + + for (Enchantment e : bookEnchantments.keySet()) { + Bukkit.getLogger().info("Iterating enchantment: " + e); + + if (itemEnchantments.get(e) == null || bookEnchantments.get(e) > itemEnchantments.get(e)) { + // Book enchantment is larger in magnitude. Assign it as a possible outcome. + probableEnchantments.put(e, bookEnchantments.get(e)); + } else if (bookEnchantments.get(e) == itemEnchantments.get(e)) { + // Book enchantment is same in magnitude. Upgrade by one level. + probableEnchantments.put(e, bookEnchantments.get(e) + 1); + } + } + + // Generate appropriate possible bonuses + Map probableBonuses = new java.util.HashMap(); + + for (String e : bookBonuses.keySet()) { + Bukkit.getLogger().info("Iterating bonus: " + e); + + if (itemBonuses.get(e) == null || bookBonuses.get(e) > itemBonuses.get(e)) { + // Book enchantment is larger in magnitude. Assign it as a possible outcome. + probableBonuses.put(e, bookBonuses.get(e)); + } else if (bookBonuses.get(e) == itemBonuses.get(e)) { + // Book enchantment is same in magnitude. Upgrade by half. + probableBonuses.put(e, Math.floor(bookBonuses.get(e) * 1.5)); + } + } + + if (probableEnchantments.size() == 0 && probableBonuses.size() == 0) { + // No possible valid enchantments. Output nothing + } else { + if (probableEnchantments.size() != 0) { + // Randomly select an enchantment to add. + int random = (int)(Math.random() * probableEnchantments.size()); + Bukkit.getLogger().info("Randomized to # " + (random + 1) + " out of " + probableEnchantments.size()); + + int i = 0; + + Enchantment appliedEnchant = null; + int magnitude = 0; + for (Enchantment e : probableEnchantments.keySet()) { + + if (i == random) { + // Rolled this one + appliedEnchant = e; + magnitude = probableEnchantments.get(e); + } + i++; + } + + int cost = 0; + + if (itemEnchantments.get(appliedEnchant) == null) { + // This enchantment doesn't exist. Calculate full cost. + + cost = Math.min(60, magnitude * magnitude); + cost = Math.max(cost, 1); // Make sure it's at least one level + + ItemStack orbs = new ItemStack(Material.SLIME_BALL); + + ItemMeta temp_meta = orbs.getItemMeta(); + temp_meta.setDisplayName(ChatColor.YELLOW + "Experience Cost"); + List temp_meta_lore = new ArrayList(); + temp_meta_lore.add(ChatColor.ITALIC + "This operation costs " + cost + " levels."); + temp_meta_lore.add(ChatColor.ITALIC + "You currently have " + player.getLevel() + " levels."); + + if (cost > player.getLevel()) { + orbs.setType(Material.MAGMA_CREAM); + temp_meta_lore.add(""); + temp_meta_lore.add(ChatColor.RED + "You can't afford this!"); + } else { + temp_meta_lore.add(""); + temp_meta_lore.add(ChatColor.GREEN + "Completing the operation will"); + temp_meta_lore.add(ChatColor.GREEN + "bring you to " + (player.getLevel() - cost) + " levels."); + } + + temp_meta.setLore(temp_meta_lore); + orbs.setItemMeta(temp_meta); + + orbs.setAmount(cost); + anvilInv.setItem(LEVELS, orbs); + anvilInv.setItem(OUTPUT, anvilInv.getItem(INPUT).clone()); + anvilInv.getItem(OUTPUT).addUnsafeEnchantment(appliedEnchant, magnitude); + } else { + // This enchantment exists. Calculate incremental cost. + + cost = Math.min(60, magnitude * magnitude - itemEnchantments.get(appliedEnchant) * itemEnchantments.get(appliedEnchant)); + cost = Math.max(cost, 1); // Make sure it's at least one level + + ItemStack orbs = new ItemStack(Material.SLIME_BALL); + + ItemMeta temp_meta = orbs.getItemMeta(); + temp_meta.setDisplayName(ChatColor.YELLOW + "Experience Cost"); + List temp_meta_lore = new ArrayList(); + temp_meta_lore.add(ChatColor.ITALIC + "This operation costs " + cost + " levels."); + temp_meta_lore.add(ChatColor.ITALIC + "You currently have " + player.getLevel() + " levels."); + + if (cost > player.getLevel()) { + orbs.setType(Material.MAGMA_CREAM); + temp_meta_lore.add(""); + temp_meta_lore.add(ChatColor.RED + "You can't afford this!"); + } else { + temp_meta_lore.add(""); + temp_meta_lore.add(ChatColor.GREEN + "Completing the operation will"); + temp_meta_lore.add(ChatColor.GREEN + "bring you to " + (player.getLevel() - cost) + " levels."); + } + + temp_meta.setLore(temp_meta_lore); + orbs.setItemMeta(temp_meta); + + orbs.setAmount(cost); + anvilInv.setItem(LEVELS, orbs); + + anvilInv.setItem(OUTPUT, anvilInv.getItem(INPUT).clone()); + anvilInv.getItem(OUTPUT).removeEnchantment(appliedEnchant); + anvilInv.getItem(OUTPUT).addUnsafeEnchantment(appliedEnchant, magnitude); + } + } else { + // Can't add enchant, but can add bonus. Prepare output for bonus. + anvilInv.setItem(OUTPUT, anvilInv.getItem(INPUT).clone()); + } + + if (probableBonuses.size() != 0) { + // Randomly select a bonus to add. + int random = (int)(Math.random() * probableBonuses.size()); + Bukkit.getLogger().info("Randomized to # " + (random + 1) + " out of " + probableBonuses.size()); + + int i = 0; + + String appliedEnchant = null; + double magnitude = 0; + for (String e : probableBonuses.keySet()) { + + if (i == random) { + // Rolled this one + appliedEnchant = e; + magnitude = probableBonuses.get(e); + } + i++; + } + + int cost = 0; + + if (itemBonuses.get(appliedEnchant) == null) { + // This bonus doesn't exist. Calculate full cost. + + cost = 30; + + ItemStack orbs = new ItemStack(Material.SLIME_BALL); + + ItemMeta temp_meta = orbs.getItemMeta(); + temp_meta.setDisplayName(ChatColor.YELLOW + "Experience Cost"); + List temp_meta_lore = new ArrayList(); + temp_meta_lore.add(ChatColor.ITALIC + "This operation costs " + cost + " levels."); + temp_meta_lore.add(ChatColor.ITALIC + "You currently have " + player.getLevel() + " levels."); + + if (cost > player.getLevel()) { + orbs.setType(Material.MAGMA_CREAM); + temp_meta_lore.add(""); + temp_meta_lore.add(ChatColor.RED + "You can't afford this!"); + } else { + temp_meta_lore.add(""); + temp_meta_lore.add(ChatColor.GREEN + "Completing the operation will"); + temp_meta_lore.add(ChatColor.GREEN + "bring you to " + (player.getLevel() - cost) + " levels."); + } + + temp_meta.setLore(temp_meta_lore); + orbs.setItemMeta(temp_meta); + + orbs.setAmount(cost); + anvilInv.setItem(LEVELS, orbs); + + // Change entry matching modified lore + List bonusesLore = anvilInv.getItem(OUTPUT).getItemMeta().getLore(); + + for (int i1 = 0; i1 < bonusesLore.size(); i1++) { + String e = bonusesLore.get(i1); + + if (containsEnchantment(e, appliedEnchant)) { + if (appliedEnchant.equalsIgnoreCase("Critical Chance")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Critical Chance"); + } + if (appliedEnchant.equalsIgnoreCase("Armor Penetration")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+" "+ChatColor.BLUE+"Armor Penetration"); + } + if (appliedEnchant.equalsIgnoreCase("Life Steal")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Life Steal"); + } + if (appliedEnchant.equalsIgnoreCase("Attack Speed")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Attack Speed"); + } + if (appliedEnchant.equalsIgnoreCase("Damage")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+" "+ChatColor.BLUE+"Damage"); + } + if (appliedEnchant.equalsIgnoreCase("Health")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+" "+ChatColor.BLUE+"Health"); + } + if (appliedEnchant.equalsIgnoreCase("Damage Reduction")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Damage Reduction"); + } + if (appliedEnchant.equalsIgnoreCase("Durability")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Durability"); + } + if (appliedEnchant.equalsIgnoreCase("Block Chance")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Block Chance"); + } + if (appliedEnchant.equalsIgnoreCase("Speed Boost Chance")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Speed Boost Chance"); + } + } + } + + anvilInv.getItem(OUTPUT).getItemMeta().setLore(bonusesLore); + + } else { + // This enchantment exists. Calculate incremental cost. + + cost = (int)(30 * itemBonuses.get(appliedEnchant) / probableBonuses.get(appliedEnchant)); // Make sure it's at least one level + + ItemStack orbs = new ItemStack(Material.SLIME_BALL); + + ItemMeta temp_meta = orbs.getItemMeta(); + temp_meta.setDisplayName(ChatColor.YELLOW + "Experience Cost"); + List temp_meta_lore = new ArrayList(); + temp_meta_lore.add(ChatColor.ITALIC + "This operation costs " + cost + " levels."); + temp_meta_lore.add(ChatColor.ITALIC + "You currently have " + player.getLevel() + " levels."); + + if (cost > player.getLevel()) { + orbs.setType(Material.MAGMA_CREAM); + temp_meta_lore.add(""); + temp_meta_lore.add(ChatColor.RED + "You can't afford this!"); + } else { + temp_meta_lore.add(""); + temp_meta_lore.add(ChatColor.GREEN + "Completing the operation will"); + temp_meta_lore.add(ChatColor.GREEN + "bring you to " + (player.getLevel() - cost) + " levels."); + } + + temp_meta.setLore(temp_meta_lore); + orbs.setItemMeta(temp_meta); + + orbs.setAmount(cost); + anvilInv.setItem(LEVELS, orbs); + + // Change entry matching modified lore + List bonusesLore = anvilInv.getItem(OUTPUT).getItemMeta().getLore(); + + for (int i1 = 0; i1 < bonusesLore.size(); i1++) { + String e = bonusesLore.get(i1); + + if (containsEnchantment(e, appliedEnchant)) { + if (appliedEnchant.equalsIgnoreCase("Critical Chance")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Critical Chance"); + } + if (appliedEnchant.equalsIgnoreCase("Armor Penetration")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+" "+ChatColor.BLUE+"Armor Penetration"); + } + if (appliedEnchant.equalsIgnoreCase("Life Steal")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Life Steal"); + } + if (appliedEnchant.equalsIgnoreCase("Attack Speed")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Attack Speed"); + } + if (appliedEnchant.equalsIgnoreCase("Damage")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+" "+ChatColor.BLUE+"Damage"); + } + if (appliedEnchant.equalsIgnoreCase("Health")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+" "+ChatColor.BLUE+"Health"); + } + if (appliedEnchant.equalsIgnoreCase("Damage Reduction")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Damage Reduction"); + } + if (appliedEnchant.equalsIgnoreCase("Durability")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Durability"); + } + if (appliedEnchant.equalsIgnoreCase("Block Chance")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Block Chance"); + } + if (appliedEnchant.equalsIgnoreCase("Speed Boost Chance")) { + bonusesLore.set(i1, ChatColor.YELLOW+"+"+magnitude+"% "+ChatColor.BLUE+"Speed Boost Chance"); + } + } + } + + anvilInv.getItem(OUTPUT).getItemMeta().setLore(bonusesLore); + } + } + + } + // TRY EVERYTHING + player.getInventory().setContents(player.getInventory().getContents()); + anvilInv.setContents(anvilInv.getContents()); + player.updateInventory(); } } - - public void sendToInventory(ItemStack item, int number, Player player, Inventory target) { - - ItemStack temp = item.clone(); - - temp.setAmount(number); - ItemStack leftovers = target.addItem(temp).get(0); - + + public void sendToInventory(int slot, int itemCount, Player player) { + Bukkit.getLogger().info("Slot: " + slot); + Bukkit.getLogger().info("Item Count: " + itemCount); + Bukkit.getLogger().info("Player: " + player.getDisplayName()); + + // Get inventories + Inventory anvilInventory = player.getOpenInventory().getTopInventory(); + Inventory playerInventory = player.getOpenInventory().getBottomInventory(); + + // Get a temporary item stack to transfer + ItemStack temp = anvilInventory.getItem(slot).clone(); + temp.setAmount(itemCount); + + // Attempt to add to the player inventory. Store leftovers in itemstack to be dropped. + ItemStack leftovers = playerInventory.addItem(temp).get(0); + if (leftovers != null) { player.getWorld().dropItemNaturally(player.getLocation(), leftovers); } - item.setAmount(item.getAmount() - number); - - if (item.getAmount() - number == 0) { - item.setType(Material.AIR); - - Bukkit.getLogger().info("Item stack size reduced to 0, " + item.getType().toString() + " removed."); - + + Bukkit.getLogger().info("Slot stack size is " + anvilInventory.getItem(slot).getAmount()); + + + if (anvilInventory.getItem(slot).getAmount() - itemCount == 0) { + // Should reduce stack count to zero, meaning item needs to be removed. + Bukkit.getLogger().info("Item stack size reduced to 0, " + anvilInventory.getItem(slot).getType().toString() + " removed."); + anvilInventory.setItem(slot, new ItemStack(Material.AIR)); + } else { + // Should set stack size. + Bukkit.getLogger().info("Reduced stack size from " + anvilInventory.getItem(slot).getAmount() + " by " + itemCount); + anvilInventory.getItem(slot).setAmount(anvilInventory.getItem(slot).getAmount() - itemCount); } // player.getWorld().dropItemNaturally(player.getLocation(), new ItemStack(anvilInv.getItem(MATERIALS).getType(), anvilInv.getItem(MATERIALS).getAmount() - maxItemsNeeded)); } + + public double getEnchantmentNumb(String s) { + //Parse the string for spaces. + String[] enchant = s.split(" "); + if (enchant[0].contains(ChatColor.YELLOW+"")) { + String newstring = ((enchant[0].replace(ChatColor.YELLOW.getChar(), ' ')).replace('%', ' ')).replace(Character.toString((char)0x00A7), Character.toString((char)0x0020)); + //Bukkit.getLogger().info("Enchant number is "+Double.valueOf(newstring)); + return Double.valueOf(newstring); + } else { + return 0; + } + } + + public String getEnchantmentName(String s) { + //Parse the string for spaces. + String[] enchant = s.split(ChatColor.BLUE+""); + if (enchant.length > 1) { + return enchant[1]; + } else { + return null; + } + } + + + public boolean containsEnchantment(String s, String enchant) { + if (s.contains(enchant)) { + return true; + } + return false; + } + } +