PaperMC/patches/server/0227-Don-t-call-getItemMeta-on-hasItemMeta.patch

65 lines
3.6 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
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.
2023-10-01 02:20:51 +02:00
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.wiki/wiki/Player.dat_format#Item_structure) making the `hasItemStack` method behave as before.
2021-06-11 14:02:28 +02:00
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
2023-12-06 21:09:14 +01:00
index 36bc8d587ef39baf4c85b30578d3066cb36ded21..1cd7a7ab18dca87090e7aba9c0683f937fdb6188 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
2023-12-06 21:09:14 +01:00
@@ -670,7 +670,7 @@ public final class CraftItemStack extends ItemStack {
2021-06-11 14:02:28 +02:00
@Override
public boolean hasItemMeta() {
2021-06-13 01:45:00 +02:00
- return CraftItemStack.hasItemMeta(this.handle) && !CraftItemFactory.instance().equals(this.getItemMeta(), null);
+ return CraftItemStack.hasItemMeta(this.handle) && (this.handle.getDamageValue() != 0 || (this.handle.getTag() != null && this.handle.getTag().tags.size() >= (this.handle.getTag().contains(CraftMetaItem.DAMAGE.NBT) ? 2 : 1))); // Paper - keep 1.12 CraftBukkit behavior without calling getItemMeta
2021-06-11 14:02:28 +02:00
}
static boolean hasItemMeta(net.minecraft.world.item.ItemStack item) {
diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java
index 3effe3c3b7ad949bc786e1b6e560553f2ca3f233..937ddd3a2b9e1df2c8835079923c339178f08459 100644
2021-06-11 14:02:28 +02:00
--- a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java
+++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java
@@ -108,6 +108,34 @@ public class ItemMetaTest extends AbstractTestingBase {
2021-06-11 14:02:28 +02:00
assertThat(itemMeta.hasConflictingEnchant(null), is(false));
}
+ // Paper start
+ private void testItemMeta(ItemStack stack) {
+ assertThat(stack.hasItemMeta(), is(false), "Should not have ItemMeta");
2021-06-11 14:02:28 +02:00
+
+ stack.setDurability((short) 0);
+ assertThat(stack.hasItemMeta(), is(false), "ItemStack with zero durability should not have ItemMeta");
2021-06-11 14:02:28 +02:00
+
+ stack.setDurability((short) 2);
+ assertThat(stack.hasItemMeta(), is(true), "ItemStack with non-zero durability should have ItemMeta");
2021-06-11 14:02:28 +02:00
+
+ stack.setLore(java.util.Collections.singletonList("Lore"));
+ assertThat(stack.hasItemMeta(), is(true), "ItemStack with lore and durability should have ItemMeta");
2021-06-11 14:02:28 +02:00
+
+ stack.setDurability((short) 0);
+ assertThat(stack.hasItemMeta(), is(true), "ItemStack with lore should have ItemMeta");
2021-06-11 14:02:28 +02:00
+
+ stack.setLore(null);
+ }
+
+ @Test
+ public void testHasItemMeta() {
+ ItemStack itemStack = new ItemStack(Material.SHEARS);
+
+ testItemMeta(itemStack);
+ testItemMeta(CraftItemStack.asCraftCopy(itemStack));
+ }
+ // Paper end
+
@Test
public void testConflictingStoredEnchantment() {
EnchantmentStorageMeta itemMeta = (EnchantmentStorageMeta) Bukkit.getItemFactory().getItemMeta(Material.ENCHANTED_BOOK);