diff --git a/src/main/java/com/alttd/playershops/utils/ShopUtil.java b/src/main/java/com/alttd/playershops/utils/ShopUtil.java index 47d54be..015cfad 100644 --- a/src/main/java/com/alttd/playershops/utils/ShopUtil.java +++ b/src/main/java/com/alttd/playershops/utils/ShopUtil.java @@ -8,22 +8,17 @@ import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.OfflinePlayer; +import org.bukkit.*; +import org.bukkit.block.BlockState; +import org.bukkit.block.ShulkerBox; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.EnchantmentStorageMeta; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.inventory.meta.SkullMeta; +import org.bukkit.inventory.meta.*; +import org.bukkit.map.MapView; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; public class ShopUtil { @@ -59,37 +54,69 @@ public class ShopUtil { 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.hasItemMeta() && stack2.hasItemMeta())) { + return false; // Only one of the items has item meta + } + if (stack1.hasItemMeta() && stack2.hasItemMeta()) { + ItemMeta itemMeta1 = stack1.getItemMeta(); + ItemMeta itemMeta2 = stack2.getItemMeta(); + + if ((itemMeta1.hasDisplayName() != itemMeta2.hasDisplayName())) { + return false; // Only one has a display name + } + if (!itemMeta1.getDisplayName().equals(itemMeta2.getDisplayName())) { + return false; // items have different display name + } + + // This is where the heavy checks are :/ + if (itemMeta1 instanceof Damageable damageable1 && itemMeta2 instanceof Damageable damageable2) { + if (damageable1.getDamage() != damageable2.getDamage()) + return false; // Not the same durability + } + // We need this check now because mapart stores data in the map NBT + if (itemMeta1 instanceof MapMeta mapMeta1 && itemMeta2 instanceof MapMeta mapMeta2) { + MapView mapView1 = mapMeta1.getMapView(); + MapView mapView2 = mapMeta2.getMapView(); + if (mapView1 == null || mapView2 == null) + return false; // at least one is null + if (mapView1.getId() != mapView2.getId()) + return false; // ID does not match + } + if (itemMeta1 instanceof BlockStateMeta blockStateMeta1 && itemMeta2 instanceof BlockStateMeta blockStateMeta2) { + // extra heavy - Banners, Shulkerboxes, beehive and more? + BlockState blockState1 = blockStateMeta1.getBlockState(); + BlockState blockState2 = blockStateMeta2.getBlockState(); + + if (blockState1 instanceof ShulkerBox shulkerBox1 && blockState2 instanceof ShulkerBox shulkerBox2) { + if (shulkerBox1.getColor() != shulkerBox2.getColor()) { + return false; // not the same color + } + // Do we need all of the above checks inside the shulker? + if (!Arrays.equals(shulkerBox1.getInventory().getContents(), shulkerBox2.getInventory().getContents())) { + return false; // not the same content + } + } + } + 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 ench1 = ((EnchantmentStorageMeta) itemMeta1).getStoredEnchants(); + Map ench2 = ((EnchantmentStorageMeta) itemMeta2).getStoredEnchants(); + if (!ench1.equals(ench2)) + return false; // Enchants aren't the same. + } + } catch (ClassNotFoundException ignored) { + } + } 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 ench1 = ((EnchantmentStorageMeta) stack1.getItemMeta()).getStoredEnchants(); - Map ench2 = ((EnchantmentStorageMeta) stack2.getItemMeta()).getStoredEnchants(); - if (!ench1.equals(ench2)) - return false; // Enchants aren't the same. - } - } catch (ClassNotFoundException e) { - } +// if (!stack1.getItemMeta().equals(stack2.getItemMeta())) +// return false; // They have the same enchants + return true; }