Model item_name component on items and tooltips

In vanilla 1.20.5+, servers and datapacks customize base item names using
the `minecraft:item_name` data component rather than `minecraft:custom_name`.
Vanilla's `ItemStack.getHoverName()` checks `custom_name` first, then
falls back to `getItemName()`, which retrieves `DataComponents.ITEM_NAME`.

Previously, Horizon did not model `item_name` in `StackData`, causing items
with custom base names from servers or resource packs to fall back to
their default translation keys instead of the server-provided name.

This adds `item_name` to `StackData`, decodes and encodes it across
network item sync, and prioritizes it in `name_line` and hover tooltips.

Fixes #2039.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lazy-importer
2026-10-26 10:21:00 +01:00
parent 9e447a7e92
commit ad7203fc8a
6 changed files with 33 additions and 0 deletions
@@ -1656,6 +1656,8 @@ impl Game<'_> {
};
let name = if let Some(n) = stack.custom_name() {
n.to_owned()
} else if let Some(n) = stack.item_name() {
n.to_owned()
} else if stack.data().is_some_and(|d| d.ominous_banner) {
self.translate("block.minecraft.ominous_banner", "Ominous Banner")
} else if stack.data().and_then(|d| d.potion.as_ref()).is_some()
@@ -1682,6 +1684,7 @@ impl Game<'_> {
// ItemContainerContents.addToTooltip.
let hover_name = |s: &ItemStack| {
s.custom_name()
.or_else(|| s.item_name())
.map_or_else(|| self.pack.names.display_item(&s.item), str::to_owned)
};
if display.shows("minecraft:container") {
+12
View File
@@ -18,6 +18,8 @@ pub struct StackData {
pub repair_cost: u32,
/// `minecraft:custom_name` as plain text.
pub custom_name: Option<String>,
/// `minecraft:item_name` as plain text.
pub item_name: Option<String>,
/// `minecraft:potion_contents.potion`.
pub potion: Option<Arc<str>>,
/// `minecraft:trim`: (material, pattern).
@@ -73,6 +75,7 @@ const MODELLED: &[&str] = &[
"minecraft:enchantments",
"minecraft:firework_explosion",
"minecraft:fireworks",
"minecraft:item_name",
"minecraft:map_id",
"minecraft:map_post_processing",
"minecraft:ominous_bottle_amplifier",
@@ -204,6 +207,9 @@ impl ItemStack {
pub fn custom_name(&self) -> Option<&str> {
self.data().and_then(|d| d.custom_name.as_deref())
}
pub fn item_name(&self) -> Option<&str> {
self.data().and_then(|d| d.item_name.as_deref())
}
pub fn potion(&self) -> Option<&str> {
self.data().and_then(|d| d.potion.as_deref())
}
@@ -473,6 +479,11 @@ pub fn components_tag(damage: u32, data: Option<&StackData>) -> Option<Tag> {
if let Some(name) = &d.custom_name {
entries.push(("minecraft:custom_name", Tag::String(name.as_str().into())));
}
if let Some(name) = &d.item_name
&& !d.ominous_banner
{
entries.push(("minecraft:item_name", Tag::String(name.as_str().into())));
}
if let Some(potion) = &d.potion {
entries.push((
"minecraft:potion_contents",
@@ -675,6 +686,7 @@ pub fn decode_components(components: Option<&Tag>) -> (u32, Option<Box<StackData
.unwrap_or(0)
.max(0) as u32,
custom_name: c.get("minecraft:custom_name").and_then(read_text),
item_name: c.get("minecraft:item_name").and_then(read_text),
potion,
map_id: c.get("minecraft:map_id").and_then(int).map(|v| v as i32),
banner_patterns: crate::banner::layers_from_nbt(c.get("minecraft:banner_patterns")),
+3
View File
@@ -246,6 +246,7 @@ mod tests {
for (id, name) in [
(component::DAMAGE, "minecraft:damage"),
(component::CUSTOM_NAME, "minecraft:custom_name"),
(component::ITEM_NAME, "minecraft:item_name"),
(component::ENCHANTMENTS, "minecraft:enchantments"),
(component::POTION_CONTENTS, "minecraft:potion_contents"),
(component::REPAIR_COST, "minecraft:repair_cost"),
@@ -305,6 +306,7 @@ mod tests {
.push((Arc::from("minecraft:sharpness"), 2));
d.repair_cost = 9;
d.custom_name = Some("Keeper".to_owned());
d.item_name = Some("Fine Diamond Chestplate".to_owned());
d.trim = Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry")));
let id = |registry: &str, name: &str| {
builtin
@@ -334,6 +336,7 @@ mod tests {
);
assert_eq!(d.repair_cost, 9);
assert_eq!(d.custom_name.as_deref(), Some("Keeper"));
assert_eq!(d.item_name.as_deref(), Some("Fine Diamond Chestplate"));
assert_eq!(
d.trim,
Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry")))
@@ -189,6 +189,9 @@ pub fn name_line(stack: &ItemStack, translate: &dyn Fn(&str) -> Option<String>)
if let Some(name) = stack.custom_name() {
return name.to_owned();
}
if let Some(name) = stack.item_name() {
return name.to_owned();
}
if stack.data().is_some_and(|d| d.ominous_banner) {
return translate("block.minecraft.ominous_banner")
.unwrap_or_else(|| "Ominous Banner".to_owned());
@@ -612,6 +615,11 @@ mod tests {
assert_eq!(rarity(&banner, "common"), "uncommon");
banner.data_mut().custom_name = Some("Captain's Standard".into());
assert_eq!(name_line(&banner, &translate), "Captain's Standard");
let mut custom_item = ItemStack::new("minecraft:diamond_sword", 1);
custom_item.data_mut().item_name = Some("Excalibur".into());
assert_eq!(name_line(&custom_item, &translate), "Excalibur");
custom_item.data_mut().custom_name = Some("Custom Blade".into());
assert_eq!(name_line(&custom_item, &translate), "Custom Blade");
banner.enchant("minecraft:unbreaking", 1);
assert_eq!(rarity(&banner, "common"), "rare");
let plain = ItemStack::new("minecraft:white_banner", 1);
+6
View File
@@ -633,6 +633,12 @@ fn network_stack(
horizon_protocol::item::Component::Text(horizon_protocol::text::literal(name)),
));
}
if let Some(name) = s.item_name() {
stack.added.push((
horizon_protocol::item::component::ITEM_NAME,
horizon_protocol::item::Component::Text(horizon_protocol::text::literal(name)),
));
}
Some(stack)
}
+1
View File
@@ -174,6 +174,7 @@ pub fn increased_repair_cost(cost: u32) -> u32 {
pub fn hover_name(names: &dyn Fn(&str) -> String, stack: &ItemStack) -> String {
stack
.custom_name()
.or_else(|| stack.item_name())
.map_or_else(|| names(&stack.item), str::to_owned)
}
/// `AnvilMenu.createResult`. `name` is the rename box text (None = untouched).