From a07cec16f8d7098fabc4a3bccb7d6da263410d4a Mon Sep 17 00:00:00 2001 From: masmc05 <63639746+masmc05@users.noreply.github.com> Date: Wed, 25 Dec 2024 22:56:24 +0200 Subject: [PATCH] Add effective name to ItemStack (#11770) --- .../main/java/org/bukkit/inventory/ItemStack.java | 12 ++++++++++++ .../bukkit/craftbukkit/inventory/CraftItemStack.java | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java b/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java index 8c9654cd19..ed95b77a0b 100644 --- a/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java +++ b/paper-api/src/main/java/org/bukkit/inventory/ItemStack.java @@ -5,6 +5,7 @@ import com.google.common.collect.ImmutableMap; import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; +import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.NamespacedKey; @@ -710,6 +711,17 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat return Bukkit.getServer().getItemFactory().displayName(this); } + /** + * Gets the effective name of this item stack shown to player in inventory. + * It takes into account the display name (with italics) from the item meta, + * the potion effect, translatable name, rarity etc. + * + * @return the effective name of this item stack + */ + public @NotNull Component effectiveName() { + return this.craftDelegate.effectiveName(); + } + /** * Minecraft updates are converting simple item stacks into more complex NBT oriented Item Stacks. * diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java index 78975412da..3799973696 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -5,6 +5,8 @@ import com.google.common.collect.ImmutableMap; import java.util.Collections; import java.util.Map; import java.util.Optional; +import io.papermc.paper.adventure.PaperAdventure; +import net.kyori.adventure.text.Component; import net.minecraft.advancements.critereon.ItemPredicate; import net.minecraft.advancements.critereon.MinMaxBounds; import net.minecraft.core.Holder; @@ -25,6 +27,7 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.material.MaterialData; +import org.jetbrains.annotations.NotNull; @DelegateDeserialization(ItemStack.class) public final class CraftItemStack extends ItemStack { @@ -467,6 +470,11 @@ public final class CraftItemStack extends ItemStack { return true; } + @Override + public @NotNull Component effectiveName() { + return this.handle == null ? Component.empty() : PaperAdventure.asAdventure(this.handle.getStyledHoverName()); + } + @Override public boolean isSimilar(ItemStack stack) { if (stack == null) {