Skip to content

Commit

Permalink
Bug fixes and minor changes
Browse files Browse the repository at this point in the history
- Added ITRADE_NO_COST_TEXT setting for when iTrade shops trade without a Cost, replaces the {AMOUNT} {NAME}
- Added Setting warning to not exceed 6 Multitrade as it can potentially cause errors with iTrade shops. Not blocking outright as it probably wont be a problem unless the resulting amount of items take up more the 54 slots...which i guess wouldn't fit on the player anyways...
- Added getCostItemStacks and getProductItemStacks to shop returning ItemStacks instead of ShopItemStacks
- Changed getCost and getProduct to return clones instead of the actual objects to prevent accidental changes and fix iTrade interstack bug
- Cleaned up if statement repetition in ShopProtectionListener
- Fixed trade multiplier not working for iTrade shops
  • Loading branch information
KillerOfPie committed Nov 22, 2021
1 parent fa31d81 commit e0bfee8
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 36 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/shanerx/tradeshop/enumys/Setting.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public enum Setting {
// ^ Multi Trade
ALLOW_MULTI_TRADE(SettingSectionKeys.GLOBAL_MULTI_TRADE, "enable", true, "Should we allow multi trades with shift + click (true/false)"),
MULTI_TRADE_DEFAULT(SettingSectionKeys.GLOBAL_MULTI_TRADE, "default-multi", 2, "Default multiplier for trades using shift + click"),
MULTI_TRADE_MAX(SettingSectionKeys.GLOBAL_MULTI_TRADE, "max-multi", 6, "Maximum amount a player can set their multiplier to, reset upon leaving", "\n"),
MULTI_TRADE_MAX(SettingSectionKeys.GLOBAL_MULTI_TRADE, "max-multi", 6, "Maximum amount a player can set their multiplier to. Not recommended to set any higher than 6 as this can cause bugs with iTrade Shops", "\n"),

// Shop Options
MAX_SHOP_USERS(SettingSectionKeys.SHOP_OPTIONS, "max-shop-users", 5, "Maximum users(Managers/Members) a shop can have"),
Expand All @@ -94,7 +94,8 @@ public enum Setting {
// ITrade Shop Options
ITRADESHOP_OWNER(SettingSectionKeys.ITRADE_SHOP_OPTIONS, "owner", "Server Shop", "Name to put on the bottom of iTrade signs"),
ITRADESHOP_HEADER(SettingSectionKeys.ITRADE_SHOP_OPTIONS, "header", "iTrade", "The header that appears at the top of the shop signs, this is also what the player types to create the sign"),
ITRADESHOP_EXPLODE(SettingSectionKeys.ITRADE_SHOP_OPTIONS, "allow-explode", false, "Can explosions damage the shop sign (true/false)", "\n"),
ITRADESHOP_EXPLODE(SettingSectionKeys.ITRADE_SHOP_OPTIONS, "allow-explode", false, "Can explosions damage the shop sign (true/false)", ""),
ITRADESHOP_NO_COST_TEXT(SettingSectionKeys.ITRADE_SHOP_OPTIONS, "no-cost-text", "nothing", "What text should be used for successful trades when no cost is present", "\n"),

// BiTrade Shop Options
BITRADESHOP_HEADER(SettingSectionKeys.BITRADE_SHOP_OPTIONS, "header", "BiTrade", "The header that appears at the top of the shop signs, this is also what the player types to create the sign"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ public void onInventoryMoveItem(InventoryMoveItemEvent event) {
return;
}

if (event.isCancelled()) {
return;
} else if (event instanceof HopperShopAccessEvent) {
return;
} else if (!event.getInitiator().getType().equals(InventoryType.HOPPER)) {
if (event.isCancelled() ||
event instanceof HopperShopAccessEvent ||
!event.getInitiator().getType().equals(InventoryType.HOPPER)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public void onBlockInteract(PlayerInteractEvent e) {
return;
case INCOMPLETE:
Message.SHOP_EMPTY.sendMessage(buyer);
buyer.sendMessage("incomplete?");
case OUT_OF_STOCK:
if (shop.getShopType() == ShopType.BITRADE && e.getAction() == Action.LEFT_CLICK_BLOCK) {
Message.SHOP_INSUFFICIENT_ITEMS.sendMessage(buyer, new Tuple<>("{ITEM}", costName), new Tuple<>("{AMOUNT}", String.valueOf(amountCost)));
Expand Down Expand Up @@ -191,12 +192,21 @@ public void onBlockInteract(PlayerInteractEvent e) {
owner = "-Unknown-";

if (tradeAllItems(shop, multiplier, e, buyer)) {
Message.ON_TRADE.sendMessage(buyer,
new Tuple<>("{AMOUNT1}", String.valueOf(amountProduct)),
new Tuple<>("{AMOUNT2}", String.valueOf(amountCost)),
new Tuple<>("{ITEM1}", productName.toLowerCase()),
new Tuple<>("{ITEM2}", costName.toLowerCase()),
new Tuple<>("{SELLER}", shop.getShopType().isITrade() ? Setting.ITRADESHOP_OWNER.getString() : owner));
if (amountCost != 0) {
Message.ON_TRADE.sendMessage(buyer,
new Tuple<>("{AMOUNT1}", String.valueOf(amountProduct)),
new Tuple<>("{AMOUNT2}", String.valueOf(amountCost)),
new Tuple<>("{ITEM1}", productName.toLowerCase()),
new Tuple<>("{ITEM2}", costName.toLowerCase()),
new Tuple<>("{SELLER}", shop.getShopType().isITrade() ? Setting.ITRADESHOP_OWNER.getString() : owner));
} else {
Message.ON_TRADE.sendMessage(buyer,
new Tuple<>("{AMOUNT1}", String.valueOf(amountProduct)),
new Tuple<>("{AMOUNT2} ", ""), //Also replaces the extra space
new Tuple<>("{ITEM1}", productName.toLowerCase()),
new Tuple<>("{ITEM2}", Setting.ITRADESHOP_NO_COST_TEXT.getString()),
new Tuple<>("{SELLER}", shop.getShopType().isITrade() ? Setting.ITRADESHOP_OWNER.getString() : owner));
}
}

shop.updateSign();
Expand Down Expand Up @@ -226,8 +236,19 @@ private boolean tradeAllItems(Shop shop, int multiplier, PlayerInteractEvent eve
}
}

for (ShopItemStack item : shop.getProduct()) {
playerInventory.addItem(item.getItemStack());
Inventory iTradeVirtualInventory = Bukkit.createInventory(null, Math.min((int) (Math.ceil(shop.getProduct().size() / 9.0) * 9) * multiplier, 54));
while (iTradeVirtualInventory.firstEmpty() != -1) {
for (ItemStack item : shop.getProductItemStacks()) {
item.setAmount(item.getMaxStackSize());
iTradeVirtualInventory.addItem(item);
}
}

productItems = getItems(iTradeVirtualInventory, shop.getProduct(), multiplier);

for (ItemStack item : productItems) {
playerInventory.addItem(item);
buyer.sendMessage("Item Traded: \n" + item.toString());
}

Bukkit.getPluginManager().callEvent(new PlayerSuccessfulTradeEvent(buyer, costItems, productItems, shop, event.getClickedBlock(), event.getBlockFace()));
Expand Down Expand Up @@ -274,6 +295,7 @@ private boolean tradeAllItems(Shop shop, int multiplier, PlayerInteractEvent eve
Message.SHOP_INSUFFICIENT_ITEMS.sendMessage(buyer,
new Tuple<>("{ITEM}", item.hasItemMeta() && item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : item.getType().toString()),
new Tuple<>("{AMOUNT}", String.valueOf(item.getAmount() * multiplier)));
buyer.sendMessage("3");
return false;
}

Expand Down
60 changes: 48 additions & 12 deletions src/main/java/org/shanerx/tradeshop/objects/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,6 @@ public void setShopType(ShopType newType) {
shopType = newType;
}

/**
* returns the cost item
*
* @return Cost ItemStack List
*/
public List<ShopItemStack> getCost() {
return cost;
}

/**
* Sets the cost item
*
Expand Down Expand Up @@ -610,12 +601,51 @@ public void addProduct(ItemStack newItem) {
}

/**
* Returns the product item
* Returns the product items
*
* @return Product ShopItemStack List
*/
public List<ShopItemStack> getProduct() {
List<ShopItemStack> ret = new ArrayList<>();
for (ShopItemStack itm : product)
ret.add(itm.clone());
return ret;
}

/**
* Returns the product items
*
* @return Product ItemStack List
*/
public List<ShopItemStack> getProduct() {
return product;
public List<ItemStack> getProductItemStacks() {
List<ItemStack> ret = new ArrayList<>();
for (ShopItemStack itm : product)
ret.add(itm.getItemStack().clone());
return ret;
}

/**
* returns the cost item
*
* @return Cost ItemStack List
*/
public List<ShopItemStack> getCost() {
List<ShopItemStack> ret = new ArrayList<>();
for (ShopItemStack itm : cost)
ret.add(itm.clone());
return ret;
}

/**
* Returns the cost items
*
* @return Cost ItemStack List
*/
public List<ItemStack> getCostItemStacks() {
List<ItemStack> ret = new ArrayList<>();
for (ShopItemStack itm : cost)
ret.add(itm.getItemStack().clone());
return ret;
}

/**
Expand Down Expand Up @@ -689,6 +719,12 @@ public void fixAfterLoad() {
chestLoc.stringToWorld();
cost.removeIf(item -> item.getItemStack().getType().toString().endsWith("SHULKER_BOX") && getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX"));
}

/* TODO Fix this after 2.4
Removed since this ends up running getItems everytime a hopper trys to access a shop.
Runs up to 3 times per second per hopper under a tradeshop with items inside
Removing this caused errors with shops not being updated properly and causing fail messages during trades
*/
if (getShopSign() != null)
updateSign();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public ShopItemStack(ItemStack itemStack) {
toBase64();
}

public ShopItemStack(ItemStack itemStack, HashMap<ShopItemStackSettingKeys, ObjectHolder<?>> settingMap) {
this.itemStack = itemStack;
this.shopSettings = settingMap;
buildMap();
toBase64();
}

public ShopItemStack(String itemStackB64) {
this.itemStackB64 = itemStackB64;
shopSettings = new HashMap<>();
Expand Down Expand Up @@ -145,7 +152,7 @@ private void buildMap() {
}

public ShopItemStack clone() {
return new ShopItemStack(itemStackB64, shopSettings);
return new ShopItemStack(itemStack.clone(), shopSettings);
}

public boolean setShopSettings(ShopItemStackSettingKeys key, ObjectHolder<?> value) {
Expand Down
26 changes: 18 additions & 8 deletions src/main/java/org/shanerx/tradeshop/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,24 @@ public ExchangeStatus canExchangeAll(Shop shop, Inventory playerInv, int multipl
}
}

for (ShopItemStack item : shop.getProduct()) {
if (!playerInventory.addItem(item.getItemStack()).isEmpty()) {
return ExchangeStatus.PLAYER_NO_SPACE;
}
}
Inventory iTradeVirtualInventory = Bukkit.createInventory(null, Math.min((int) (Math.ceil(shop.getProduct().size() / 9.0) * 9) * multiplier, 54));
while (iTradeVirtualInventory.firstEmpty() != -1) {
for (ItemStack item : shop.getProductItemStacks()) {
item.setAmount(item.getMaxStackSize());
iTradeVirtualInventory.addItem(item);
}
}

productItems = getItems(iTradeVirtualInventory, shop.getProduct(), multiplier);

for (ItemStack item : productItems) {
if (!playerInventory.addItem(item).isEmpty()) {
return ExchangeStatus.PLAYER_NO_SPACE;
}
}

return ExchangeStatus.SUCCESS; //Successfully completed trade
} else if (shop.getShopType() == ShopType.BITRADE && action == Action.LEFT_CLICK_BLOCK) { //BiTrade Reversed Trade
return ExchangeStatus.SUCCESS; //Successfully completed trade
} else if (shop.getShopType() == ShopType.BITRADE && action == Action.LEFT_CLICK_BLOCK) { //BiTrade Reversed Trade

//Method to find Cost items in player inventory and add to cost array
costItems = getItems(playerInventory, shop.getProduct(), multiplier); //Reverse BiTrade, Product is Cost
Expand Down Expand Up @@ -474,7 +484,7 @@ public ArrayList<ItemStack> getItems(Inventory inventory, List<ShopItemStack> it
debugger.log("ShopTradeListener > Inventory Location Being Searched: " + (inventory.getLocation() != null ? inventory.getLocation().toString() : "null"), DebugLevels.TRADE);

for (ShopItemStack item : items) {
totalCount += item.getItemStack().getAmount() * multiplier;
totalCount += item.getAmount() * multiplier;
int count = item.getItemStack().getAmount() * multiplier;

debugger.log("ShopTradeListener > Item Material Being Searched for: " + item.getItemStack().getType().name(), DebugLevels.TRADE);
Expand Down

0 comments on commit e0bfee8

Please sign in to comment.