2019-04-22 23:36:14 +02:00
From 1357a61602da080e892436071b2f5171f3dc59ea 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
2019-04-22 23:36:14 +02:00
index bc8fb28163..ca9399bdbe 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
2019-03-28 05:58:05 +01:00
@@ -525,7 +525,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
}
static boolean hasItemMeta(net.minecraft.server.ItemStack item) {
diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java
2019-04-22 23:36:14 +02:00
index d4f4508cce..3a2793f322 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
@@ -5,6 +5,7 @@ import static org.hamcrest.Matchers.*;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections; // Paper
import java.util.List;
2018-09-29 01:31:59 +02:00
import java.util.UUID;
2019-04-22 23:36:14 +02:00
@@ -183,7 +184,47 @@ public class ItemMetaTest extends AbstractTestingBase {
assertThat("noTag and enchanted stacks should not be similar", noTag.isSimilar(enchanted), is(false));
assertThat("noTag and enchanted stacks should not be equal", noTag.equals(enchanted), is(false));
+
2018-07-29 18:30:01 +02:00
+ // Paper start - test additional ItemMeta damage cases
2019-04-22 23:36:14 +02:00
+ ItemStack clone = CraftItemStack.asBukkitCopy(CraftItemStack.asNMSCopy(noDamage));
+ assertThat("Bukkit and craft stacks should be similar", noDamage.isSimilar(clone), is(true));
+ assertThat("Bukkit and craft stacks should be equal", noDamage.equals(clone), is(true));
2018-07-29 18:30:01 +02:00
+
2019-04-22 23:36:14 +02:00
+ ItemStack pureBukkit = new ItemStack(Material.DIAMOND_SWORD);
2018-07-29 18:30:01 +02:00
+ pureBukkit.setDurability((short) 2);
+ net.minecraft.server.ItemStack nms = CraftItemStack.asNMSCopy(pureBukkit);
+ ItemStack other = CraftItemStack.asBukkitCopy(nms);
+
+ assertThat("Bukkit and NMS ItemStack copies should be similar", pureBukkit.isSimilar(other), is(true));
+ assertThat("Bukkit and NMS ItemStack copies should be equal", pureBukkit.equals(other), is(true));
2019-04-22 23:36:14 +02:00
+ }
+
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));
+
+ stack.setLore(Collections.singletonList("Lore"));
+ 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-04-22 23:36:14 +02:00
}
2018-07-29 18:30:01 +02:00
+ // Paper end
2019-04-22 23:36:14 +02:00
2018-07-29 18:30:01 +02:00
@Test
public void testBlockStateMeta() {
--
2019-03-20 02:46:00 +01:00
2.21.0
2018-07-29 18:30:01 +02:00