Add effective name to ItemStack (#11770)

This commit is contained in:
masmc05 2024-12-25 22:56:24 +02:00 committed by GitHub
parent 140577b762
commit a07cec16f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 0 deletions

View file

@ -5,6 +5,7 @@ import com.google.common.collect.ImmutableMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
@ -710,6 +711,17 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat
return Bukkit.getServer().getItemFactory().displayName(this); 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. * Minecraft updates are converting simple item stacks into more complex NBT oriented Item Stacks.
* *

View file

@ -5,6 +5,8 @@ import com.google.common.collect.ImmutableMap;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Optional; 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.ItemPredicate;
import net.minecraft.advancements.critereon.MinMaxBounds; import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.core.Holder; import net.minecraft.core.Holder;
@ -25,6 +27,7 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.MaterialData; import org.bukkit.material.MaterialData;
import org.jetbrains.annotations.NotNull;
@DelegateDeserialization(ItemStack.class) @DelegateDeserialization(ItemStack.class)
public final class CraftItemStack extends ItemStack { public final class CraftItemStack extends ItemStack {
@ -467,6 +470,11 @@ public final class CraftItemStack extends ItemStack {
return true; return true;
} }
@Override
public @NotNull Component effectiveName() {
return this.handle == null ? Component.empty() : PaperAdventure.asAdventure(this.handle.getStyledHoverName());
}
@Override @Override
public boolean isSimilar(ItemStack stack) { public boolean isSimilar(ItemStack stack) {
if (stack == null) { if (stack == null) {