Skip to content

Commit

Permalink
Fix for EnchantedBooks not being compared properly
Browse files Browse the repository at this point in the history
- Added compare for EnchantmentStorageMeta MetaType
- Minor tweaks and Variable name fixes
  • Loading branch information
KillerOfPie committed Nov 8, 2021
1 parent 06fed29 commit 7824f0c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 40 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/shanerx/tradeshop/enumys/DebugLevels.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public enum DebugLevels {
INVENTORY_CLOSE_NPE(5, Level.WARNING), // 16
ITEM_COMPARE(6, Level.WARNING), // 32
NAME_COMPARE(7, Level.WARNING), // 64
SHULKERS_SUCK(8, Level.WARNING) // 128
SHULKERS_SUCK(8, Level.WARNING), // 128
ENCHANT_CHECKS(9, Level.WARNING) // 256

;

Expand Down
52 changes: 33 additions & 19 deletions src/main/java/org/shanerx/tradeshop/objects/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -862,29 +862,43 @@ public ShopStatus getStatus() {
*
* @return true if shop opened
*/
public ShopStatus setOpen() {
setStatus(ShopStatus.OPEN);
updateStatus();
public ShopStatus setOpen() {
setStatus(ShopStatus.OPEN);
updateStatus();

saveShop();
updateSign();
return status;
}
return status;
}

/**
* Automatically updates a shops status if it is not CLOSED
*/
public void updateStatus() {
if (!status.equals(ShopStatus.CLOSED)) {
if (!isMissingItems() && (chestLoc != null || shopType.equals(ShopType.ITRADE))) {
if (shopType.equals(ShopType.ITRADE) || (getChestAsSC() != null && getChestAsSC().hasStock(product)))
setStatus(ShopStatus.OPEN);
else
setStatus(ShopStatus.OUT_OF_STOCK);
} else {
setStatus(ShopStatus.INCOMPLETE);
}
}
/**
* Checks if the shop has sufficient product stock to make a trade
*/
public boolean hasProductStock() {
return !shopType.isITrade() && hasProduct() && getChestAsSC() != null && getChestAsSC().hasStock(product);
}

/**
* Checks if the shop has sufficient cost stock to make a trade(Use for BiTrade)
*/
public boolean hasCostStock() {
return !shopType.isITrade() && hasCost() && getChestAsSC() != null && getChestAsSC().hasStock(cost);
}

/**
* Automatically updates a shops status if it is not CLOSED
*/
public void updateStatus() {
if (!status.equals(ShopStatus.CLOSED)) {
if (!isMissingItems() && (chestLoc != null || shopType.isITrade())) {
if (shopType.isITrade() || hasProductStock() || (shopType.isBiTrade() && hasCostStock()))
setStatus(ShopStatus.OPEN);
else
setStatus(ShopStatus.OUT_OF_STOCK);
} else {
setStatus(ShopStatus.INCOMPLETE);
}
}
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/shanerx/tradeshop/objects/ShopChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,20 @@ public BlockState getBlockState() {
}

public Inventory getInventory() {
try {
return ((InventoryHolder) chest.getState()).getInventory();
} catch (ClassCastException | NullPointerException ex) {
}
try {
return ((InventoryHolder) chest.getState()).getInventory();
} catch (ClassCastException | NullPointerException ex) {
}

return null;
}

public boolean hasStock(List<ShopItemStack> product) {
return product.size() > 0 && getItems(getInventory(), product, 1).get(0) != null;
}
public boolean hasStock(List<ShopItemStack> itemToCheck) {
return itemToCheck.size() > 0 && getItems(getInventory(), itemToCheck, 1).get(0) != null;
}

public void loadFromName() {
if (isShopChest(chest)) {
if (isShopChest(chest)) {
String[] name = ((Container) chest.getState()).getPersistentDataContainer().get(plugin.getSignKey(), PersistentDataType.STRING)
.replaceAll("Sign:", "Sign" + titleSeparator).replaceAll("Owner:", "Owner" + titleSeparator)
.split(sectionSeparator);
Expand Down
37 changes: 27 additions & 10 deletions src/main/java/org/shanerx/tradeshop/objects/ShopItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public boolean isSimilar(ItemStack toCompare) {
BookMeta itemStackBookMeta = itemStack.hasItemMeta() && itemStack.getItemMeta() instanceof BookMeta ? ((BookMeta) itemStackMeta) : null,
toCompareBookMeta = toCompare.hasItemMeta() && toCompare.getItemMeta() instanceof BookMeta ? ((BookMeta) toCompareMeta) : null;

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

debugger.log("itemstack useMeta: " + useMeta, DebugLevels.ITEM_COMPARE);
Expand Down Expand Up @@ -301,16 +301,33 @@ public boolean isSimilar(ItemStack toCompare) {

// If compareEnchantments is on
if (getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_ENCHANTMENTS) && useMeta) {
// Return False if hasEnchantments differs (one has one doesn't)
if (itemStackMeta.hasEnchants() != toCompareMeta.hasEnchants()) {
debugger.log("itemStackMeta hasEnchants: " + itemStackMeta.hasEnchants(), DebugLevels.ITEM_COMPARE);
debugger.log("toCompareMeta hasEnchants: " + toCompareMeta.hasEnchants(), DebugLevels.ITEM_COMPARE);
return false;
}
if (itemStackMeta instanceof EnchantmentStorageMeta && toCompareMeta instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta itemStackEnchantmentStorageMeta = (EnchantmentStorageMeta) itemStackMeta,
toCompareEnchantmentStorageMeta = (EnchantmentStorageMeta) toCompareMeta;

// Return False if itemStack hasEnchantments && Enchant maps are not equal
if (itemStackMeta.hasEnchants() && !itemStackMeta.getEnchants().equals(toCompareMeta.getEnchants()))
return false;
debugger.log("itemStackEnchantmentStorageMeta Enchants: " + itemStackEnchantmentStorageMeta.getStoredEnchants(), DebugLevels.ENCHANT_CHECKS);
debugger.log("toCompareEnchantmentStorageMeta Enchants: " + toCompareEnchantmentStorageMeta.getStoredEnchants(), DebugLevels.ENCHANT_CHECKS);

// Return False if hasEnchantments differs (one has one doesn't)
if (itemStackEnchantmentStorageMeta.hasStoredEnchants() != toCompareEnchantmentStorageMeta.hasStoredEnchants())
return false;

// Return False if itemStack hasEnchantments && Enchant maps are not equal
if (itemStackEnchantmentStorageMeta.hasStoredEnchants() && !itemStackEnchantmentStorageMeta.getStoredEnchants().equals(toCompareEnchantmentStorageMeta.getStoredEnchants()))
return false;
} else {

// Return False if hasEnchantments differs (one has one doesn't)
if (itemStackMeta.hasEnchants() != toCompareMeta.hasEnchants()) {
debugger.log("itemStackMeta hasEnchants: " + itemStackMeta.hasEnchants(), DebugLevels.ITEM_COMPARE);
debugger.log("toCompareMeta hasEnchants: " + toCompareMeta.hasEnchants(), DebugLevels.ITEM_COMPARE);
return false;
}

// Return False if itemStack hasEnchantments && Enchant maps are not equal
if (itemStackMeta.hasEnchants() && !itemStackMeta.getEnchants().equals(toCompareMeta.getEnchants()))
return false;
}
}

// If compareName is on
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/shanerx/tradeshop/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ public ArrayList<ItemStack> getItems(Inventory inventory, List<ShopItemStack> it

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

debugger.log("ShopTradeListener > Item Material Being Searched for: " + item.getItemStack().getType().name(), DebugLevels.TRADE);
debugger.log("ShopTradeListener > Item count: " + count, DebugLevels.TRADE);
Expand All @@ -485,7 +485,7 @@ public ArrayList<ItemStack> getItems(Inventory inventory, List<ShopItemStack> it
boolean isSimilar = item.isSimilar(storageItem.clone());
if (isSimilar) {

traded = Math.min(Math.min(storageItem.getAmount(), item.getItemStack().getMaxStackSize()), count);
int traded = Math.min(Math.min(storageItem.getAmount(), item.getItemStack().getMaxStackSize()), count);

storageItem.setAmount(traded);

Expand Down

0 comments on commit 7824f0c

Please sign in to comment.