Skip to content

Commit

Permalink
Add effective name to ItemStack (#11770)
Browse files Browse the repository at this point in the history
  • Loading branch information
masmc05 authored Dec 25, 2024
1 parent 140577b commit a07cec1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions paper-api/src/main/java/org/bukkit/inventory/ItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -710,6 +711,17 @@ public net.kyori.adventure.text.event.HoverEvent<net.kyori.adventure.text.event.
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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;
Expand All @@ -25,6 +27,7 @@
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 {
Expand Down Expand Up @@ -467,6 +470,11 @@ void skullCallback(final net.minecraft.world.item.component.ResolvableProfile re
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) {
Expand Down

0 comments on commit a07cec1

Please sign in to comment.