diff --git a/pom.xml b/pom.xml index db1ce4b..f0e6cf0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.dzious.bukkit.enchantcontrol EnchantControl_Reforged - 1.2.3 + 1.2.4 1.8 diff --git a/src/com/dzious/bukkit/enchantcontrol/listener/EnchantmentTableListener.java b/src/com/dzious/bukkit/enchantcontrol/listener/EnchantmentTableListener.java index 65f680b..0665fab 100644 --- a/src/com/dzious/bukkit/enchantcontrol/listener/EnchantmentTableListener.java +++ b/src/com/dzious/bukkit/enchantcontrol/listener/EnchantmentTableListener.java @@ -8,6 +8,7 @@ import com.dzious.bukkit.enchantcontrol.EnchantControl; import org.bukkit.ChatColor; +import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.EnchantmentOffer; import org.bukkit.event.EventHandler; @@ -26,8 +27,9 @@ public EnchantmentTableListener (EnchantControl plugin) { @EventHandler(priority = EventPriority.HIGHEST) public void onEnchantmentPreparation(PrepareItemEnchantEvent e) { - if (!plugin.getEnchantmentManager().hasEnchantActive(e.getItem())) + if (e.getItem().getType() != Material.BOOK && !plugin.getEnchantmentManager().hasEnchantActive(e.getItem())) { return; + } plugin.getLogManager().logDebugConsole("Generating offers : "); @@ -49,18 +51,24 @@ public void onEnchantmentPreparation(PrepareItemEnchantEvent e) Enchantment newEnchantment = plugin.getEnchantmentManager().rerollEnchantment(e.getItem(), offersList); - e.getOffers()[i].setEnchantment(newEnchantment); if (newEnchantment != null) { + e.getOffers()[i].setEnchantment(newEnchantment); if (e.getOffers()[i].getEnchantmentLevel() > plugin.getEnchantmentManager().getAffectedEnchantments().get(newEnchantment)) e.getOffers()[i].setEnchantmentLevel(plugin.getEnchantmentManager().getAffectedEnchantments().get(newEnchantment)); } else { - e.getOffers()[i].setEnchantmentLevel(0); + e.getOffers()[i] = null; //.setEnchantmentLevel(0); } } else if (plugin.getEnchantmentManager().getAffectedEnchantments().get(e.getOffers()[i].getEnchantment()) < e.getOffers()[i].getEnchantmentLevel()){ e.getOffers()[i].setEnchantmentLevel(plugin.getEnchantmentManager().getAffectedEnchantments().get(e.getOffers()[i].getEnchantment())); } } - plugin.getLogManager().logDebugConsole(e.getOffers().toString()); + for (int i = 0; i < e.getOffers().length; i++) { + if (e.getOffers()[i] != null) { + plugin.getLogManager().logDebugConsole("Final offer " + i + " is " + ChatColor.GREEN + e.getOffers()[i].getEnchantment().toString()); + } else { + plugin.getLogManager().logDebugConsole("Final offer " + i + " is " + ChatColor.GREEN + "null"); + } + } plugin.getLogManager().logDebugConsole(offersList.toString()); plugin.getPlayersManager().getPlayer(e.getEnchanter().getUniqueId()).setOffers(e.getOffers(), offersList); } diff --git a/src/com/dzious/bukkit/enchantcontrol/listener/VillagerTradeListener.java b/src/com/dzious/bukkit/enchantcontrol/listener/VillagerTradeListener.java index 48b2186..2638f70 100644 --- a/src/com/dzious/bukkit/enchantcontrol/listener/VillagerTradeListener.java +++ b/src/com/dzious/bukkit/enchantcontrol/listener/VillagerTradeListener.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import com.dzious.bukkit.enchantcontrol.EnchantControl; @@ -9,6 +10,7 @@ import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.entity.VillagerAcquireTradeEvent; import org.bukkit.inventory.ItemStack; @@ -25,7 +27,7 @@ public VillagerTradeListener (EnchantControl plugin) { this.plugin = plugin; } - @EventHandler + @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = false) public void onVillagerAcquireTrade(VillagerAcquireTradeEvent e) { ItemStack item = e.getRecipe().getResult(); @@ -33,8 +35,8 @@ public void onVillagerAcquireTrade(VillagerAcquireTradeEvent e) (item.getItemMeta().getEnchants().isEmpty() && (!(item.getItemMeta() instanceof EnchantmentStorageMeta) || ((EnchantmentStorageMeta)(item.getItemMeta())).getStoredEnchants().isEmpty()))) { - plugin.getLogManager().logDebugConsole("Trade item is not enchanted."); - return; + plugin.getLogManager().logDebugConsole("Trade item is not enchanted."); + return; } plugin.getLogManager().logDebugConsole("Trade item : " + e.getRecipe().getResult()); @@ -78,9 +80,12 @@ public void onVillagerAcquireTrade(VillagerAcquireTradeEvent e) plugin.getLogManager().logDebugConsole("Enchantments (End) : " + finalEnchantments.toString()); if (finalEnchantments.isEmpty() && item.getType() == Material.ENCHANTED_BOOK) { - e.setCancelled(true); - plugin.getServer().getPluginManager().callEvent(new VillagerAcquireTradeEvent(e.getEntity(), - new MerchantRecipe(new ItemStack(Material.BOOK), 0, 12, true,1, (float)(0.05)))); + MerchantRecipe recipe = new MerchantRecipe(new ItemStack(Material.BOOK), 12, 12, true, 1, (float)0.05); + List ingredients = new ArrayList<>(); + ingredients.add(new ItemStack(Material.EMERALD, 4)); + ingredients.add(new ItemStack(Material.AIR)); + recipe.setIngredients(ingredients); + e.setRecipe(recipe); } else { for (Map.Entry enchantment : finalEnchantments.entrySet()) { plugin.getLogManager().logDebugConsole("Enchantment (Loop) : " + finalEnchantments.toString()); @@ -90,7 +95,10 @@ public void onVillagerAcquireTrade(VillagerAcquireTradeEvent e) ((EnchantmentStorageMeta)meta).addStoredEnchant(enchantment.getKey(), enchantment.getValue(), true); } } - e.getRecipe().getResult().setItemMeta(meta); + item.setItemMeta(meta); + MerchantRecipe recipe = new MerchantRecipe(item , e.getRecipe().getUses(), e.getRecipe().getMaxUses(), e.getRecipe().hasExperienceReward(), e.getRecipe().getVillagerExperience(), e.getRecipe().getPriceMultiplier()); + recipe.setIngredients(e.getRecipe().getIngredients()); + e.setRecipe(recipe); if (!item.getItemMeta().getEnchants().isEmpty()) { plugin.getLogManager().logDebugConsole("Enchantments (Meta) : " + e.getRecipe().getResult().getItemMeta().getEnchants().toString()); } else { @@ -98,47 +106,4 @@ public void onVillagerAcquireTrade(VillagerAcquireTradeEvent e) } } } - - // if (e.getRecipe().getResult().getEnchantments().isEmpty() && - // (e.getRecipe().getResult().getItemMeta() == null | - // !(e.getRecipe().getResult().getItemMeta() instanceof EnchantmentStorageMeta) || - // !((EnchantmentStorageMeta)(e.getRecipe().getResult().getItemMeta())).hasStoredEnchants())) - // return; - - // if (e.getRecipe().getResult().getEnchantments().isEmpty() == false) { - // for (Map.Entry enchantment : e.getRecipe().getResult().getEnchantments().entrySet()) { - // if (enchantment.getValue() > plugin.getEnchantmentManager().getAffectedEnchantments().get(enchantment.getKey())) { - // if (plugin.getEnchantmentManager().getAffectedEnchantments().get(enchantment.getKey()) <= 0) - // e.getRecipe().getResult().removeEnchantment(enchantment.getKey()); - // else if (enchantment.getValue() > plugin.getEnchantmentManager().getAffectedEnchantments().get(enchantment.getKey())) { - // e.getRecipe().getResult().removeEnchantment(enchantment.getKey()); - // e.getRecipe().getResult().addEnchantment(enchantment.getKey(),plugin.getEnchantmentManager().getAffectedEnchantments().get(enchantment.getKey())); - // } - // } - // } - // if (e.getRecipe().getResult().getEnchantments().isEmpty() == true) { - // e.setCancelled(true); - // plugin.getServer().getPluginManager().callEvent(new VillagerAcquireTradeEvent(e.getEntity(), new MerchantRecipe(new ItemStack(Material.AIR), 0, 0,false,0,1))); - // } - // } else { - // for (Map.Entry enchantment :((EnchantmentStorageMeta)(e.getRecipe().getResult().getItemMeta())).getStoredEnchants().entrySet()) { - // if (enchantment.getValue() > plugin.getEnchantmentManager().getAffectedEnchantments().get(enchantment.getKey())) { - // if (plugin.getEnchantmentManager().getAffectedEnchantments().get(enchantment.getKey()) <= 0) { - // EnchantmentStorageMeta meta = ((EnchantmentStorageMeta)(e.getRecipe().getResult().getItemMeta())); - // meta.removeStoredEnchant(enchantment.getKey()); - // e.getRecipe().getResult().setItemMeta(meta); - // } else if (enchantment.getValue() > plugin.getEnchantmentManager().getAffectedEnchantments().get(enchantment.getKey())) { - // EnchantmentStorageMeta meta = ((EnchantmentStorageMeta)(e.getRecipe().getResult().getItemMeta())); - // meta.removeStoredEnchant(enchantment.getKey()); - // meta.addStoredEnchant(enchantment.getKey(),plugin.getEnchantmentManager().getAffectedEnchantments().get(enchantment.getKey()), true); - // e.getRecipe().getResult().setItemMeta(meta); - // } - // } - // } - // if (((EnchantmentStorageMeta)(e.getRecipe().getResult().getItemMeta())).getStoredEnchants().isEmpty() == true) { - // e.setCancelled(true); - // plugin.getServer().getPluginManager().callEvent(new VillagerAcquireTradeEvent(e.getEntity(), new MerchantRecipe(new ItemStack(Material.BOOK), 0, 12, true,1, (float)(0.05)))); - // } - // } - // } } diff --git a/src/com/dzious/bukkit/enchantcontrol/plugin/EnchantmentManager.java b/src/com/dzious/bukkit/enchantcontrol/plugin/EnchantmentManager.java index 0467e20..f7d149f 100644 --- a/src/com/dzious/bukkit/enchantcontrol/plugin/EnchantmentManager.java +++ b/src/com/dzious/bukkit/enchantcontrol/plugin/EnchantmentManager.java @@ -8,6 +8,7 @@ import com.dzious.bukkit.enchantcontrol.EnchantControl; import org.bukkit.ChatColor; +import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; @@ -92,8 +93,12 @@ public List getValidEnchantments(ItemStack item) { List validEnchantments = new ArrayList<>(); for (Map.Entry enchantment : enchantments.entrySet()) { - if (enchantment.getKey().canEnchantItem(item) == true && enchantment.getValue() > 0) - validEnchantments.add(enchantment.getKey()); + if (enchantment.getValue() > 0) { + if (item.getType() != Material.BOOK) + validEnchantments.add(enchantment.getKey()); + else if (enchantment.getKey().canEnchantItem(item)) + validEnchantments.add(enchantment.getKey()); + } } return (validEnchantments); }