From 5f839a52dbba22ec016cbcb82828eb1550448000 Mon Sep 17 00:00:00 2001 From: Sotr Date: Sat, 25 Jan 2020 20:18:51 +0700 Subject: [PATCH] New matcher system with item matching fixed Former-commit-id: fb3867fde4cdc34638f994bb3be4a634eda057d5 --- .../quickshop/Shop/ArmorStandDisplayItem.java | 2 +- .../quickshop/Shop/ContainerShop.java | 14 ++--- .../maxgamer/quickshop/Shop/DisplayItem.java | 2 +- .../org/maxgamer/quickshop/Shop/Info.java | 4 +- .../quickshop/Shop/InventoryPreview.java | 2 +- .../quickshop/Shop/RealDisplayItem.java | 2 +- .../maxgamer/quickshop/Util/ItemMatcher.java | 60 ++++++++----------- .../org/maxgamer/quickshop/Util/Util.java | 2 +- src/main/resources/config.yml | 7 +-- 9 files changed, 43 insertions(+), 52 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java index ec98a865c..350377652 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java @@ -52,7 +52,7 @@ public class ArmorStandDisplayItem implements DisplayItem { ArmorStandDisplayItem(@NotNull Shop shop) { this.shop = shop; - this.originalItemStack = shop.getItem().clone(); + this.originalItemStack = new ItemStack(shop.getItem()); this.originalItemStack.setAmount(1); } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java index 5612314da..74247649a 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java @@ -102,7 +102,7 @@ public class ContainerShop implements Shop { this.location = location; this.price = price; this.moderator = moderator; - this.item = item.clone(); + this.item = new ItemStack(item); this.plugin = QuickShop.instance; this.item.setAmount(1); this.shopType = type; @@ -260,7 +260,7 @@ public class ContainerShop implements Shop { /** @return The durability of the item */ @Override public short getDurability() { - return (short) ((Damageable) Objects.requireNonNull(this.item.getItemMeta())).getDamage(); + return (short) ((Damageable) this.item.getItemMeta()).getDamage(); } /** @return The name of the player who owns the shop. */ @@ -314,7 +314,7 @@ public class ContainerShop implements Shop { ItemStack[] contents = p.getInventory().getContents(); for (int i = 0; amount1 > 0 && i < contents.length; i++) { ItemStack stack = contents[i]; - if (stack == null) { + if (stack == null || stack.getType() == Material.AIR) { continue; // No item } if (matches(stack)) { @@ -347,7 +347,7 @@ public class ContainerShop implements Shop { ItemStack item = playerContents[i]; if (item != null && this.matches(item)) { // Copy it, we don't want to interfere - item = item.clone(); + item = new ItemStack(item); // Amount = total, item.getAmount() = how many items in the // stack int stackSize = Math.min(amount1, item.getAmount()); @@ -526,7 +526,7 @@ public class ContainerShop implements Shop { ArrayList floor = new ArrayList<>(5); Inventory pInv = p.getInventory(); if (this.isUnlimited()) { - ItemStack item = this.item.clone(); + ItemStack item = new ItemStack(this.item); while (amount > 0) { int stackSize = Math.min(amount, this.item.getMaxStackSize()); item.setAmount(stackSize); @@ -538,9 +538,9 @@ public class ContainerShop implements Shop { for (int i = 0; amount > 0 && i < chestContents.length; i++) { // Can't clone it here, it could be null ItemStack item = chestContents[i]; - if (item != null && this.matches(item)) { + if (item != null && item.getType() != Material.AIR && this.matches(item)) { // Copy it, we don't want to interfere - item = item.clone(); + item = new ItemStack(item); // Amount = total, item.getAmount() = how many items in the // stack int stackSize = Math.min(amount, item.getAmount()); diff --git a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java index ed9eb2841..6473486b7 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java @@ -133,7 +133,7 @@ public interface DisplayItem { * @return New itemStack with protect flag. */ static ItemStack createGuardItemStack(@NotNull ItemStack itemStack, @NotNull Shop shop) { - itemStack = itemStack.clone(); + itemStack = new ItemStack(itemStack); itemStack.setAmount(1); ItemMeta iMeta = itemStack.getItemMeta(); if (QuickShop.instance.getConfig().getBoolean("shop.display-item-use-name")) { diff --git a/src/main/java/org/maxgamer/quickshop/Shop/Info.java b/src/main/java/org/maxgamer/quickshop/Shop/Info.java index 513a780fd..782422ba0 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/Info.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/Info.java @@ -46,7 +46,7 @@ public class Info { this.action = action; this.last = last; if (item != null) { - this.item = item.clone(); + this.item = new ItemStack(item); } } @@ -60,7 +60,7 @@ public class Info { this.action = action; this.last = last; if (item != null) { - this.item = item.clone(); + this.item = new ItemStack(item); } if (shop != null) { this.shop = shop.clone(); diff --git a/src/main/java/org/maxgamer/quickshop/Shop/InventoryPreview.java b/src/main/java/org/maxgamer/quickshop/Shop/InventoryPreview.java index a4837cb9a..56eae3e09 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/InventoryPreview.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/InventoryPreview.java @@ -52,7 +52,7 @@ public class InventoryPreview implements Listener { * @param player Target player. */ public InventoryPreview(@NotNull ItemStack itemStack, @NotNull Player player) { - this.itemStack = itemStack.clone(); + this.itemStack = new ItemStack(itemStack); this.player = player; if (Objects.requireNonNull(this.itemStack.getItemMeta()).hasLore()) { ItemMeta itemMeta = this.itemStack.getItemMeta(); diff --git a/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java index ce6816a31..923086a7f 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java @@ -54,7 +54,7 @@ public class RealDisplayItem implements DisplayItem { */ RealDisplayItem(@NotNull Shop shop) { this.shop = shop; - this.originalItemStack = shop.getItem().clone(); + this.originalItemStack = new ItemStack(shop.getItem()); this.originalItemStack.setAmount(1); // this.displayLoc = shop.getLocation().clone().add(0.5, 1.2, 0.5); diff --git a/src/main/java/org/maxgamer/quickshop/Util/ItemMatcher.java b/src/main/java/org/maxgamer/quickshop/Util/ItemMatcher.java index 6b2d63ca9..b4e3e5216 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/ItemMatcher.java +++ b/src/main/java/org/maxgamer/quickshop/Util/ItemMatcher.java @@ -46,7 +46,6 @@ public class ItemMatcher { * @return true if the itemstacks match. (Material, durability, enchants, name) */ public boolean matches(@Nullable ItemStack requireStack, @Nullable ItemStack givenStack) { - if (requireStack == givenStack) { return true; // Referring to the same thing, or both are null. } @@ -57,10 +56,10 @@ public class ItemMatcher { return false; // One of them is null (Can't be both, see above) } - requireStack = requireStack.clone(); - requireStack.setAmount(1); - givenStack = givenStack.clone(); - givenStack.setAmount(1); + //requireStack = requireStack.clone(); + //requireStack.setAmount(1); + //givenStack = givenStack.clone(); + //givenStack.setAmount(1); // if (plugin.getConfig().getBoolean("shop.strict-matches-check")) { // Util.debugLog("Execute strict match check..."); @@ -71,37 +70,22 @@ public class ItemMatcher { // } switch (plugin.getConfig().getInt("matcher.work-type")) { case 1: - return requireStack.isSimilar(givenStack); case 2: - return requireStack.equals(givenStack); + return requireStack.isSimilar(givenStack); + case 0: + default: + ; } - if (!typeMatches(requireStack, givenStack)) { + if (requireStack.getType() != givenStack.getType()) { Util.debugLog("Type not match."); return false; } - // if (requireStack.hasItemMeta() != givenStack.hasItemMeta()) { - // Util.debugLog("Meta not matched"); - // return false; - - if (requireStack.hasItemMeta()) { - if (!givenStack.hasItemMeta()) { - Util.debugLog("Meta not match."); - return false; - } - return itemMetaMatcher.matches(requireStack, givenStack); - } - - return true; - } - - private boolean typeMatches(ItemStack requireStack, ItemStack givenStack) { - return requireStack.getType().equals(givenStack.getType()); + return itemMetaMatcher.matches(requireStack, givenStack); } } - class ItemMetaMatcher { private boolean repaircost; private boolean attributes; @@ -202,27 +186,36 @@ class ItemMetaMatcher { private boolean repairCostMatches(ItemMeta required, ItemMeta test) { Util.debugLog("Matching: Repair Cost"); boolean requiredIs = required instanceof Repairable; + if (!canMatches(requiredIs, test instanceof Repairable)) { + return false; + } else if (!repaircost || !requiredIs) { + return true; + } + boolean requiredHas = requiredIs && ((Repairable) required).hasRepairCost(); - return repaircost ? (canMatches(requiredIs, test instanceof Repairable) - && canMatches(requiredHas, ((Repairable) test).hasRepairCost()) ? + return canMatches(requiredHas, ((Repairable) test).hasRepairCost()) ? (requiredHas ? ((Repairable) required).getRepairCost() == ((Repairable) test).getRepairCost() : true) : - false) - : true; + false; } private boolean potionMatches(ItemMeta required, ItemMeta test) { Util.debugLog("Matching: Potion"); boolean requiredIs = required instanceof PotionMeta; + if (!canMatches(requiredIs, test instanceof PotionMeta)) { + return false; + } else if (!potions || !requiredIs) { + return true; + } + boolean requiredHasColor = requiredIs && ((PotionMeta) required).hasColor(); boolean requiredHasCustomEffects = requiredIs && ((PotionMeta) required).hasCustomEffects(); - return potions ? (canMatches(requiredIs, test instanceof PotionMeta) - && canMatches(requiredHasColor, ((PotionMeta) test).hasColor()) + return canMatches(requiredHasColor, ((PotionMeta) test).hasColor()) && canMatches(requiredHasCustomEffects, ((PotionMeta) test).hasCustomEffects()) ? (requiredHasColor @@ -238,8 +231,7 @@ class ItemMetaMatcher { .equals(((PotionMeta) test).getBasePotionData()))) : - false) - : true; + false; } boolean matches(ItemStack requiredStack, ItemStack testStack) { diff --git a/src/main/java/org/maxgamer/quickshop/Util/Util.java b/src/main/java/org/maxgamer/quickshop/Util/Util.java index 7ccebd79b..2ebad8a6f 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/Util.java +++ b/src/main/java/org/maxgamer/quickshop/Util/Util.java @@ -183,7 +183,7 @@ public class Util { int items = 0; for (ItemStack iStack : inv.getStorageContents()) { //noinspection ConstantConditions - if (iStack == null) { + if (iStack == null || iStack.getType() == Material.AIR) { continue; } if (plugin.getItemMatcher().matches(item, iStack)) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index fd00525df..c17c01c72 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -319,10 +319,9 @@ effect: onclick: true #Item matcher, don't touch it if you didn't know how to use it. matcher: - #Matcher type - #0=QuickShop user-friendly matcher. - #1=Bukkit standard item matcher. - #2=Java equals matcher. + # Matcher type + #0= QuickShop item matcher with configurable options below. + #1= Bukkit item matcher, can be more accurate. work-type: 0 #For Item item: