Change item matcher

This commit is contained in:
Len 2024-06-30 11:00:01 +02:00 committed by destro174
parent f5dc2dc670
commit cf7c77bf47

View File

@ -50,6 +50,47 @@ public class ShopUtil {
* @return true if the itemstacks match. (Material, durability, enchants, name)
*/
public static boolean matches(ItemStack stack1, ItemStack stack2) {
if (stack1 == stack2)
return true; // Referring to the same thing, or both are null.
if (stack1 == null || stack2 == null)
return false; // One of them is null (Can't be both, see above)
if (stack1.getType() != stack2.getType())
return false; // Not the same material
if (stack1.getDurability() != stack2.getDurability())
return false; // Not the same durability
if (!stack1.getEnchantments().equals(stack2.getEnchantments()))
return false; // They have the same enchants
if (!stack1.getItemMeta().equals(stack2.getItemMeta()))
return false; // They have the same enchants
if (stack1.getItemMeta().hasDisplayName() || stack2.getItemMeta().hasDisplayName()) {
if (stack1.getItemMeta().hasDisplayName() && stack2.getItemMeta().hasDisplayName()) {
if (!stack1.getItemMeta().getDisplayName().equals(stack2.getItemMeta().getDisplayName())) {
return false; // items have different display name
}
} else {
return false; // one of the item stacks have a display name
}
}
try {
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
boolean book1 = stack1.getItemMeta() instanceof EnchantmentStorageMeta;
boolean book2 = stack2.getItemMeta() instanceof EnchantmentStorageMeta;
if (book1 != book2)
return false;// One has enchantment meta, the other does not.
if (book1 == true) { // They are the same here (both true or both
// false). So if one is true, the other is
// true.
Map<Enchantment, Integer> ench1 = ((EnchantmentStorageMeta) stack1.getItemMeta()).getStoredEnchants();
Map<Enchantment, Integer> ench2 = ((EnchantmentStorageMeta) stack2.getItemMeta()).getStoredEnchants();
if (!ench1.equals(ench2))
return false; // Enchants aren't the same.
}
} catch (ClassNotFoundException e) {
}
return matches2(stack1, stack2);
}
public static boolean matches2(ItemStack stack1, ItemStack stack2) {
if (stack1 == stack2)
return true; // Referring to the same thing, or both are null.
@ -73,6 +114,22 @@ public class ShopUtil {
return false; // items have different display name
// This is where the heavy checks are :/
try {
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
boolean book1 = itemMeta1 instanceof EnchantmentStorageMeta;
boolean book2 = itemMeta2 instanceof EnchantmentStorageMeta;
if (book1 != book2)
return false;// One has enchantment meta, the other does not.
if (book1 == true) { // They are the same here (both true or both false). So if one is true, the other is true.
Map<Enchantment, Integer> ench1 = ((EnchantmentStorageMeta) itemMeta1).getStoredEnchants();
Map<Enchantment, Integer> ench2 = ((EnchantmentStorageMeta) itemMeta2).getStoredEnchants();
if (!ench1.equals(ench2))
return false; // Enchants aren't the same.
}
} catch (ClassNotFoundException ignored) {
}
if (itemMeta1 instanceof Damageable damageable1 && itemMeta2 instanceof Damageable damageable2)
if (damageable1.getDamage() != damageable2.getDamage())
return false; // Not the same durability
@ -84,8 +141,8 @@ public class ShopUtil {
if (mapView1 == null || mapView2 == null)
return false; // at least one is null
if (mapView1.getId() != mapView2.getId())
return false; // ID does not match
if (mapView1.getId() == mapView2.getId())
return true; // ID does not match
}
if (itemMeta1 instanceof TropicalFishBucketMeta tropicalFishBucketMeta1 && itemMeta2 instanceof TropicalFishBucketMeta tropicalFishBucketMeta2) {
@ -120,20 +177,22 @@ public class ShopUtil {
}
}
try {
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
boolean book1 = itemMeta1 instanceof EnchantmentStorageMeta;
boolean book2 = itemMeta2 instanceof EnchantmentStorageMeta;
if (book1 != book2)
return false;// One has enchantment meta, the other does not.
if (book1 == true) { // They are the same here (both true or both false). So if one is true, the other is true.
Map<Enchantment, Integer> ench1 = ((EnchantmentStorageMeta) itemMeta1).getStoredEnchants();
Map<Enchantment, Integer> ench2 = ((EnchantmentStorageMeta) itemMeta2).getStoredEnchants();
if (!ench1.equals(ench2))
return false; // Enchants aren't the same.
}
} catch (ClassNotFoundException ignored) {
// TODO
/* if (itemMeta1 instanceof BannerMeta bannerMeta1 && itemMeta2 instanceof BannerMeta bannerMeta2) {
if (bannerMeta1.numberOfPatterns() != bannerMeta2.numberOfPatterns())
return false;
if (!bannerMeta1.getPatterns().equals(bannerMeta2.getPatterns()))
return false;
}
if (itemMeta1 instanceof BookMeta bookMeta1 && itemMeta2 instanceof BookMeta bookMeta2) {
// Todo Books
}
if (itemMeta1 instanceof PotionMeta potionMeta1 && itemMeta2 instanceof PotionMeta potionMeta2) {
}*/
}
// if (!stack1.getEnchantments().equals(stack2.getEnchantments()))
// return false; // They have the same enchants