2020-05-06 11:48:49 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2018-07-29 18:30:01 +02:00
From: Hugo Manrique <hugmanrique@gmail.com>
Date: Thu, 26 Jul 2018 14:10:23 +0200
Subject: [PATCH] Don't call getItemMeta on hasItemMeta
Spigot 1.13 checks if any field (which are manually copied from the ItemStack's "tag" NBT tag) on the ItemMeta class of an ItemStack is set.
We could just check if the "tag" NBT tag is empty, albeit that would break some plugins. The only general tag added on 1.13 is "Damage", and we can just check if the "tag" NBT tag contains any other tag that's not "Damage" (https://minecraft.gamepedia.com/Player.dat_format#Item_structure) making the `hasItemStack` method behave as before.
Returns true if getDamage() == 0 or has damage tag or other tag is set.
Check the `ItemMetaTest#testTaggedButNotMeta` method to see how this method behaves.
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
2021-03-16 08:19:45 +01:00
index 315addab147dfecf4aa88d32d154cefe850d0a78..09d7a86b5f6cffdf6664ad657dd4f8dd8eabdd70 100644
2018-07-29 18:30:01 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
2020-07-03 08:08:00 +02:00
@@ -570,7 +570,7 @@ public final class CraftItemStack extends ItemStack {
2018-07-29 18:30:01 +02:00
@Override
public boolean hasItemMeta() {
- return hasItemMeta(handle) && !CraftItemFactory.instance().equals(getItemMeta(), null);
+ return hasItemMeta(handle) && (handle.getDamage() != 0 || (handle.getTag() != null && handle.getTag().map.size() >= (handle.getTag().hasKey(CraftMetaItem.DAMAGE.NBT) ? 2 : 1))); // Paper - keep 1.12 CraftBukkit behavior without calling getItemMeta
}
2021-03-16 08:19:45 +01:00
static boolean hasItemMeta(net.minecraft.world.item.ItemStack item) {
2018-07-29 18:30:01 +02:00
diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java
2021-03-16 08:19:45 +01:00
index 42f577ed3508ba5a380648461e149f16ce97c9bd..b85a0a4c4f134dd6012d9141244ecf97b4300b65 100644
2018-07-29 18:30:01 +02:00
--- a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java
+++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java
2021-03-16 08:19:45 +01:00
@@ -96,6 +96,34 @@ public class ItemMetaTest extends AbstractTestingBase {
2019-12-12 01:03:31 +01:00
assertThat(itemMeta.hasConflictingEnchant(null), is(false));
}
+ // Paper start
2018-07-29 18:30:01 +02:00
+ private void testItemMeta(ItemStack stack) {
+ assertThat("Should not have ItemMeta", stack.hasItemMeta(), is(false));
+
+ stack.setDurability((short) 0);
+ assertThat("ItemStack with zero durability should not have ItemMeta", stack.hasItemMeta(), is(false));
+
+ stack.setDurability((short) 2);
+ assertThat("ItemStack with non-zero durability should have ItemMeta", stack.hasItemMeta(), is(true));
+
2021-03-16 08:19:45 +01:00
+ stack.setLore(java.util.Collections.singletonList("Lore"));
2018-07-29 18:30:01 +02:00
+ assertThat("ItemStack with lore and durability should have ItemMeta", stack.hasItemMeta(), is(true));
+
+ stack.setDurability((short) 0);
+ assertThat("ItemStack with lore should have ItemMeta", stack.hasItemMeta(), is(true));
+
+ stack.setLore(null);
+ }
+
+ @Test
+ public void testHasItemMeta() {
+ ItemStack itemStack = new ItemStack(Material.SHEARS);
+
+ testItemMeta(itemStack);
+ testItemMeta(CraftItemStack.asCraftCopy(itemStack));
2019-12-12 01:03:31 +01:00
+ }
2018-07-29 18:30:01 +02:00
+ // Paper end
2019-12-12 01:03:31 +01:00
+
2018-07-29 18:30:01 +02:00
@Test
2019-12-12 01:03:31 +01:00
public void testConflictingStoredEnchantment() {
EnchantmentStorageMeta itemMeta = (EnchantmentStorageMeta) Bukkit.getItemFactory().getItemMeta(Material.ENCHANTED_BOOK);