Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
- Prevents and forcefully removes shops using Shulker Boxes as a cost when the storage inventory is a Shulker Box
- Moved Shulker Inventory checking into ShopItemStack#isSimilar
- Improved Shulker Inventory checking
- Fix for #93
  • Loading branch information
KillerOfPie committed Nov 4, 2020
1 parent 80e93f1 commit 1643b5a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 57 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/shanerx/tradeshop/commands/CommandRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ public void setCost() {
return;
}

if (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 @@ -511,6 +516,11 @@ public void addCost() {
return;
}

if (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
1 change: 1 addition & 0 deletions src/main/java/org/shanerx/tradeshop/enumys/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public enum Message {
FULL_AMOUNT(MessageSectionKeys.UNUSED, "&cYou must have &e{AMOUNT} &cof a single type of &e{ITEM}&c!", "\\Unused\\"),
HELD_EMPTY(MessageSectionKeys.NONE, "&eYou are currently holding nothing.", "Text to display when the player is not holding anything"),
ILLEGAL_ITEM(MessageSectionKeys.NONE, "&cYou cannot use one or more of those items in shops.", "Text to display when a shop failed creation due to an illegal item "),
NO_SHULKER_COST(MessageSectionKeys.NONE, "&cYou cannot add a Shulker Box as a cost when the shop uses it for storage.", "Text to display when a shop failed creation due to using a shulker box as cost when the shop uses it for storage: "),
INSUFFICIENT_ITEMS(MessageSectionKeys.NONE, "&cYou do not have &e{AMOUNT} {ITEM}&c!", "Text to display when the player does not have enough items:"),
INVALID_ARGUMENTS(MessageSectionKeys.NONE, "&eTry &6/tradeshop help &eto display help!", "Text to display when invalid arguments are submitted through the \"/tradeshop\" command:"),
INVALID_SIGN(MessageSectionKeys.UNUSED, "&cInvalid sign format!", "\\Unused\\"),
Expand Down
34 changes: 13 additions & 21 deletions src/main/java/org/shanerx/tradeshop/objects/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ 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 @@ -469,7 +470,8 @@ public void addCost(ItemStack newItem) {
}
}

items.forEach((ItemStack iS) -> cost.add(new ShopItemStack(iS)));
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"));

saveShop();
updateSign();
Expand Down Expand Up @@ -625,6 +627,7 @@ public void fixAfterLoad() {
shopLoc.stringToWorld();
if (!shopType.isITrade() && chestLoc != null)
chestLoc.stringToWorld();
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 @@ -984,7 +987,6 @@ public ShopRole checkRole(UUID uuidToCheck) {
* Updates the number of trades the shop can make
*/
public void updateFullTradeCount() {
Utils utils = new Utils();
if (!hasStorage() || !hasProduct())
availableTrades = 0;

Expand All @@ -996,25 +998,14 @@ public void updateFullTradeCount() {

for (ShopItemStack item : getProduct()) {
totalCount += item.getItemStack().getAmount();
if (item.getItemStack().getType().name().endsWith("SHULKER_BOX")) {
for (ItemStack itm : clone.getStorageContents()) {
if (itm != null && itm.getType().name().endsWith("SHULKER_BOX")) {
if (utils.compareShulkers(itm, item.getItemStack())) {
clone.removeItem(itm);
currentCount++;
}
}
}
} else {
int traded;
for (ItemStack storageItem : clone.getStorageContents()) {
if (storageItem != null && item.isSimilar(storageItem)) {
traded = Math.min(storageItem.getAmount(), item.getItemStack().getMaxStackSize());

storageItem.setAmount(traded);
clone.removeItem(storageItem);
currentCount += traded;
}
int traded;
for (ItemStack storageItem : clone.getStorageContents()) {
if (storageItem != null && item.isSimilar(storageItem)) {
traded = Math.min(storageItem.getAmount(), item.getItemStack().getMaxStackSize());

storageItem.setAmount(traded);
clone.removeItem(storageItem);
currentCount += traded;
}
}
}
Expand Down Expand Up @@ -1046,6 +1037,7 @@ 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"));
cost = updatedCostList;
saveShop();
updateSign();
Expand Down
53 changes: 30 additions & 23 deletions src/main/java/org/shanerx/tradeshop/objects/ShopItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,37 @@ public boolean isSimilar(ItemStack toCompare) {
toCompareBookMeta = toCompare.hasItemMeta() && toCompare.getItemMeta() instanceof BookMeta ? ((BookMeta) toCompareMeta) : null;


boolean useMeta = itemStack.hasItemMeta() == toCompare.hasItemMeta() && itemStack != null, useBookMeta = itemStackBookMeta != null && toCompareBookMeta != null;
boolean useMeta = itemStack.hasItemMeta() == toCompare.hasItemMeta() && itemStackMeta != null, useBookMeta = itemStackBookMeta != null && toCompareBookMeta != null;

debugger.log("itemstack isBookMeta: " + (itemStackBookMeta != null), DebugLevels.ITEM_COMPARE);
debugger.log("toCompare isBookMeta: " + (toCompareBookMeta != null), DebugLevels.ITEM_COMPARE);
debugger.log("itemstack useMeta: " + useMeta, DebugLevels.ITEM_COMPARE);
debugger.log("toCompare useMeta: " + useMeta, DebugLevels.ITEM_COMPARE);

// If compareShulkerInventory is on
if (itemStack.getType().toString().endsWith("SHULKER_BOX")) {
if (compareShulkerInventory) {
try {
ArrayList<ItemStack> contents1 = Lists.newArrayList(((ShulkerBox) ((BlockStateMeta) toCompareMeta).getBlockState()).getInventory().getContents()),
contents2 = Lists.newArrayList(((ShulkerBox) ((BlockStateMeta) itemStackMeta).getBlockState()).getInventory().getContents());

contents1.removeIf(Objects::isNull);
contents2.removeIf(Objects::isNull);

if (contents1.isEmpty() != contents2.isEmpty())
return false;

for (ItemStack itm : contents2) {
if (!contents1.remove(itm))
return false;
}

if (!contents1.isEmpty())
return false;

} catch (ClassCastException ex) {
return false;
}
}
}

// If compareDurability is on
if (compareDurability > -1 && compareDurability < 3 && useMeta) {
Expand Down Expand Up @@ -379,26 +406,6 @@ public boolean isSimilar(ItemStack toCompare) {
return !itemStackMeta.hasAttributeModifiers() || Objects.equals(itemStackMeta.getAttributeModifiers(), toCompareMeta.getAttributeModifiers());
}

// If compareShulkerInventory is on
if (compareShulkerInventory && useMeta) {
if (!itemStack.getType().toString().toUpperCase().endsWith("SHULKER_BOX") || !toCompare.getType().toString().toUpperCase().endsWith("SHULKER_BOX"))
return false;

try {
ArrayList<ItemStack> contents1 = Lists.newArrayList(((ShulkerBox) ((BlockStateMeta) toCompare.clone().getItemMeta()).getBlockState()).getInventory().getContents());
ShulkerBox shulker2 = (ShulkerBox) ((BlockStateMeta) toCompare.clone().getItemMeta()).getBlockState();

for (ItemStack itm : shulker2.getInventory().getContents()) {
contents1.remove(itm);
}

if (!contents1.isEmpty())
return false;

} catch (ClassCastException ex) {
return false;
}
}
return true;
}

Expand Down
25 changes: 12 additions & 13 deletions src/main/java/org/shanerx/tradeshop/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,17 @@

package org.shanerx.tradeshop.utils;

import com.google.common.collect.Lists;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.ShulkerBox;
import org.bukkit.block.Sign;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.MaterialData;
import org.bukkit.plugin.PluginDescriptionFile;
Expand Down Expand Up @@ -399,10 +396,10 @@ public ExchangeStatus canExchangeAll(Shop shop, Inventory playerInv, int multipl

if (shop.getShopType() == ShopType.ITRADE) { //ITrade trade

//Method to find Cost items in player inventory and add to cost array
costItems = getItems(playerInventory, shop.getCost(), multiplier);
//Method to find Cost items in player inventory and add to cost array
costItems = getItems(playerInventory, shop.getCost(), multiplier);

if (costItems != null) {
if (!costItems.isEmpty()) {
if (costItems.get(0) == null) {
return ExchangeStatus.PLAYER_NO_COST;
}
Expand Down Expand Up @@ -487,16 +484,17 @@ 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.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("|"));
//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);
//debugger.log("ShopTradeListener > clone Contents: " + contents.toString(), DebugLevels.TRADE);
if (compareShulkers(itm, item.getItemStack())) {
clone.removeItem(itm);
ret.add(itm);
Expand All @@ -509,6 +507,7 @@ public ArrayList<ItemStack> getItems(Inventory inventory, List<ShopItemStack> it
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);
Expand Down Expand Up @@ -543,7 +542,7 @@ public ArrayList<ItemStack> getItems(Inventory inventory, List<ShopItemStack> it

if (currentCount >= totalCount) break;
}
}
//}

if (currentCount < totalCount) {
debugger.log("ShopTradeListener > TotalCount: " + totalCount, DebugLevels.TRADE);
Expand All @@ -558,7 +557,7 @@ public ArrayList<ItemStack> getItems(Inventory inventory, List<ShopItemStack> it
return ret;
}

public boolean compareShulkers(ItemStack item1, ItemStack item2) {
/* public boolean compareShulkers(ItemStack item1, ItemStack item2) {
if (item1 == null || item2 == null)
return false;
Expand All @@ -576,5 +575,5 @@ public boolean compareShulkers(ItemStack item1, ItemStack item2) {
} catch (ClassCastException ex) {
return false;
}
}
}*/
}

0 comments on commit 1643b5a

Please sign in to comment.