[untest] Rewrite & fix ItemMetaMatcher

Former-commit-id: 5b976c5f18c0b26ef5f69ddea1152aecf1050e43
This commit is contained in:
Sotr committed 2020-01-25 04:20:35 +07:00
1 parent e84dcefb05
commit 44f5c133bb
1 file changed
+164 -254
@@ -1,20 +1,17 @@
/*
* This file is a part of project QuickShop, the name is ItemMatcher.java
* Copyright (C) Ghost_chu <https://github.com/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
* <https://github.com/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 <http://www.gnu.org/licenses/>.
* You should have received a copy of the GNU Lesser General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
package org.maxgamer.quickshop.Util;
@@ -29,6 +26,7 @@ import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
@@ -36,9 +34,11 @@ 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.NotNull;
import org.jetbrains.annotations.Nullable;
import org.maxgamer.quickshop.QuickShop;
import com.google.common.collect.Multimap;
import com.sk89q.worldedit.extension.factory.ItemFactory;
/** A util allow quickshop check item matches easy and quick. */
public class ItemMatcher {
@@ -47,9 +47,8 @@ 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")));
}
/**
@@ -67,13 +66,8 @@ 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)
}
@@ -82,13 +76,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);
@@ -101,9 +95,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()) {
@@ -121,6 +115,7 @@ public class ItemMatcher {
}
}
class ItemMetaMatcher {
private boolean repaircost;
private boolean attributes;
@@ -131,6 +126,7 @@ class ItemMetaMatcher {
private boolean itemflags;
private boolean lores;
private boolean potions;
private boolean bukkit;
public ItemMetaMatcher(ConfigurationSection itemMatcherConfig) {
this.damage = itemMatcherConfig.getBoolean("damage");
@@ -142,269 +138,183 @@ 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) {
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<Attribute, AttributeModifier> mod2 = test.getAttributeModifiers();
return required.getAttributeModifiers()
.entries()
.stream().allMatch(e -> mod2.containsEntry(e.getKey(), e.getValue()));
}
Util.debugLog("Matching: Attribute Modifiers");
return attributes ?
// For non-exists, the default is same as null.
Objects.equals(required.getAttributeModifiers(), test.getAttributeModifiers()) : true;
}
private boolean customModelDataMatches(ItemMeta required, ItemMeta test) {
if (!this.custommodeldata) {
return true;
}
if (!required.hasCustomModelData()) {
return true;
} else {
if (!test.hasCustomModelData()) {
return false;
}
return required.getCustomModelData() == test.getCustomModelData();
}
Util.debugLog("Matching: Custom Model Data");
boolean requiredHas = required.hasCustomModelData();
return custommodeldata ? (canMatches(requiredHas, test.hasCustomModelData()) ?
(requiredHas ?
required.getCustomModelData() == test.getCustomModelData() : true) :
false) : true;
}
private boolean damageMatches(ItemMeta required, ItemMeta test) {
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();
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;
}
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 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 enchMatches(ItemMeta required, ItemMeta test) {
if (!this.enchs) {
return true;
}
Util.debugLog("Checking enchantments");
if (required.hasEnchants()) {
if (!test.hasEnchants()) {
return false;
}
Map<Enchantment, Integer> enchMap1 = required.getEnchants();
Map<Enchantment, Integer> enchMap2 = test.getEnchants();
return Util.mapMatches(enchMap1, enchMap2);
}
// Enchantment books
if ((required instanceof EnchantmentStorageMeta)) {
if (!(test instanceof EnchantmentStorageMeta)) {
return false;
}
Map<Enchantment, Integer> stor1 = ((EnchantmentStorageMeta) required).getStoredEnchants();
Map<Enchantment, Integer> stor2 = ((EnchantmentStorageMeta) test).getStoredEnchants();
return Util.mapMatches(stor1, stor2);
}
return true;
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 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());
}
private boolean itemFlagsMatches(ItemMeta required, ItemMeta test) {
Util.debugLog("Matching: Item Flags");
return itemflags ? required.getItemFlags().equals(test.getItemFlags()) : true;
}
// We didn't touch the loresMatches because many plugin use this check item.
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<String> lores1 = meta1.getLore();
List<String> lores2 = meta2.getLore();
return Arrays.deepEquals(
Objects.requireNonNull(lores1).toArray(), Objects.requireNonNull(lores2).toArray());
// 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;
}
boolean matches(ItemStack requireStack, ItemStack givenStack) {
Util.debugLog("Begin the item matches checking...");
if (requireStack.hasItemMeta() != givenStack.hasItemMeta()) {
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) {
return true;
}
ItemMeta requiredMeta = requiredStack.getItemMeta();
ItemMeta testMeta = testStack.getItemMeta();
if (!damageMatches(requiredMeta, testMeta)) {
return false;
}
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)) {
if (!repairCostMatches(requiredMeta, testMeta)) {
return false;
}
if (meta1 == null) {
Util.debugLog("Pass");
return true; // Both null...
}
if (!damageMatches(meta1, meta2)) {
if (!displayNameMatches(requiredMeta, testMeta)) {
return false;
}
if (!repaircostMatches(meta1, meta2)) {
if (!loreMatches(requiredMeta, testMeta)) {
return false;
}
if (!displayMatches(meta1, meta2)) {
if (!enchantsMatches(requiredMeta, testMeta)) {
return false;
}
if (!loresMatches(meta1, meta2)) {
if (!potionMatches(requiredMeta, testMeta)) {
return false;
}
if (!enchMatches(meta1, meta2)) {
if (!attributeModifiersMatches(requiredMeta, testMeta)) {
return false;
}
if (!potionMatches(meta1, meta2)) {
return false;
}
if (!attributeModifiersMatches(meta1, meta2)) {
return false;
}
if (!itemFlagsMatches(meta1, meta2)) {
if (!itemFlagsMatches(requiredMeta, testMeta)) {
return false;
}
try {
if (!customModelDataMatches(meta1, meta2)) {
if (!customModelDataMatches(requiredMeta, testMeta)) {
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());
}
}