From e7d9c31b568e7482303047489643f86e1c92ec61 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Wed, 12 May 2021 23:16:33 -0700 Subject: [PATCH] Add ItemStack#displayName to get the formatted display name of an ItemStack (#5628) The display name includes formatting such as item hover event, display name (in italics if renamed in anvil), and color based on rarity. This is what vanilla uses for give command feedback, and when an item is shown in a death message. --- Spigot-API-Patches/Adventure.patch | 18 ++++++++++++++++++ .../ensureServerConversions-API.patch | 6 +++--- Spigot-Server-Patches/Adventure.patch | 17 +++++++++++++++++ ...Implement-ensureServerConversions-API.patch | 4 ++-- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Spigot-API-Patches/Adventure.patch b/Spigot-API-Patches/Adventure.patch index d02e6b2b38..f9a91368ac 100644 --- a/Spigot-API-Patches/Adventure.patch +++ b/Spigot-API-Patches/Adventure.patch @@ -2869,6 +2869,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + */ + @NotNull + net.kyori.adventure.text.event.HoverEvent asHoverEvent(final @NotNull ItemStack item, final @NotNull java.util.function.UnaryOperator op); ++ ++ /** ++ * Get the formatted display name of the {@link ItemStack}. ++ * ++ * @param itemStack the {@link ItemStack} ++ * @return display name of the {@link ItemStack} ++ */ ++ @NotNull ++ net.kyori.adventure.text.Component displayName(@NotNull ItemStack itemStack); + // Paper end } diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java @@ -2895,6 +2904,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + public net.kyori.adventure.text.event.HoverEvent asHoverEvent(final @NotNull java.util.function.UnaryOperator op) { + return org.bukkit.Bukkit.getServer().getItemFactory().asHoverEvent(this, op); + } ++ ++ /** ++ * Get the formatted display name of the {@link ItemStack}. ++ * ++ * @return display name of the {@link ItemStack} ++ */ ++ public @NotNull net.kyori.adventure.text.Component displayName() { ++ return Bukkit.getServer().getItemFactory().displayName(this); ++ } + // Paper end } diff --git a/src/main/java/org/bukkit/inventory/meta/BookMeta.java b/src/main/java/org/bukkit/inventory/meta/BookMeta.java diff --git a/Spigot-API-Patches/ensureServerConversions-API.patch b/Spigot-API-Patches/ensureServerConversions-API.patch index 0e64dea686..55b5a8c188 100644 --- a/Spigot-API-Patches/ensureServerConversions-API.patch +++ b/Spigot-API-Patches/ensureServerConversions-API.patch @@ -13,7 +13,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 @@ -0,0 +0,0 @@ public interface ItemFactory { */ @NotNull - net.kyori.adventure.text.event.HoverEvent asHoverEvent(final @NotNull ItemStack item, final @NotNull java.util.function.UnaryOperator op); + net.kyori.adventure.text.Component displayName(@NotNull ItemStack itemStack); + + /** + * Minecart updates are converting simple item stacks into more complex NBT oriented Item Stacks. @@ -42,8 +42,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 /** @@ -0,0 +0,0 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, net.kyor - public net.kyori.adventure.text.event.HoverEvent asHoverEvent(final @NotNull java.util.function.UnaryOperator op) { - return org.bukkit.Bukkit.getServer().getItemFactory().asHoverEvent(this, op); + public @NotNull net.kyori.adventure.text.Component displayName() { + return Bukkit.getServer().getItemFactory().displayName(this); } + + /** diff --git a/Spigot-Server-Patches/Adventure.patch b/Spigot-Server-Patches/Adventure.patch index c17fa89e63..4c88ce5747 100644 --- a/Spigot-Server-Patches/Adventure.patch +++ b/Spigot-Server-Patches/Adventure.patch @@ -1612,6 +1612,18 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 return this.g; } +diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java +index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 +--- a/src/main/java/net/minecraft/world/item/ItemStack.java ++++ b/src/main/java/net/minecraft/world/item/ItemStack.java +@@ -0,0 +0,0 @@ public final class ItemStack { + } + // CraftBukkit end + ++ public IChatBaseComponent displayName() { return this.C(); } // Paper - OBFHELPER + public IChatBaseComponent C() { + IChatMutableComponent ichatmutablecomponent = (new ChatComponentText("")).addSibling(this.getName()); + diff --git a/src/main/java/net/minecraft/world/item/enchantment/Enchantment.java b/src/main/java/net/minecraft/world/item/enchantment/Enchantment.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/net/minecraft/world/item/enchantment/Enchantment.java @@ -2602,6 +2614,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + final net.minecraft.nbt.NBTTagCompound tag = CraftItemStack.asNMSCopy(item).getTag(); + return net.kyori.adventure.text.event.HoverEvent.showItem(op.apply(net.kyori.adventure.text.event.HoverEvent.ShowItem.of(item.getType().getKey(), item.getAmount(), io.papermc.paper.adventure.PaperAdventure.asBinaryTagHolder(tag)))); + } ++ ++ @Override ++ public net.kyori.adventure.text.@org.jetbrains.annotations.NotNull Component displayName(@org.jetbrains.annotations.NotNull ItemStack itemStack) { ++ return io.papermc.paper.adventure.PaperAdventure.asAdventure(CraftItemStack.asNMSCopy(itemStack).displayName()); ++ } + // Paper end } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchantCustom.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMerchantCustom.java diff --git a/Spigot-Server-Patches/Implement-ensureServerConversions-API.patch b/Spigot-Server-Patches/Implement-ensureServerConversions-API.patch index 0cfb2eccf5..d45cbbe21a 100644 --- a/Spigot-Server-Patches/Implement-ensureServerConversions-API.patch +++ b/Spigot-Server-Patches/Implement-ensureServerConversions-API.patch @@ -11,8 +11,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemFactory.java @@ -0,0 +0,0 @@ public final class CraftItemFactory implements ItemFactory { - final net.minecraft.nbt.NBTTagCompound tag = CraftItemStack.asNMSCopy(item).getTag(); - return net.kyori.adventure.text.event.HoverEvent.showItem(op.apply(net.kyori.adventure.text.event.HoverEvent.ShowItem.of(item.getType().getKey(), item.getAmount(), io.papermc.paper.adventure.PaperAdventure.asBinaryTagHolder(tag)))); + public net.kyori.adventure.text.@org.jetbrains.annotations.NotNull Component displayName(@org.jetbrains.annotations.NotNull ItemStack itemStack) { + return io.papermc.paper.adventure.PaperAdventure.asAdventure(CraftItemStack.asNMSCopy(itemStack).displayName()); } + + // Paper start