diff --git a/src/main/java/org/shanerx/tradeshop/shop/Shop.java b/src/main/java/org/shanerx/tradeshop/shop/Shop.java index 064641dc..2e7593bb 100644 --- a/src/main/java/org/shanerx/tradeshop/shop/Shop.java +++ b/src/main/java/org/shanerx/tradeshop/shop/Shop.java @@ -1065,6 +1065,22 @@ public void setSideItems(ShopItemSide side, ItemStack newItem) { addSideItem(side, newItem); } + /** + * Attempts to fix the side of the trade in cases where an error was found. + * + * @param side Side of the trade to fix + */ + public void fixSide(ShopItemSide side) { + getSide(side).clear(); + try { + getSideItemStacks(side).forEach((item) -> { + if (item != null) addSideItem(side, item); + }); + } catch (NullPointerException ignored) { + getSide(side).clear(); + } + } + /** * Adds more items to the specified side * @@ -1107,7 +1123,7 @@ public void addSideItem(ShopItemSide side, ItemStack newItem) { * Checks if the shop has sufficient stock to make a trade on specified side */ public boolean hasSideStock(ShopItemSide side) { - return !shopType.isITrade() && hasSide(side) && getChestAsSC() != null && getChestAsSC().hasStock(getSideList(side)); + return !shopType.isITrade() && hasSide(side) && getChestAsSC() != null && getChestAsSC().hasStock(side, getSideList(side)); } /** diff --git a/src/main/java/org/shanerx/tradeshop/shop/ShopChest.java b/src/main/java/org/shanerx/tradeshop/shop/ShopChest.java index 45a8bed4..6c727fca 100644 --- a/src/main/java/org/shanerx/tradeshop/shop/ShopChest.java +++ b/src/main/java/org/shanerx/tradeshop/shop/ShopChest.java @@ -39,6 +39,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataType; import org.shanerx.tradeshop.TradeShop; +import org.shanerx.tradeshop.item.ShopItemSide; import org.shanerx.tradeshop.item.ShopItemStack; import org.shanerx.tradeshop.shoplocation.IllegalWorldException; import org.shanerx.tradeshop.shoplocation.ShopLocation; @@ -208,8 +209,13 @@ public Inventory getInventory() { return null; } - public boolean hasStock(List itemToCheck) { - return itemToCheck.size() > 0 && getItems(getInventory().getStorageContents(), itemToCheck, 1).get(0) != null; + public boolean hasStock(ShopItemSide side, List itemToCheck) { + List result = getItems(getInventory().getStorageContents(), itemToCheck, 1); + if (result.get(0) == null && result.size() == 1) { + getShop().fixSide(side); + } + + return itemToCheck.size() > 0 && result.get(0) != null; } public void loadFromName() { diff --git a/src/main/java/org/shanerx/tradeshop/utils/Utils.java b/src/main/java/org/shanerx/tradeshop/utils/Utils.java index a64055db..4d937dfb 100644 --- a/src/main/java/org/shanerx/tradeshop/utils/Utils.java +++ b/src/main/java/org/shanerx/tradeshop/utils/Utils.java @@ -589,14 +589,18 @@ public List getItems(ItemStack[] storageContents, List totalCount += count; for (ItemStack storageItem : storage.keySet()) { - boolean isSimilar = item.isSimilar(storageItem); - if (isSimilar) { - int taken = megaMin(storage.get(storageItem), count, storageItem.getMaxStackSize()); + if (item.isSimilar(storageItem)) { + int taken; + try { + taken = megaMin(storage.get(storageItem), count, storageItem.getMaxStackSize()); - if (found.putIfAbsent(item.getItemStack(), taken) != null) - found.put(item.getItemStack(), storage.get(storageItem) + taken); + if (found.putIfAbsent(item.getItemStack(), taken) != null) + found.put(item.getItemStack(), storage.get(storageItem) + taken); - storage.put(storageItem, storage.get(storageItem) - taken); + storage.put(storageItem, storage.get(storageItem) - taken); + } catch (NullPointerException ignored) { + return createBadList(); + } ItemStack goodItem = storageItem; goodItem.setAmount(taken);