Skip to content

Commit

Permalink
Shulker Dupe Fix and other various changes
Browse files Browse the repository at this point in the history
- 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()
  • Loading branch information
KillerOfPie committed Nov 4, 2021
1 parent 8314a9f commit 33fcf6c
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 62 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<br/>
>**Name:** TradeShop<br/>
>**Version:** v2.2.0<br/>
>**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 <br/>
> **Authors:** Lori00 / ShanerX & KillerOfPie<br/>
> **Name:** TradeShop<br/>
> **Version:** v2.3.2-STABLE<br/>
> **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 <br/>
>*Please see [the wiki](https://github.com/SparklingComet/TradeShop/wiki) for more information regarding supported versions.*
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<groupId>org.shanerx</groupId>
<artifactId>tradeshop</artifactId>
<version>2.3.1-STABLE</version>
<version>2.3.2-DEV</version>
<packaging>jar</packaging>
<name>TradeShop</name>
<url>https://tradeshop.github.io/</url>
Expand Down
36 changes: 23 additions & 13 deletions src/main/java/org/shanerx/tradeshop/commands/CommandRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<ItemStack>(itemInHand));

PlayerShopChangeEvent changeEvent = new PlayerShopChangeEvent(pSender, shop, ShopChange.SET_COST, new ObjectHolder<ItemStack>(costItem));
Bukkit.getPluginManager().callEvent(changeEvent);
if (changeEvent.isCancelled()) return;
shop.setCost(itemInHand);

shop.setCost(costItem);

sendMessage(Message.ITEM_ADDED.getPrefixed());
}
Expand Down Expand Up @@ -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;
}
Expand Down
30 changes: 20 additions & 10 deletions src/main/java/org/shanerx/tradeshop/enumys/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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$ <name> <newlevel>"),
// 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$ <name>"),
REMOVE_USER(Lists.newArrayList("removeUser", "removeManager", "removeMember"), Permissions.NONE, 2, 2, true, "Remove user from shop", "/tradeshop $cmd$ <Name>"),
ADD_MEMBER(Lists.newArrayList("addMember"), Permissions.NONE, 2, 2, true, "Add member to shop", "/tradeshop $cmd$ <name>"),

// 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]"),
Expand All @@ -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 #>"),
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$ <Amount>"),
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$ <name> <newlevel>"),
RELOAD(Lists.newArrayList("reload"), Permissions.MANAGE_PLUGIN, 1, 1, false, "Reload configuration files", "/tradeshop $cmd$");


/**
* Name of the permission
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/shanerx/tradeshop/enumys/DebugLevels.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/org/shanerx/tradeshop/objects/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -1052,7 +1053,8 @@ public void updateShopUsers(Set<ShopUser> updatedUserSet) {
* Updates shops cost list
*/
public void updateCost(List<ShopItemStack> 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();
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/org/shanerx/tradeshop/objects/ShopItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,21 @@ public boolean isSimilar(ItemStack toCompare) {
if (itemStack.getType().toString().endsWith("SHULKER_BOX") &&
getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_SHULKER_INVENTORY)) {
try {
ArrayList<ItemStack> contents1 = Lists.newArrayList(((ShulkerBox) ((BlockStateMeta) toCompareMeta).getBlockState()).getInventory().getContents()),
contents2 = Lists.newArrayList(((ShulkerBox) ((BlockStateMeta) itemStackMeta).getBlockState()).getInventory().getContents());
ArrayList<ItemStack> 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) {
Expand All @@ -229,21 +229,21 @@ public boolean isSimilar(ItemStack toCompare) {
itemStack.getType().equals(Material.BUNDLE) &&
getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_BUNDLE_INVENTORY)) {
try {
ArrayList<ItemStack> contents1 = Lists.newArrayList(((BundleMeta) toCompareMeta).getItems()),
contents2 = Lists.newArrayList(((BundleMeta) itemStackMeta).getItems());
ArrayList<ItemStack> 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) {
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/org/shanerx/tradeshop/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,22 +481,24 @@ public ArrayList<ItemStack> getItems(Inventory inventory, List<ShopItemStack> 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;
}
Expand Down

0 comments on commit 33fcf6c

Please sign in to comment.