Fix meta matcher class in the matcher class

Former-commit-id: 3e7f2f90a576fbc0c37056298f8d939bbad1d16e [formerly f0ee1ce46f6125f4b4f8866b39031cc2b1abdbc1]
Former-commit-id: 01ebe770e0b638c381156310633a6620bb65e077
This commit is contained in:
Ghost_chu committed 2019-08-19 15:21:10 +08:00
1 parent 16d2903df4
commit 1cdb34bec5
1 file changed
+244 -243
@@ -41,7 +41,7 @@ public class ItemMatcher {
}
if (requireStack == null || givenStack == null) {
Util.debugLog("Match failed: A stack is null: " + "requireStack["+String.valueOf(requireStack)+"] givenStack["+givenStack+"]");
Util.debugLog("Match failed: A stack is null: " + "requireStack[" + String.valueOf(requireStack) + "] givenStack[" + givenStack + "]");
return false; // One of them is null (Can't be both, see above)
}
@@ -66,7 +66,7 @@ public class ItemMatcher {
// }
if (requireStack.hasItemMeta()) {
if(!givenStack.hasItemMeta()){
if (!givenStack.hasItemMeta()) {
Util.debugLog("Meta not match.");
return false;
}
@@ -79,288 +79,289 @@ public class ItemMatcher {
private boolean typeMatches(ItemStack requireStack, ItemStack givenStack) {
return requireStack.getType().equals(givenStack.getType());
}
}
class ItemMetaMatcher {
private boolean repaircost;
private boolean attributes;
private boolean custommodeldata;
private boolean damage;
private boolean displayname;
private boolean enchs;
private boolean itemflags;
private boolean lores;
private boolean potions;
class ItemMetaMatcher {
private boolean repaircost;
private boolean attributes;
private boolean custommodeldata;
private boolean damage;
private boolean displayname;
private boolean enchs;
private boolean itemflags;
private boolean lores;
private boolean potions;
public ItemMetaMatcher(ConfigurationSection itemMatcherConfig) {
this.damage = itemMatcherConfig.getBoolean("damage");
this.repaircost = itemMatcherConfig.getBoolean("repaircost");
this.displayname = itemMatcherConfig.getBoolean("displayname");
this.lores = itemMatcherConfig.getBoolean("lores");
this.enchs = itemMatcherConfig.getBoolean("enchs");
this.potions = itemMatcherConfig.getBoolean("potions");
this.attributes = itemMatcherConfig.getBoolean("attributes");
this.itemflags = itemMatcherConfig.getBoolean("itemflags");
this.custommodeldata = itemMatcherConfig.getBoolean("custommodeldata");
public ItemMetaMatcher(ConfigurationSection itemMatcherConfig) {
this.damage = itemMatcherConfig.getBoolean("damage");
this.repaircost = itemMatcherConfig.getBoolean("repaircost");
this.displayname = itemMatcherConfig.getBoolean("displayname");
this.lores = itemMatcherConfig.getBoolean("lores");
this.enchs = itemMatcherConfig.getBoolean("enchs");
this.potions = itemMatcherConfig.getBoolean("potions");
this.attributes = itemMatcherConfig.getBoolean("attributes");
this.itemflags = itemMatcherConfig.getBoolean("itemflags");
this.custommodeldata = itemMatcherConfig.getBoolean("custommodeldata");
}
private boolean attributeModifiersMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.attributes) {
return true;
}
//requireStack doen't need require must have AM, skipping..
if (!meta1.hasAttributeModifiers()) {
return true;
} else {
//If require AM but hadn't, the item not matched.
if (!meta2.hasAttributeModifiers()) {
return false;
}
return Util.mapMatches(meta1.getAttributeModifiers().asMap(), meta2.getAttributeModifiers().asMap());
}
}
private boolean customModelDataMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.custommodeldata) {
return true;
}
if (!meta1.hasCustomModelData()) {
return true;
} else {
if (!meta2.hasCustomModelData()) {
return false;
}
return meta1.getCustomModelData() == meta2.getCustomModelData();
}
private boolean attributeModifiersMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.attributes) {
return true;
}
//requireStack doen't need require must have AM, skipping..
if (!meta1.hasAttributeModifiers()) {
return true;
} else {
//If require AM but hadn't, the item not matched.
if (!meta2.hasAttributeModifiers()) {
return false;
}
return Util.mapMatches(meta1.getAttributeModifiers().asMap(), meta2.getAttributeModifiers().asMap());
}
}
private boolean damageMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.damage) {
return true;
}
private boolean customModelDataMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.custommodeldata) {
return true;
}
if (!meta1.hasCustomModelData()) {
return true;
} else {
if (!meta2.hasCustomModelData()) {
return false;
}
return meta1.getCustomModelData() == meta2.getCustomModelData();
}
}
private boolean damageMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.damage) {
return true;
}
// if (!(meta1 instanceof Damageable)) {
// return true; //No damage need to check.
// }
// if(!(meta2 instanceof Damageable)){
// return false;
// }
try {
Damageable damage1 = (Damageable) meta1;
Damageable damage2 = (Damageable) meta2;
//Check them damages, if givenDamage >= requireDamage, allow it.
return damage2.getDamage() >= damage1.getDamage();
}catch (Throwable th){
th.printStackTrace();
return true;
}
try {
Damageable damage1 = (Damageable) meta1;
Damageable damage2 = (Damageable) meta2;
//Check them damages, if givenDamage >= requireDamage, allow it.
return damage2.getDamage() >= damage1.getDamage();
} catch (Throwable th) {
th.printStackTrace();
return true;
}
}
private boolean displayMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.displayname) {
return true;
private boolean displayMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.displayname) {
return true;
}
if (!meta1.hasDisplayName()) {
return true;
} else {
if (!meta2.hasDisplayName()) {
return false;
}
if (!meta1.hasDisplayName()) {
return meta1.getDisplayName().equals(meta2.getDisplayName());
}
}
private boolean enchMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.enchs) {
return true;
}
if (!meta1.hasEnchants()) {
return true;
} else {
if (!meta2.hasEnchants()) {
return false;
}
Map<Enchantment, Integer> enchMap1 = meta1.getEnchants();
Map<Enchantment, Integer> enchMap2 = meta2.getEnchants();
if (!Util.mapMatches(enchMap1, enchMap2)) {
return false;
}
if (!(meta1 instanceof EnchantmentStorageMeta)) {
return true;
} else {
if (!meta2.hasDisplayName()) {
if (!(meta2 instanceof EnchantmentStorageMeta)) {
return false;
}
return meta1.getDisplayName().equals(meta2.getDisplayName());
Map<Enchantment, Integer> stor1 = ((EnchantmentStorageMeta) meta1).getStoredEnchants();
Map<Enchantment, Integer> stor2 = ((EnchantmentStorageMeta) meta2).getStoredEnchants();
return Util.mapMatches(stor1, stor2);
}
}
}
private boolean enchMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.enchs) {
return true;
}
if (!meta1.hasEnchants()) {
return true;
} else {
if (!meta2.hasEnchants()) {
return false;
}
Map<Enchantment, Integer> enchMap1 = meta1.getEnchants();
Map<Enchantment, Integer> enchMap2 = meta2.getEnchants();
if (!Util.mapMatches(enchMap1, enchMap2)) {
return false;
}
if (!(meta1 instanceof EnchantmentStorageMeta)) {
return true;
} else {
if (!(meta2 instanceof EnchantmentStorageMeta)) {
return false;
}
Map<Enchantment, Integer> stor1 = ((EnchantmentStorageMeta) meta1).getStoredEnchants();
Map<Enchantment, Integer> stor2 = ((EnchantmentStorageMeta) meta2).getStoredEnchants();
return Util.mapMatches(stor1, stor2);
}
}
private boolean itemFlagsMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.itemflags) {
return true;
}
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 meta1, ItemMeta meta2) {
if (!this.itemflags) {
return true;
}
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.
private boolean loresMatches(ItemMeta meta1, ItemMeta meta2) {
Util.debugLog("Lores checker");
if (!this.lores) {
return true;
}
//We didn't touch the loresMatches because many plugin use this check item.
private boolean loresMatches(ItemMeta meta1, ItemMeta meta2) {
Util.debugLog("Lores checker");
if (!this.lores) {
return true;
}
Util.debugLog("Lores checking");
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(lores1.toArray(), lores2.toArray());
Util.debugLog("Lores checking");
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(lores1.toArray(), lores2.toArray());
}
boolean matches(ItemStack requireStack, ItemStack givenStack) {
Util.debugLog("Begin the item matches checking...");
if (requireStack.hasItemMeta() != givenStack.hasItemMeta()) {
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)) {
return false;
}
if (meta1 == null) {
Util.debugLog("Pass");
return true; //Both null...
}
if (!damageMatches(meta1, meta2)) {
return false;
}
if (!repaircostMatches(meta1, meta2)) {
return false;
}
if (!displayMatches(meta1, meta2)) {
return false;
}
if (!loresMatches(meta1, meta2)) {
return false;
}
if (!enchMatches(meta1, meta2)) {
return false;
}
if (!potionMatches(meta1, meta2)) {
return false;
}
if (!attributeModifiersMatches(meta1, meta2)) {
return false;
}
if (!itemFlagsMatches(meta1, meta2)) {
return false;
}
try {
if (!customModelDataMatches(meta1, meta2)) {
return false;
}
} catch (NoSuchMethodError err) {
//Ignore, for 1.13 compatibility
}
boolean matches(ItemStack requireStack, ItemStack givenStack) {
Util.debugLog("Begin the item matches checking...");
if (requireStack.hasItemMeta() != givenStack.hasItemMeta()) {
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)) {
return false;
}
if (meta1 == null) {
Util.debugLog("Pass");
return true; //Both null...
}
if (!damageMatches(meta1, meta2)) {
return false;
}
if (!repaircostMatches(meta1, meta2)) {
return false;
}
if (!displayMatches(meta1, meta2)) {
return false;
}
if (!loresMatches(meta1, meta2)) {
return false;
}
if (!enchMatches(meta1, meta2)) {
return false;
}
if (!potionMatches(meta1, meta2)) {
return false;
}
if (!attributeModifiersMatches(meta1, meta2)) {
return false;
}
if (!itemFlagsMatches(meta1, meta2)) {
return false;
}
try {
if (!customModelDataMatches(meta1, meta2)) {
return false;
}
} catch (NoSuchMethodError err) {
//Ignore, for 1.13 compatibility
}
Util.debugLog("Pass");
return true;
}
private boolean repaircostMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.repaircost) {
return true;
}
private boolean repaircostMatches(ItemMeta meta1, ItemMeta meta2) {
if(!this.repaircost){
return true;
}
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();
}
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;
}
if ((meta1 instanceof PotionMeta) != (meta2 instanceof PotionMeta)) {
private boolean potionMatches(ItemMeta meta1, ItemMeta meta2) {
if (!this.potions) {
return true;
}
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;
}
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()){
} else {
if (!potion1.getColor().equals(potion2.getColor())) {
return false;
}else{
if (!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 (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;
}
}
}
PotionData data1 = potion1.getBasePotionData();
PotionData data2 = potion2.getBasePotionData();
if (data2.equals(data1)) {
return true;
}
private boolean rootMatches(ItemMeta meta1, ItemMeta meta2) {
return (meta1.hashCode() == meta2.hashCode());
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());
}
}