diff --git a/src/main/java/org/maxgamer/quickshop/Util/ItemMatcher.java b/src/main/java/org/maxgamer/quickshop/Util/ItemMatcher.java index 6b2d63ca9..2926180ef 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/ItemMatcher.java +++ b/src/main/java/org/maxgamer/quickshop/Util/ItemMatcher.java @@ -1,28 +1,40 @@ /* - * This file is a part of project QuickShop, the name is ItemMatcher.java Copyright (C) Ghost_chu - * Copyright (C) Bukkit Commons Studio and contributors + * This file is a part of project QuickShop, the name is ItemMatcher.java + * Copyright (C) Ghost_chu + * Copyright (C) Bukkit Commons Studio and contributors * - * This program is free software: you can redistribute it and/or modify it under the terms of the - * GNU Lesser General Public License as published by the Free Software Foundation, either version 3 - * of the License, or (at your option) any later version. + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without - * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. * - * You should have received a copy of the GNU Lesser General Public License along with this program. - * If not, see . + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . */ package org.maxgamer.quickshop.Util; +import com.google.common.collect.Multimap; +import java.util.Arrays; +import java.util.List; +import java.util.Map; import java.util.Objects; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeModifier; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.inventory.meta.Repairable; +import org.bukkit.potion.PotionData; import org.jetbrains.annotations.Nullable; import org.maxgamer.quickshop.QuickShop; @@ -33,8 +45,9 @@ public class ItemMatcher { public ItemMatcher(QuickShop plugin) { this.plugin = plugin; - itemMetaMatcher = new ItemMetaMatcher( - Objects.requireNonNull(plugin.getConfig().getConfigurationSection("matcher.item"))); + itemMetaMatcher = + new ItemMetaMatcher( + Objects.requireNonNull(plugin.getConfig().getConfigurationSection("matcher.item"))); } /** @@ -52,8 +65,13 @@ public class ItemMatcher { } if (requireStack == null || givenStack == null) { - Util.debugLog("Match failed: A stack is null: " + "requireStack[" + requireStack - + "] givenStack[" + givenStack + "]"); + Util.debugLog( + "Match failed: A stack is null: " + + "requireStack[" + + requireStack + + "] givenStack[" + + givenStack + + "]"); return false; // One of them is null (Can't be both, see above) } @@ -62,13 +80,13 @@ public class ItemMatcher { givenStack = givenStack.clone(); givenStack.setAmount(1); - // if (plugin.getConfig().getBoolean("shop.strict-matches-check")) { - // Util.debugLog("Execute strict match check..."); - // return requireStack.equals(givenStack); - // } - // if(plugin.getConfig().getBoolean("matcher.use-bukkit-matcher")){ - // return givenStack.isSimilar(requireStack); - // } + // if (plugin.getConfig().getBoolean("shop.strict-matches-check")) { + // Util.debugLog("Execute strict match check..."); + // return requireStack.equals(givenStack); + // } + // if(plugin.getConfig().getBoolean("matcher.use-bukkit-matcher")){ + // return givenStack.isSimilar(requireStack); + // } switch (plugin.getConfig().getInt("matcher.work-type")) { case 1: return requireStack.isSimilar(givenStack); @@ -81,9 +99,9 @@ public class ItemMatcher { return false; } - // if (requireStack.hasItemMeta() != givenStack.hasItemMeta()) { - // Util.debugLog("Meta not matched"); - // return false; + // if (requireStack.hasItemMeta() != givenStack.hasItemMeta()) { + // Util.debugLog("Meta not matched"); + // return false; if (requireStack.hasItemMeta()) { if (!givenStack.hasItemMeta()) { @@ -101,7 +119,6 @@ public class ItemMatcher { } } - class ItemMetaMatcher { private boolean repaircost; private boolean attributes; @@ -112,7 +129,6 @@ class ItemMetaMatcher { private boolean itemflags; private boolean lores; private boolean potions; - private boolean bukkit; public ItemMetaMatcher(ConfigurationSection itemMatcherConfig) { this.damage = itemMatcherConfig.getBoolean("damage"); @@ -124,183 +140,269 @@ class ItemMetaMatcher { this.attributes = itemMatcherConfig.getBoolean("attributes"); this.itemflags = itemMatcherConfig.getBoolean("itemflags"); this.custommodeldata = itemMatcherConfig.getBoolean("custommodeldata"); - - this.bukkit = damage && repaircost && displayname && lores && enchs && potions && attributes - && itemflags && custommodeldata; - } - - private boolean canMatches(boolean a, boolean b) { - return a == b; } private boolean attributeModifiersMatches(ItemMeta required, ItemMeta test) { - Util.debugLog("Matching: Attribute Modifiers"); - return attributes ? - // For non-exists, the default is same as null. - Objects.equals(required.getAttributeModifiers(), test.getAttributeModifiers()) : true; + if (!this.attributes) { + return true; + } + // requireStack doen't need require must have AM, skipping.. + if (!required.hasAttributeModifiers()) { + return true; + } else { + // If require AM but hadn't, the item not matched. + if (!test.hasAttributeModifiers()) { + return false; + } + // Do not get modifier multiple times cause it use copyOf internally + Multimap mod2 = test.getAttributeModifiers(); + return required.getAttributeModifiers() + .entries() + .stream().allMatch(e -> mod2.containsEntry(e.getKey(), e.getValue())); + } } private boolean customModelDataMatches(ItemMeta required, ItemMeta test) { - Util.debugLog("Matching: Custom Model Data"); - boolean requiredHas = required.hasCustomModelData(); - return custommodeldata ? (canMatches(requiredHas, test.hasCustomModelData()) ? - - (requiredHas ? - required.getCustomModelData() == test.getCustomModelData() : true) : - - false) : true; + if (!this.custommodeldata) { + return true; + } + if (!required.hasCustomModelData()) { + return true; + } else { + if (!test.hasCustomModelData()) { + return false; + } + return required.getCustomModelData() == test.getCustomModelData(); + } } private boolean damageMatches(ItemMeta required, ItemMeta test) { - Util.debugLog("Matching: Damage"); - boolean requiredIs = required instanceof Damageable; - return damage ? (canMatches(requiredIs, test instanceof Damageable) ? - - (requiredIs ? ((Damageable) required).getDamage() == ((Damageable) test).getDamage() : true) - : - - false) : true; + if (!this.damage) { + return true; + } + if (!(required instanceof Damageable)) { + return true; //No damage need to check. + } else if (!(test instanceof Damageable)) { + return false; + } + Util.debugLog("Checking damage"); + Damageable requiredDamage = (Damageable) required; + Damageable testDamage = (Damageable) test; + // Check them damages, if givenDamage >= requireDamage, allow it. + return testDamage.getDamage() >= requiredDamage.getDamage(); } - private boolean displayNameMatches(ItemMeta required, ItemMeta test) { - Util.debugLog("Matching: Display Name"); - boolean requiredHas = required.hasDisplayName(); - return displayname ? (canMatches(requiredHas, test.hasDisplayName()) ? - - (requiredHas ? required.getDisplayName().equals(test.getDisplayName()) : true) : - - false) : true; + private boolean displayMatches(ItemMeta required, ItemMeta test) { + if (!this.displayname) { + return true; + } + Util.debugLog("Checking displayname"); + if (!required.hasDisplayName()) { + return true; + } else if (!test.hasDisplayName()) { + return false; + } + return required.getDisplayName().equals(test.getDisplayName()); } - private boolean enchantsMatches(ItemMeta required, ItemMeta test) { - Util.debugLog("Matching: Enchants"); - boolean requiredHas = required.hasEnchants(); - return enchs ? (canMatches(requiredHas, test.hasEnchants()) ? - - (requiredHas ? Util.mapMatches(required.getEnchants(), test.getEnchants()) : true) : - - false) : true; + private boolean enchMatches(ItemMeta required, ItemMeta test) { + if (!this.enchs) { + return true; + } + Util.debugLog("Checking enchantments"); + + if (required.hasEnchants()) { + if (!test.hasEnchants()) { + return false; + } + + Map enchMap1 = required.getEnchants(); + Map enchMap2 = test.getEnchants(); + return Util.mapMatches(enchMap1, enchMap2); + } + + // Enchantment books + if ((required instanceof EnchantmentStorageMeta)) { + if (!(test instanceof EnchantmentStorageMeta)) { + return false; + } + + Map stor1 = ((EnchantmentStorageMeta) required).getStoredEnchants(); + Map stor2 = ((EnchantmentStorageMeta) test).getStoredEnchants(); + return Util.mapMatches(stor1, stor2); + } + + return true; } - private boolean itemFlagsMatches(ItemMeta required, ItemMeta test) { - Util.debugLog("Matching: Item Flags"); - return itemflags ? required.getItemFlags().equals(test.getItemFlags()) : true; + private boolean itemFlagsMatches(ItemMeta meta1, ItemMeta meta2) { + if (!this.itemflags) { + return true; + } + Util.debugLog("Checking itemflags"); + if (meta1.getItemFlags().isEmpty()) { + return true; + } else { + if (meta2.getItemFlags().isEmpty()) { + return false; + } + return Arrays.deepEquals(meta1.getItemFlags().toArray(), meta2.getItemFlags().toArray()); + } } // We didn't touch the loresMatches because many plugin use this check item. - // Re: Did you mean this method? I don't think there is actually any plugin use this as a helper. - private boolean loreMatches(ItemMeta required, ItemMeta test) { - Util.debugLog("Matching: Lore"); - boolean requiredHas = required.hasLore(); - return lores ? (canMatches(requiredHas, test.hasLore()) ? - - (requiredHas ? required.getLore().equals(test.getLore()) : true) : - - false) : true; - } - - private boolean repairCostMatches(ItemMeta required, ItemMeta test) { - Util.debugLog("Matching: Repair Cost"); - boolean requiredIs = required instanceof Repairable; - boolean requiredHas = requiredIs && ((Repairable) required).hasRepairCost(); - return repaircost ? (canMatches(requiredIs, test instanceof Repairable) - && canMatches(requiredHas, ((Repairable) test).hasRepairCost()) ? - - (requiredHas - ? ((Repairable) required).getRepairCost() == ((Repairable) test).getRepairCost() - : true) - : - - false) - : true; - } - - private boolean potionMatches(ItemMeta required, ItemMeta test) { - Util.debugLog("Matching: Potion"); - boolean requiredIs = required instanceof PotionMeta; - boolean requiredHasColor = requiredIs && ((PotionMeta) required).hasColor(); - boolean requiredHasCustomEffects = requiredIs && ((PotionMeta) required).hasCustomEffects(); - - return potions ? (canMatches(requiredIs, test instanceof PotionMeta) - && canMatches(requiredHasColor, ((PotionMeta) test).hasColor()) - && canMatches(requiredHasCustomEffects, ((PotionMeta) test).hasCustomEffects()) ? - - (requiredHasColor - ? ((PotionMeta) required).getColor().equals(((PotionMeta) test).getColor()) - : - - (requiredHasCustomEffects - ? ((PotionMeta) required).getCustomEffects() - .equals(((PotionMeta) test).getCustomEffects()) - : ( - - ((PotionMeta) required).getBasePotionData()) - .equals(((PotionMeta) test).getBasePotionData()))) - : - - false) - : true; - } - - boolean matches(ItemStack requiredStack, ItemStack testStack) { - String method = bukkit ? "Bukkit" : "QuickShop"; - Util.debugLog("Matching item by method " + method + " @ " + requiredStack.getType() + ", " - + testStack.getType()); - - boolean result = matches0(requiredStack, testStack); - Util.debugLog("Matches result (" + method + "): " + String.valueOf(result).toUpperCase()); - return result; - } - - boolean matches0(ItemStack requiredStack, ItemStack testStack) { - if (bukkit) { - return requiredStack.isSimilar(testStack); - } - - boolean requiredHas = requiredStack.hasItemMeta(); - if (!canMatches(requiredHas, testStack.hasItemMeta())) { - return false; - } else if (!requiredHas) { + private boolean loresMatches(ItemMeta meta1, ItemMeta meta2) { + if (!this.lores) { return true; } + Util.debugLog("Checking lores"); + if (meta1.hasLore() != meta2.hasLore()) { + return false; + } + if (!meta1.hasLore()) { + return true; // No lores need to be checked. + } + List lores1 = meta1.getLore(); + List lores2 = meta2.getLore(); + return Arrays.deepEquals( + Objects.requireNonNull(lores1).toArray(), Objects.requireNonNull(lores2).toArray()); + } - ItemMeta requiredMeta = requiredStack.getItemMeta(); - ItemMeta testMeta = testStack.getItemMeta(); - - if (!damageMatches(requiredMeta, testMeta)) { + boolean matches(ItemStack requireStack, ItemStack givenStack) { + Util.debugLog("Begin the item matches checking..."); + if (requireStack.hasItemMeta() != givenStack.hasItemMeta()) { return false; } - if (!repairCostMatches(requiredMeta, testMeta)) { + if (!requireStack.hasItemMeta()) { + return true; // Passed check. no meta need to check. + } + ItemMeta meta1 = requireStack.getItemMeta(); + ItemMeta meta2 = givenStack.getItemMeta(); + if ((meta1 == null) != (meta2 == null)) { return false; } - if (!displayNameMatches(requiredMeta, testMeta)) { + if (meta1 == null) { + Util.debugLog("Pass"); + return true; // Both null... + } + if (!damageMatches(meta1, meta2)) { return false; } - if (!loreMatches(requiredMeta, testMeta)) { + if (!repaircostMatches(meta1, meta2)) { return false; } - if (!enchantsMatches(requiredMeta, testMeta)) { + if (!displayMatches(meta1, meta2)) { return false; } - if (!potionMatches(requiredMeta, testMeta)) { + if (!loresMatches(meta1, meta2)) { return false; } - if (!attributeModifiersMatches(requiredMeta, testMeta)) { + if (!enchMatches(meta1, meta2)) { return false; } - if (!itemFlagsMatches(requiredMeta, testMeta)) { + if (!potionMatches(meta1, meta2)) { + return false; + } + if (!attributeModifiersMatches(meta1, meta2)) { + return false; + } + if (!itemFlagsMatches(meta1, meta2)) { return false; } - try { - if (!customModelDataMatches(requiredMeta, testMeta)) { + if (!customModelDataMatches(meta1, meta2)) { return false; } } catch (NoSuchMethodError err) { - Util.debugLog("Ignored custom model data."); // Ignore, for 1.13 compatibility } - + Util.debugLog("Pass"); return true; } + + private boolean repaircostMatches(ItemMeta meta1, ItemMeta meta2) { + if (!this.repaircost) { + return true; + } + Util.debugLog("Checking the reportcost"); + if (!(meta1 instanceof Repairable)) { + return true; + } + if (!(meta2 instanceof Repairable)) { + return false; + } + Repairable repairable1 = (Repairable) meta1; + Repairable repairable2 = (Repairable) meta2; + if (repairable1.hasRepairCost() != repairable2.hasRepairCost()) { + return false; + } + if (repairable1.hasRepairCost()) { + return repairable2.getRepairCost() <= repairable1.getRepairCost(); + } + return true; + } + + private boolean potionMatches(ItemMeta meta1, ItemMeta meta2) { + if (!this.potions) { + return true; + } + Util.debugLog("Checking potion effects"); + if ((meta1 instanceof PotionMeta) != (meta2 instanceof PotionMeta)) { + return false; + } + + if (!(meta1 instanceof PotionMeta)) { + return true; // No potion meta needs to be checked. + } + + PotionMeta potion1 = (PotionMeta) meta1; + PotionMeta potion2 = (PotionMeta) meta2; + + if (potion1.hasColor()) { + if (!potion2.hasColor()) { + return false; + } else { + if (!Objects.requireNonNull(potion1.getColor()).equals(potion2.getColor())) { + return false; + } + } + } + if (potion1.hasCustomEffects()) { + if (!potion2.hasCustomEffects()) { + return false; + } + if (!Arrays.deepEquals( + potion1.getCustomEffects().toArray(), potion2.getCustomEffects().toArray())) { + return false; + } + // if + // (!Util.listMatches(potion1.getCustomEffects(),potion2.getCustomEffects())) { + // return false; + // } + } + PotionData data1 = potion1.getBasePotionData(); + PotionData data2 = potion2.getBasePotionData(); + if (data2.equals(data1)) { + return true; + } + if (!data2.getType().equals(data1.getType())) { + return false; + } + if (data1.isExtended()) { + if (!data2.isExtended()) { + return false; + } + } + if (data1.isUpgraded()) { + //noinspection RedundantIfStatement + if (!data2.isUpgraded()) { + return false; + } + } + return true; + } + + private boolean rootMatches(ItemMeta meta1, ItemMeta meta2) { + return (meta1.hashCode() == meta2.hashCode()); + } }