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
This commit is contained in:
PretzelJohn 2022-01-21 21:08:24 -05:00
parent afacf4adc8
commit dd49756014
2 changed files with 13 additions and 9 deletions

View File

@ -40,19 +40,20 @@ public class InventoryListener implements Listener {
public void onPlayerStopTrading(final InventoryCloseEvent event) { public void onPlayerStopTrading(final InventoryCloseEvent event) {
//Don't do anything unless the player is actually finished trading with a villager //Don't do anything unless the player is actually finished trading with a villager
if(event.getInventory().getType() != InventoryType.MERCHANT) return; if(event.getInventory().getType() != InventoryType.MERCHANT) return;
if(!(event.getInventory().getHolder() instanceof Villager)) return;
if(!(event.getPlayer() instanceof Player)) return; if(!(event.getPlayer() instanceof Player)) return;
if(!(event.getInventory().getHolder() instanceof Villager)) return;
final Player player = (Player)event.getPlayer(); 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 //Reset the villager's NBT data when a player is finished trading
final PlayerData playerData = instance.getPlayerData().get(player.getUniqueId()); final PlayerData playerData = instance.getPlayerData().get(player.getUniqueId());
if(playerData == null) return; if(playerData == null) return;
final VillagerWrapper villager = playerData.getTradingVillager(); final VillagerWrapper villagerWrapper = playerData.getTradingVillager();
if(villager == null) return; if(villagerWrapper == null) return;
playerData.setTradingVillager(null); playerData.setTradingVillager(null);
villager.reset(); villagerWrapper.reset();
} }
/** Handles when a player successfully trades with a villager */ /** 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.getWhoClicked() instanceof Player)) return;
if(event.getRawSlot() != 2) return; if(event.getRawSlot() != 2) return;
final Player player = (Player)event.getWhoClicked(); 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 //Get the items involved in the trade
final ItemStack result = event.getCurrentItem(); final ItemStack result = event.getCurrentItem();
@ -86,7 +88,7 @@ public class InventoryListener implements Listener {
if(cooldownStr.equals("0") && restockStr.equals("0")) return; if(cooldownStr.equals("0") && restockStr.equals("0")) return;
//Get the selected recipe by the items in the slots //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) { if(selectedRecipe == null) {
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -94,7 +96,7 @@ public class InventoryListener implements Listener {
//Add a cooldown to the trade if the player has reached the max uses //Add a cooldown to the trade if the player has reached the max uses
final PlayerData playerData = instance.getPlayerData().get(player.getUniqueId()); 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; if(playerData == null || playerData.getTradingVillager() == null) return;
Bukkit.getScheduler().runTaskLater(instance, () -> { Bukkit.getScheduler().runTaskLater(instance, () -> {
int uses = selectedRecipe.getUses(); int uses = selectedRecipe.getUses();

View File

@ -31,7 +31,9 @@ public class VillagerWrapper {
final List<RecipeWrapper> recipes = new ArrayList<>(); final List<RecipeWrapper> recipes = new ArrayList<>();
//Add the recipes from the villager's NBT data into a list of wrapped recipes //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) { for(NBTCompound nbtRecipe : nbtRecipes) {
recipes.add(new RecipeWrapper(nbtRecipe)); recipes.add(new RecipeWrapper(nbtRecipe));
} }