From 33fcf6cbb67333f8fbe2f157fd2544400704dbe3 Mon Sep 17 00:00:00 2001 From: Nathan Thor Date: Thu, 4 Nov 2021 16:01:13 -0700 Subject: [PATCH] Shulker Dupe Fix and other various changes - Version bump to 2.3.2-DEV - Fixed Errors caused by preventing shulkers as cost/product when storage is a shulker box when no storage was present(mostly iTrade shops) - Re-organized Commands enum list to match wiki sections - Changed some variable names to be more specific - Utils#getItems, fix for Shulker dupe was missing #clone() --- README.md | 8 ++--- pom.xml | 2 +- .../tradeshop/commands/CommandRunner.java | 36 ++++++++++++------- .../shanerx/tradeshop/enumys/Commands.java | 30 ++++++++++------ .../shanerx/tradeshop/enumys/DebugLevels.java | 5 ++- .../org/shanerx/tradeshop/objects/Shop.java | 14 ++++---- .../tradeshop/objects/ShopItemStack.java | 32 ++++++++--------- .../org/shanerx/tradeshop/utils/Utils.java | 24 +++++++------ 8 files changed, 89 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 558f29cb..6792f4cd 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,10 @@ ## Description This plugin manages how players trade one item for another. The concept behind it is very simple, you just place a chest and a sign on top, then write all the information on the sign. ->**Authors:** Lori00 / ShanerX & KillerOfPie
->**Name:** TradeShop
->**Version:** v2.2.0
->**Tested MC versions:** MC-1.8.X, 1.9.X, 1.10.X, 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x
+> **Authors:** Lori00 / ShanerX & KillerOfPie
+> **Name:** TradeShop
+> **Version:** v2.3.2-STABLE
+> **Tested MC versions:** MC-1.8.X, 1.9.X, 1.10.X, 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x
>*Please see [the wiki](https://github.com/SparklingComet/TradeShop/wiki) for more information regarding supported versions.* diff --git a/pom.xml b/pom.xml index d6832020..92c76339 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,7 @@ org.shanerx tradeshop - 2.3.1-STABLE + 2.3.2-DEV jar TradeShop https://tradeshop.github.io/ diff --git a/src/main/java/org/shanerx/tradeshop/commands/CommandRunner.java b/src/main/java/org/shanerx/tradeshop/commands/CommandRunner.java index b95980bf..aceb9869 100644 --- a/src/main/java/org/shanerx/tradeshop/commands/CommandRunner.java +++ b/src/main/java/org/shanerx/tradeshop/commands/CommandRunner.java @@ -316,6 +316,11 @@ public void setProduct() { return; } + if (!(shop.getShopType().isITrade() && shop.getInventoryLocation() == null) && itemInHand.getType().toString().endsWith("SHULKER_BOX") && shop.getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")) { + sendMessage(Message.NO_SHULKER_COST.getPrefixed()); + return; + } + if (amount > 0) { itemInHand.setAmount(amount); } @@ -386,6 +391,11 @@ public void addProduct() { return; } + if (!(shop.getShopType().isITrade() && shop.getInventoryLocation() == null) && itemInHand.getType().toString().endsWith("SHULKER_BOX") && shop.getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")) { + sendMessage(Message.NO_SHULKER_COST.getPrefixed()); + return; + } + if (amount > 0) { itemInHand.setAmount(amount); } @@ -438,43 +448,43 @@ public void setCost() { return; } - ItemStack itemInHand; + ItemStack costItem; if (mat == null) { - itemInHand = pSender.getInventory().getItemInMainHand().clone(); + costItem = pSender.getInventory().getItemInMainHand().clone(); } else { - itemInHand = new ItemStack(mat, 1); + costItem = new ItemStack(mat, 1); } - if (itemInHand.getType() == Material.AIR) { + if (costItem.getType() == Material.AIR) { sendMessage(Message.HELD_EMPTY.getPrefixed()); return; } - if (!isValidType(itemInHand.getType())) { + if (!isValidType(costItem.getType())) { sendMessage(Message.ILLEGAL_ITEM.getPrefixed()); return; } - if (itemInHand.getType().toString().endsWith("SHULKER_BOX") && shop.getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")) { + if (!(shop.getShopType().isITrade() && shop.getInventoryLocation() == null) && costItem.getType().toString().endsWith("SHULKER_BOX") && shop.getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")) { sendMessage(Message.NO_SHULKER_COST.getPrefixed()); return; } if (amount > 0) { - itemInHand.setAmount(amount); + costItem.setAmount(amount); } - if (Math.ceil((double) itemInHand.getAmount() / (double) itemInHand.getMaxStackSize()) > Setting.MAX_ITEMS_PER_TRADE_SIDE.getInt()) { + if (Math.ceil((double) costItem.getAmount() / (double) costItem.getMaxStackSize()) > Setting.MAX_ITEMS_PER_TRADE_SIDE.getInt()) { sendMessage(Message.TOO_MANY_ITEMS.getPrefixed().replaceAll("%side%", "costs")); return; } - - PlayerShopChangeEvent changeEvent = new PlayerShopChangeEvent(pSender, shop, ShopChange.SET_COST, new ObjectHolder(itemInHand)); + + PlayerShopChangeEvent changeEvent = new PlayerShopChangeEvent(pSender, shop, ShopChange.SET_COST, new ObjectHolder(costItem)); Bukkit.getPluginManager().callEvent(changeEvent); if (changeEvent.isCancelled()) return; - - shop.setCost(itemInHand); + + shop.setCost(costItem); sendMessage(Message.ITEM_ADDED.getPrefixed()); } @@ -531,7 +541,7 @@ public void addCost() { return; } - if (itemInHand.getType().toString().endsWith("SHULKER_BOX") && shop.getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")) { + if (!(shop.getShopType().isITrade() && shop.getInventoryLocation() == null) && itemInHand.getType().toString().endsWith("SHULKER_BOX") && shop.getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")) { sendMessage(Message.NO_SHULKER_COST.getPrefixed()); return; } diff --git a/src/main/java/org/shanerx/tradeshop/enumys/Commands.java b/src/main/java/org/shanerx/tradeshop/enumys/Commands.java index 8205a8ec..b8cc1437 100644 --- a/src/main/java/org/shanerx/tradeshop/enumys/Commands.java +++ b/src/main/java/org/shanerx/tradeshop/enumys/Commands.java @@ -40,13 +40,18 @@ public enum Commands { - HELP(Lists.newArrayList("help", "?"), Permissions.HELP, 1, 2, false, "Display help message", "/tradeshop $cmd$ [command]"), - SETUP(Lists.newArrayList("setup", "start", "create", "make"), Permissions.HELP, 1, 1, false, "Display shop setup tutorial", "/tradeshop $cmd$"), - BUGS(Lists.newArrayList("bugs", "bug"), Permissions.NONE, 1, 1, false, "Report bugs to the developers", "/tradeshop $cmd$"), - PLAYER_LEVEL(Lists.newArrayList("playerlevel", "pl"), Permissions.MANAGE_PLUGIN, 2, 3, false, "If Internal Permissions is enable this allows the getting and setting of player permission levels.", "/tradeshop $cmd$ "), + // Shop other management commands + OPEN(Lists.newArrayList("open"), Permissions.NONE, 1, 1, true, "Open shop", "/tradeshop $cmd$"), + CLOSE(Lists.newArrayList("close"), Permissions.NONE, 1, 1, true, "Close shop", "/tradeshop $cmd$"), + SWITCH(Lists.newArrayList("switch"), Permissions.EDIT, 1, 1, true, "Switch shop type", "/tradeshop $cmd$"), + EDIT(Lists.newArrayList("edit", "e"), Permissions.EDIT, 1, 1, true, "Opens Edit GUI for shop", "/tradeshop $cmd$"), + + // Shop user management commands ADD_MANAGER(Lists.newArrayList("addManager"), Permissions.NONE, 2, 2, true, "Add manager to shop", "/tradeshop $cmd$ "), REMOVE_USER(Lists.newArrayList("removeUser", "removeManager", "removeMember"), Permissions.NONE, 2, 2, true, "Remove user from shop", "/tradeshop $cmd$ "), ADD_MEMBER(Lists.newArrayList("addMember"), Permissions.NONE, 2, 2, true, "Add member to shop", "/tradeshop $cmd$ "), + + // Shop item management commands ADD_PRODUCT(Lists.newArrayList("addProduct"), Permissions.NONE, 1, 3, true, "Add product to shop", "/tradeshop $cmd$ [Amount] [Material]"), ADD_COST(Lists.newArrayList("addCost"), Permissions.NONE, 1, 3, true, "Add cost to shop", "/tradeshop $cmd$ [Amount] [Material]"), SET_PRODUCT(Lists.newArrayList("setProduct"), Permissions.NONE, 1, 3, true, "Set product of shop ", "/tradeshop $cmd$ [Amount] [Material]"), @@ -55,16 +60,21 @@ public enum Commands { REMOVE_COST(Lists.newArrayList("removeCost", "delCost"), Permissions.NONE, 2, 2, true, "Removes a product from the shop", "/tradeshop $cmd$ "), LIST_PRODUCT(Lists.newArrayList("listProduct"), Permissions.NONE, 1, 1, true, "Lists the products in the shop", "/tradeshop $cmd$"), LIST_COST(Lists.newArrayList("listCost"), Permissions.NONE, 1, 1, true, "Lists the costs in a shop", "/tradeshop $cmd$"), - OPEN(Lists.newArrayList("open"), Permissions.NONE, 1, 1, true, "Open shop", "/tradeshop $cmd$"), - CLOSE(Lists.newArrayList("close"), Permissions.NONE, 1, 1, true, "Close shop", "/tradeshop $cmd$"), + + // Shop info/Player commands WHO(Lists.newArrayList("who"), Permissions.INFO, 1, 1, true, "Shop members of shop", "/tradeshop $cmd$"), WHAT(Lists.newArrayList("what", "peek", "shop", "view"), Permissions.INFO, 1, 1, true, "Peek at shop inventory", "/tradeshop $cmd$"), - RELOAD(Lists.newArrayList("reload"), Permissions.MANAGE_PLUGIN, 1, 1, false, "Reload configuration files", "/tradeshop $cmd$"), - SWITCH(Lists.newArrayList("switch"), Permissions.EDIT, 1, 1, true, "Switch shop type", "/tradeshop $cmd$"), MULTI(Lists.newArrayList("multi", "multiply", "many"), Permissions.NONE, 1, 2, true, "Changes trade multiplier for this login", "/tradeshop $cmd$ "), STATUS(Lists.newArrayList("status", "stats", "s"), Permissions.INFO, 1, 2, true, "Displays the status of all shops the player has a relation to", "/tradeshop $cmd$ [Name]"), - EDIT(Lists.newArrayList("edit", "e"), Permissions.NONE, 1, 1, true, "Opens Edit GUI for shop", "/tradeshop $cmd$"), - TOGGLE_STATUS(Lists.newArrayList("togglestatus", "togglemotd"), Permissions.NONE, 1, 1, true, "Toggles the join message containing the list of shops one is involved with", "/tradeshop togglestatus"); + TOGGLE_STATUS(Lists.newArrayList("togglestatus", "togglemotd", "tstatus"), Permissions.INFO, 1, 1, true, "Toggles the join message containing the list of shops one is involved with", "/tradeshop togglestatus"), + + // Other commands + HELP(Lists.newArrayList("help", "?"), Permissions.HELP, 1, 2, false, "Display help message", "/tradeshop $cmd$ [command]"), + SETUP(Lists.newArrayList("setup", "start", "create", "make"), Permissions.HELP, 1, 1, false, "Display shop setup tutorial", "/tradeshop $cmd$"), + BUGS(Lists.newArrayList("bugs", "bug"), Permissions.NONE, 1, 1, false, "Report bugs to the developers", "/tradeshop $cmd$"), + PLAYER_LEVEL(Lists.newArrayList("playerlevel", "pl"), Permissions.MANAGE_PLUGIN, 2, 3, false, "If Internal Permissions is enable this allows the getting and setting of player permission levels.", "/tradeshop $cmd$ "), + RELOAD(Lists.newArrayList("reload"), Permissions.MANAGE_PLUGIN, 1, 1, false, "Reload configuration files", "/tradeshop $cmd$"); + /** * Name of the permission diff --git a/src/main/java/org/shanerx/tradeshop/enumys/DebugLevels.java b/src/main/java/org/shanerx/tradeshop/enumys/DebugLevels.java index 348da93d..71a67b6f 100644 --- a/src/main/java/org/shanerx/tradeshop/enumys/DebugLevels.java +++ b/src/main/java/org/shanerx/tradeshop/enumys/DebugLevels.java @@ -37,7 +37,10 @@ public enum DebugLevels { TRADE(4, Level.WARNING), // 8 INVENTORY_CLOSE_NPE(5, Level.WARNING), // 16 ITEM_COMPARE(6, Level.WARNING), // 32 - NAME_COMPARE(7, Level.WARNING); // 64 + NAME_COMPARE(7, Level.WARNING), // 64 + SHULKERS_SUCK(8, Level.WARNING) // 128 + + ; //position is what value to check for this level in the binary string -1. // diff --git a/src/main/java/org/shanerx/tradeshop/objects/Shop.java b/src/main/java/org/shanerx/tradeshop/objects/Shop.java index be63ec6b..07a056f6 100644 --- a/src/main/java/org/shanerx/tradeshop/objects/Shop.java +++ b/src/main/java/org/shanerx/tradeshop/objects/Shop.java @@ -448,7 +448,6 @@ public void setCost(ItemStack newItem) { cost.clear(); addCost(newItem); - cost.removeIf(item -> item.getItemStack().getType().toString().endsWith("SHULKER_BOX") && getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")); } /** @@ -472,12 +471,13 @@ public void addCost(ItemStack newItem) { ItemStack itm = newItem.clone(); itm.setAmount(amount); items.add(itm); - amount -= amount; + amount = 0; } } items.forEach((ItemStack iS) -> cost.add(new ShopItemStack(iS))); - cost.removeIf(item -> item.getItemStack().getType().toString().endsWith("SHULKER_BOX") && getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")); + if (!getShopType().isITrade() && chestLoc != null) + cost.removeIf(item -> item.getItemStack().getType().toString().endsWith("SHULKER_BOX") && getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")); saveShop(); updateSign(); @@ -637,9 +637,10 @@ public void fixAfterLoad() { if (utils == null) utils = new Utils(); shopLoc.stringToWorld(); - if (!shopType.isITrade() && chestLoc != null) + if (!getShopType().isITrade() && chestLoc != null) { chestLoc.stringToWorld(); - cost.removeIf(item -> item.getItemStack().getType().toString().endsWith("SHULKER_BOX") && getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")); + cost.removeIf(item -> item.getItemStack().getType().toString().endsWith("SHULKER_BOX") && getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")); + } if (getShopSign() != null) updateSign(); } @@ -1052,7 +1053,8 @@ public void updateShopUsers(Set updatedUserSet) { * Updates shops cost list */ public void updateCost(List updatedCostList) { - updatedCostList.removeIf(item -> item.getItemStack().getType().toString().endsWith("SHULKER_BOX") && getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")); + if (!getShopType().isITrade() && chestLoc != null) + updatedCostList.removeIf(item -> item.getItemStack().getType().toString().endsWith("SHULKER_BOX") && getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX")); cost = updatedCostList; saveShop(); updateSign(); diff --git a/src/main/java/org/shanerx/tradeshop/objects/ShopItemStack.java b/src/main/java/org/shanerx/tradeshop/objects/ShopItemStack.java index 558e22a2..2eafa654 100644 --- a/src/main/java/org/shanerx/tradeshop/objects/ShopItemStack.java +++ b/src/main/java/org/shanerx/tradeshop/objects/ShopItemStack.java @@ -202,21 +202,21 @@ public boolean isSimilar(ItemStack toCompare) { if (itemStack.getType().toString().endsWith("SHULKER_BOX") && getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_SHULKER_INVENTORY)) { try { - ArrayList contents1 = Lists.newArrayList(((ShulkerBox) ((BlockStateMeta) toCompareMeta).getBlockState()).getInventory().getContents()), - contents2 = Lists.newArrayList(((ShulkerBox) ((BlockStateMeta) itemStackMeta).getBlockState()).getInventory().getContents()); + ArrayList itemStackContents = Lists.newArrayList(((ShulkerBox) ((BlockStateMeta) toCompareMeta).getBlockState()).getInventory().getContents()), + toCompareContents = Lists.newArrayList(((ShulkerBox) ((BlockStateMeta) itemStackMeta).getBlockState()).getInventory().getContents()); - contents1.removeIf(Objects::isNull); - contents2.removeIf(Objects::isNull); + itemStackContents.removeIf(Objects::isNull); + toCompareContents.removeIf(Objects::isNull); - if (contents1.isEmpty() != contents2.isEmpty()) + if (itemStackContents.isEmpty() != toCompareContents.isEmpty()) return false; - for (ItemStack itm : contents2) { - if (!contents1.remove(itm)) + for (ItemStack itm : toCompareContents) { + if (!itemStackContents.remove(itm)) return false; } - if (!contents1.isEmpty()) + if (!itemStackContents.isEmpty()) return false; } catch (ClassCastException ex) { @@ -229,21 +229,21 @@ public boolean isSimilar(ItemStack toCompare) { itemStack.getType().equals(Material.BUNDLE) && getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_BUNDLE_INVENTORY)) { try { - ArrayList contents1 = Lists.newArrayList(((BundleMeta) toCompareMeta).getItems()), - contents2 = Lists.newArrayList(((BundleMeta) itemStackMeta).getItems()); + ArrayList itemStackContents = Lists.newArrayList(((BundleMeta) toCompareMeta).getItems()), + toCompareContents = Lists.newArrayList(((BundleMeta) itemStackMeta).getItems()); - contents1.removeIf(Objects::isNull); - contents2.removeIf(Objects::isNull); + itemStackContents.removeIf(Objects::isNull); + toCompareContents.removeIf(Objects::isNull); - if (contents1.isEmpty() != contents2.isEmpty()) + if (itemStackContents.isEmpty() != toCompareContents.isEmpty()) return false; - for (ItemStack itm : contents2) { - if (!contents1.remove(itm)) + for (ItemStack itm : toCompareContents) { + if (!itemStackContents.remove(itm)) return false; } - if (!contents1.isEmpty()) + if (!itemStackContents.isEmpty()) return false; } catch (ClassCastException ex) { diff --git a/src/main/java/org/shanerx/tradeshop/utils/Utils.java b/src/main/java/org/shanerx/tradeshop/utils/Utils.java index dfabfe7f..aae75d7b 100644 --- a/src/main/java/org/shanerx/tradeshop/utils/Utils.java +++ b/src/main/java/org/shanerx/tradeshop/utils/Utils.java @@ -481,22 +481,24 @@ public ArrayList getItems(Inventory inventory, List it debugger.log("ShopTradeListener > Item count: " + count, DebugLevels.TRADE); for (ItemStack storageItem : clone.getStorageContents()) { - boolean isSimilar = item.isSimilar(storageItem); - if (storageItem != null && isSimilar) { + if (storageItem != null) { + boolean isSimilar = item.isSimilar(storageItem.clone()); + 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 traded: " + count, DebugLevels.TRADE); + count -= traded; + currentCount += traded; + debugger.log("ShopTradeListener > Item traded: " + count, DebugLevels.TRADE); + } + debugger.log("ShopTradeListener > isSimilar: " + isSimilar, DebugLevels.NAME_COMPARE); } - debugger.log("ShopTradeListener > isSimilar: " + isSimilar, DebugLevels.NAME_COMPARE); if (currentCount >= totalCount) break; }