Skip to content

Commit

Permalink
[FIX] Books throwing error in enchant table. Villager Trades getting …
Browse files Browse the repository at this point in the history
…restricted enchants
  • Loading branch information
Dzious committed Dec 14, 2020
1 parent 4120881 commit 36c2faa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 57 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.dzious.bukkit.enchantcontrol</groupId>
<artifactId>EnchantControl_Reforged</artifactId>
<version>1.2.3</version>
<version>1.2.4</version>

<properties>
<java.version>1.8</java.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 : ");

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dzious.bukkit.enchantcontrol.EnchantControl;

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;
Expand All @@ -25,16 +27,16 @@ public VillagerTradeListener (EnchantControl plugin) {
this.plugin = plugin;
}

@EventHandler
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = false)
public void onVillagerAcquireTrade(VillagerAcquireTradeEvent e)
{
ItemStack item = e.getRecipe().getResult();
if (item.getItemMeta() == null ||
(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());
Expand Down Expand Up @@ -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<ItemStack> 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, Integer> enchantment : finalEnchantments.entrySet()) {
plugin.getLogManager().logDebugConsole("Enchantment (Loop) : " + finalEnchantments.toString());
Expand All @@ -90,55 +95,15 @@ 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 {
plugin.getLogManager().logDebugConsole("Stored Enchantments (Meta) : " + ((EnchantmentStorageMeta)(e.getRecipe().getResult().getItemMeta())).getStoredEnchants().toString());
}
}
}

// 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, Integer> 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, Integer> 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))));
// }
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,8 +93,12 @@ public List<Enchantment> getValidEnchantments(ItemStack item) {
List<Enchantment> validEnchantments = new ArrayList<>();

for (Map.Entry<Enchantment, Integer> 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);
}
Expand Down

0 comments on commit 36c2faa

Please sign in to comment.