From 686e4da27bb13d40f0a2c451aa004fba6a9ee204 Mon Sep 17 00:00:00 2001 From: KillerOfPie Date: Wed, 4 Nov 2020 10:41:13 -0800 Subject: [PATCH] Fixes - Fixes for errors during trade and destruction of iTrade shops - Removed old shulker code --- .../listeners/ShopTradeListener.java | 25 +++--- .../org/shanerx/tradeshop/objects/Shop.java | 4 +- .../org/shanerx/tradeshop/utils/Utils.java | 87 ++++--------------- 3 files changed, 32 insertions(+), 84 deletions(-) diff --git a/src/main/java/org/shanerx/tradeshop/listeners/ShopTradeListener.java b/src/main/java/org/shanerx/tradeshop/listeners/ShopTradeListener.java index d2822810..734ead21 100644 --- a/src/main/java/org/shanerx/tradeshop/listeners/ShopTradeListener.java +++ b/src/main/java/org/shanerx/tradeshop/listeners/ShopTradeListener.java @@ -179,18 +179,19 @@ private boolean tradeAllItems(Shop shop, int multiplier, Action action, Player b if (shop.getShopType() == ShopType.ITRADE && action.equals(Action.RIGHT_CLICK_BLOCK)) { //ITrade trade - //Method to find Cost items in player inventory and add to cost array - costItems = getItems(playerInventory, shop.getCost(), multiplier); - if (costItems.get(0) == null) { - ItemStack item = costItems.get(1); - buyer.sendMessage(Message.INSUFFICIENT_ITEMS.getPrefixed() - .replace("{ITEM}", item.hasItemMeta() && item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : item.getType().toString()) - .replace("{AMOUNT}", String.valueOf(item.getAmount() * multiplier))); - return false; - } - - for (ItemStack item : costItems) { - playerInventory.removeItem(item); + if (!shop.getCost().isEmpty()) { + costItems = getItems(playerInventory, shop.getCost(), multiplier); + if (costItems.get(0) == null) { + ItemStack item = costItems.get(1); + buyer.sendMessage(Message.INSUFFICIENT_ITEMS.getPrefixed() + .replace("{ITEM}", item.hasItemMeta() && item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : item.getType().toString()) + .replace("{AMOUNT}", String.valueOf(item.getAmount() * multiplier))); + return false; + } + + for (ItemStack item : costItems) { + playerInventory.removeItem(item); + } } for (ShopItemStack item : shop.getProduct()) { diff --git a/src/main/java/org/shanerx/tradeshop/objects/Shop.java b/src/main/java/org/shanerx/tradeshop/objects/Shop.java index a4e1e195..f0f5c055 100644 --- a/src/main/java/org/shanerx/tradeshop/objects/Shop.java +++ b/src/main/java/org/shanerx/tradeshop/objects/Shop.java @@ -987,8 +987,10 @@ public ShopRole checkRole(UUID uuidToCheck) { * Updates the number of trades the shop can make */ public void updateFullTradeCount() { - if (!hasStorage() || !hasProduct()) + if (!hasStorage() || !hasProduct()) { availableTrades = 0; + return; + } Inventory shopInventory = hasStorage() ? getChestAsSC().getInventory() : null; diff --git a/src/main/java/org/shanerx/tradeshop/utils/Utils.java b/src/main/java/org/shanerx/tradeshop/utils/Utils.java index 76df29a3..582ba921 100644 --- a/src/main/java/org/shanerx/tradeshop/utils/Utils.java +++ b/src/main/java/org/shanerx/tradeshop/utils/Utils.java @@ -485,64 +485,29 @@ public ArrayList getItems(Inventory inventory, List it for (ShopItemStack item : items) { totalCount += item.getItemStack().getAmount() * multiplier; - /* - if (item.getItemStack().getType().name().endsWith("SHULKER_BOX")) { - for (ItemStack itm : clone.getStorageContents()) { - if (itm != null && itm.getType().name().endsWith("SHULKER_BOX")) { - debugger.log("ShopTradeListener > Type of Item: " + itm.getType(), DebugLevels.TRADE); - debugger.log("ShopTradeListener > Amount of Item: " + itm.getAmount(), DebugLevels.TRADE); - //StringBuilder contents = new StringBuilder(); - //Arrays.stream(clone.getContents()).forEach(a -> contents.append(a != null ? a.getType().toString() : "Empty").append("|")); - - //debugger.log("ShopTradeListener > clone Contents: " + contents.toString(), DebugLevels.TRADE); - if (compareShulkers(itm, item.getItemStack())) { - clone.removeItem(itm); - ret.add(itm); - currentCount++; - } - - debugger.log("ShopTradeListener > CurrentCount: " + currentCount, DebugLevels.TRADE); - } - - if (currentCount >= totalCount) break; - } - } else { - */ - int count = item.getItemStack().getAmount() * multiplier, traded; - - debugger.log("ShopTradeListener > Item Material Being Searched for: " + item.getItemStack().getType().name(), DebugLevels.TRADE); - debugger.log("ShopTradeListener > Item count: " + count, DebugLevels.TRADE); - - for (ItemStack storageItem : clone.getStorageContents()) { - // Skips empty slots - if (storageItem != null) { + int count = item.getItemStack().getAmount() * multiplier, traded; - debugger.log("ShopTradeListener > Type of Item: " + storageItem.getType(), DebugLevels.TRADE); - debugger.log("ShopTradeListener > Amount of Item: " + storageItem.getAmount(), DebugLevels.TRADE); + debugger.log("ShopTradeListener > Item Material Being Searched for: " + item.getItemStack().getType().name(), DebugLevels.TRADE); + debugger.log("ShopTradeListener > Item count: " + count, DebugLevels.TRADE); - boolean isSimilar = item.isSimilar(storageItem); - debugger.log("ShopTradeListener > Location Similar: " + isSimilar, DebugLevels.TRADE); + for (ItemStack storageItem : clone.getStorageContents()) { + if (storageItem != null && item.isSimilar(storageItem)) { - if (isSimilar) { + traded = Math.min(Math.min(storageItem.getAmount(), item.getItemStack().getMaxStackSize()), count); - traded = Math.min(Math.min(storageItem.getAmount(), item.getItemStack().getMaxStackSize()), count); + storageItem.setAmount(traded); - storageItem.setAmount(traded); + clone.removeItem(storageItem); + ret.add(storageItem); + debugger.log("ShopTradeListener > Item traded: " + traded, DebugLevels.TRADE); - clone.removeItem(storageItem); - ret.add(storageItem); - debugger.log("ShopTradeListener > Item traded: " + traded, DebugLevels.TRADE); - - count -= traded; - currentCount += traded; - - debugger.log("ShopTradeListener > Item new count: " + count, DebugLevels.TRADE); - } - } + count -= traded; + currentCount += traded; + debugger.log("ShopTradeListener > Item new count: " + count, DebugLevels.TRADE); + } - if (currentCount >= totalCount) break; - } - //} + if (currentCount >= totalCount) break; + } if (currentCount < totalCount) { debugger.log("ShopTradeListener > TotalCount: " + totalCount, DebugLevels.TRADE); @@ -556,24 +521,4 @@ public ArrayList getItems(Inventory inventory, List it return ret; } - - /* public boolean compareShulkers(ItemStack item1, ItemStack item2) { - - if (item1 == null || item2 == null) - return false; - - try { - ArrayList contents1 = Lists.newArrayList(((ShulkerBox) ((BlockStateMeta) item1.clone().getItemMeta()).getBlockState()).getInventory().getContents()); - ShulkerBox shulker2 = (ShulkerBox) ((BlockStateMeta) item2.clone().getItemMeta()).getBlockState(); - - for (ItemStack itm : shulker2.getInventory().getContents()) { - contents1.remove(itm); - } - - return contents1.isEmpty(); - - } catch (ClassCastException ex) { - return false; - } - }*/ } \ No newline at end of file