Skip to content

Commit

Permalink
Bug Fix
Browse files Browse the repository at this point in the history
- Fix for bug reported by LewisB1#9506 on Discord where errors were being caused by old shops that required `/ts what` to view.
  • Loading branch information
KillerOfPie committed Jun 23, 2022
1 parent 28f2c1b commit 3ea9ed0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
18 changes: 17 additions & 1 deletion src/main/java/org/shanerx/tradeshop/shop/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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));
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/shanerx/tradeshop/shop/ShopChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -208,8 +209,13 @@ public Inventory getInventory() {
return null;
}

public boolean hasStock(List<ShopItemStack> itemToCheck) {
return itemToCheck.size() > 0 && getItems(getInventory().getStorageContents(), itemToCheck, 1).get(0) != null;
public boolean hasStock(ShopItemSide side, List<ShopItemStack> itemToCheck) {
List<ItemStack> 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() {
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/org/shanerx/tradeshop/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -589,14 +589,18 @@ public List<ItemStack> getItems(ItemStack[] storageContents, List<ShopItemStack>
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);
Expand Down

0 comments on commit 3ea9ed0

Please sign in to comment.