From dd4975601458fbde19a476abc5b6ef1a374f9e99 Mon Sep 17 00:00:00 2001 From: PretzelJohn <58197328+PretzelJohn@users.noreply.github.com> Date: Fri, 21 Jan 2022 21:08:24 -0500 Subject: [PATCH] Version 1.5.1: * Fixed an NPE that occurred when a villager has no offers To-do: * Add enchantments, data, etc. to ingredients and result * Add config editor GUI in-game --- .../listeners/InventoryListener.java | 18 ++++++++++-------- .../wrappers/VillagerWrapper.java | 4 +++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/com/pretzel/dev/villagertradelimiter/listeners/InventoryListener.java b/src/com/pretzel/dev/villagertradelimiter/listeners/InventoryListener.java index 4aaa4ba..d90fb5d 100644 --- a/src/com/pretzel/dev/villagertradelimiter/listeners/InventoryListener.java +++ b/src/com/pretzel/dev/villagertradelimiter/listeners/InventoryListener.java @@ -40,19 +40,20 @@ public class InventoryListener implements Listener { public void onPlayerStopTrading(final InventoryCloseEvent event) { //Don't do anything unless the player is actually finished trading with a villager if(event.getInventory().getType() != InventoryType.MERCHANT) return; - if(!(event.getInventory().getHolder() instanceof Villager)) return; if(!(event.getPlayer() instanceof Player)) return; + if(!(event.getInventory().getHolder() instanceof Villager)) return; final Player player = (Player)event.getPlayer(); - if(settings.shouldSkipNPC(player) || settings.shouldSkipNPC((Villager)event.getInventory().getHolder())) return; //Skips NPCs + final Villager villager = (Villager)event.getInventory().getHolder(); + if(settings.shouldSkipNPC(player) || settings.shouldSkipNPC(villager)) return; //Skips NPCs //Reset the villager's NBT data when a player is finished trading final PlayerData playerData = instance.getPlayerData().get(player.getUniqueId()); if(playerData == null) return; - final VillagerWrapper villager = playerData.getTradingVillager(); - if(villager == null) return; + final VillagerWrapper villagerWrapper = playerData.getTradingVillager(); + if(villagerWrapper == null) return; playerData.setTradingVillager(null); - villager.reset(); + villagerWrapper.reset(); } /** Handles when a player successfully trades with a villager */ @@ -63,7 +64,8 @@ public class InventoryListener implements Listener { if(!(event.getWhoClicked() instanceof Player)) return; if(event.getRawSlot() != 2) return; final Player player = (Player)event.getWhoClicked(); - if(settings.shouldSkipNPC(player) || settings.shouldSkipNPC((Villager)event.getInventory().getHolder())) return; //Skips NPCs + final Villager villager = (Villager)event.getInventory().getHolder(); + if(settings.shouldSkipNPC(player) || settings.shouldSkipNPC(villager)) return; //Skips NPCs //Get the items involved in the trade final ItemStack result = event.getCurrentItem(); @@ -86,7 +88,7 @@ public class InventoryListener implements Listener { if(cooldownStr.equals("0") && restockStr.equals("0")) return; //Get the selected recipe by the items in the slots - final MerchantRecipe selectedRecipe = getSelectedRecipe((Villager)event.getInventory().getHolder(), ingredient1, ingredient2, result); + final MerchantRecipe selectedRecipe = getSelectedRecipe(villager, ingredient1, ingredient2, result); if(selectedRecipe == null) { event.setCancelled(true); return; @@ -94,7 +96,7 @@ public class InventoryListener implements Listener { //Add a cooldown to the trade if the player has reached the max uses final PlayerData playerData = instance.getPlayerData().get(player.getUniqueId()); - final PlayerData villagerData = instance.getPlayerData().get(((Villager)event.getInventory().getHolder()).getUniqueId()); + final PlayerData villagerData = instance.getPlayerData().get(villager.getUniqueId()); if(playerData == null || playerData.getTradingVillager() == null) return; Bukkit.getScheduler().runTaskLater(instance, () -> { int uses = selectedRecipe.getUses(); diff --git a/src/com/pretzel/dev/villagertradelimiter/wrappers/VillagerWrapper.java b/src/com/pretzel/dev/villagertradelimiter/wrappers/VillagerWrapper.java index f774b76..06787ef 100644 --- a/src/com/pretzel/dev/villagertradelimiter/wrappers/VillagerWrapper.java +++ b/src/com/pretzel/dev/villagertradelimiter/wrappers/VillagerWrapper.java @@ -31,7 +31,9 @@ public class VillagerWrapper { final List recipes = new ArrayList<>(); //Add the recipes from the villager's NBT data into a list of wrapped recipes - final NBTCompoundList nbtRecipes = entity.getCompound("Offers").getCompoundList("Recipes"); + final NBTCompound offers = entity.getCompound("Offers"); + if(offers == null) return recipes; + final NBTCompoundList nbtRecipes = offers.getCompoundList("Recipes"); for(NBTCompound nbtRecipe : nbtRecipes) { recipes.add(new RecipeWrapper(nbtRecipe)); }