diff --git a/crates/horizon-client/src/game/stations.rs b/crates/horizon-client/src/game/stations.rs index 2b51a81a1..56b19e393 100644 --- a/crates/horizon-client/src/game/stations.rs +++ b/crates/horizon-client/src/game/stations.rs @@ -1703,6 +1703,8 @@ impl Game<'_> { }; let name = if let Some(n) = stack.custom_name() { n.to_owned() + } else if let Some(n) = stack.item_name() { + n } 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() @@ -1729,7 +1731,9 @@ impl Game<'_> { // ItemContainerContents.addToTooltip. let hover_name = |s: &ItemStack| { s.custom_name() - .map_or_else(|| self.pack.names.display_item(&s.item), str::to_owned) + .map(str::to_owned) + .or_else(|| s.item_name()) + .unwrap_or_else(|| self.pack.names.display_item(&s.item)) }; if display.shows("minecraft:container") { for text in diff --git a/crates/horizon-client/src/item_data.rs b/crates/horizon-client/src/item_data.rs index c07e2f352..ac624e85b 100644 --- a/crates/horizon-client/src/item_data.rs +++ b/crates/horizon-client/src/item_data.rs @@ -204,6 +204,14 @@ impl ItemStack { pub fn custom_name(&self) -> Option<&str> { self.data().and_then(|d| d.custom_name.as_deref()) } + /// `minecraft:item_name` in the current language (`getItemName`'s + /// component). It stays in the patch as loaded, so a translatable name + /// saves and reaches clients unchanged; the ominous banner's is modelled + /// by `ominous_banner` instead. + pub fn item_name(&self) -> Option { + let name = self.data()?.other("minecraft:item_name")?; + Some(crate::entity_nbt::name_text(name).plain()) + } pub fn potion(&self) -> Option<&str> { self.data().and_then(|d| d.potion.as_deref()) } diff --git a/crates/horizon-client/src/item_sync.rs b/crates/horizon-client/src/item_sync.rs index d18ed3af5..81ab1d4f6 100644 --- a/crates/horizon-client/src/item_sync.rs +++ b/crates/horizon-client/src/item_sync.rs @@ -286,6 +286,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"), @@ -345,6 +346,10 @@ mod tests { .push((Arc::from("minecraft:sharpness"), 2)); d.repair_cost = 9; d.custom_name = Some("Keeper".to_owned()); + // A translatable item name reaches the client as the same component. + let item_name = Tag::compound([("translate", Tag::String("filled_map.monument".into()))]); + d.other + .push(("minecraft:item_name".into(), item_name.clone())); d.trim = Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry"))); let id = |registry: &str, name: &str| { builtin @@ -374,6 +379,7 @@ mod tests { ); assert_eq!(d.repair_cost, 9); assert_eq!(d.custom_name.as_deref(), Some("Keeper")); + assert_eq!(d.other("minecraft:item_name"), Some(&item_name)); assert_eq!( d.trim, Some((Arc::from("minecraft:gold"), Arc::from("minecraft:sentry"))) diff --git a/crates/horizon-client/src/item_tooltip.rs b/crates/horizon-client/src/item_tooltip.rs index cb52965a0..88f01439e 100644 --- a/crates/horizon-client/src/item_tooltip.rs +++ b/crates/horizon-client/src/item_tooltip.rs @@ -188,6 +188,9 @@ pub fn name_line(stack: &ItemStack, translate: &dyn Fn(&str) -> Option) if let Some(name) = stack.custom_name() { return name.to_owned(); } + if let Some(name) = stack.item_name() { + return name; + } if stack.data().is_some_and(|d| d.ominous_banner) { return translate("block.minecraft.ominous_banner") .unwrap_or_else(|| "Ominous Banner".to_owned()); @@ -611,6 +614,14 @@ 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().other.push(( + "minecraft:item_name".into(), + Tag::String("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); diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 9224d3711..e1fecf1de 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -645,6 +645,13 @@ fn network_stack( horizon_protocol::item::Component::Text(horizon_protocol::text::literal(name)), )); } + // The component as loaded, so a translatable name stays one. + if let Some(name) = s.data().and_then(|d| d.other("minecraft:item_name")) { + stack.added.push(( + horizon_protocol::item::component::ITEM_NAME, + horizon_protocol::item::Component::Text(name.clone()), + )); + } Some(stack) } diff --git a/crates/horizon-client/src/stations.rs b/crates/horizon-client/src/stations.rs index c24d8b39f..a307b35cf 100644 --- a/crates/horizon-client/src/stations.rs +++ b/crates/horizon-client/src/stations.rs @@ -175,7 +175,9 @@ pub fn increased_repair_cost(cost: u32) -> u32 { pub fn hover_name(names: &dyn Fn(&str) -> String, stack: &ItemStack) -> String { stack .custom_name() - .map_or_else(|| names(&stack.item), str::to_owned) + .map(str::to_owned) + .or_else(|| stack.item_name()) + .unwrap_or_else(|| names(&stack.item)) } /// `AnvilMenu.createResult`. `name` is the rename box text (None = untouched). pub fn anvil_result(