From dc36089156d9af32cd00be9dc4f966faea90cd3a Mon Sep 17 00:00:00 2001 From: Kacpherek Date: Mon, 26 Oct 2026 19:37:51 +0100 Subject: [PATCH] Apply the set_name loot function to generated items --- .../examples/probe/entity_death_loot_fixture.rs | 1 + crates/horizon-client/src/game/creatures.rs | 4 ++++ crates/horizon-client/src/item_data.rs | 10 ++++++++++ crates/horizon-client/src/mobs/drops.rs | 1 + crates/horizon-client/src/structure_blocks.rs | 5 +++++ crates/horizon-world/src/loot.rs | 14 +++++++++++++- 6 files changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/examples/probe/entity_death_loot_fixture.rs b/crates/horizon-client/examples/probe/entity_death_loot_fixture.rs index 3f47ba787..f05bd8b83 100644 --- a/crates/horizon-client/examples/probe/entity_death_loot_fixture.rs +++ b/crates/horizon-client/examples/probe/entity_death_loot_fixture.rs @@ -24,6 +24,7 @@ fn stack(spec: &str) -> LootStack { exploration: None, ominous_amplifier: None, stew_effects: Vec::new(), + names: Vec::new(), }; for e in parts { let (name, level) = e.rsplit_once(':').unwrap_or((e, "1")); diff --git a/crates/horizon-client/src/game/creatures.rs b/crates/horizon-client/src/game/creatures.rs index 845d2aee6..50fd5ddf5 100644 --- a/crates/horizon-client/src/game/creatures.rs +++ b/crates/horizon-client/src/game/creatures.rs @@ -3342,6 +3342,7 @@ fn to_loot(stack: &ItemStack) -> horizon_world::loot::LootStack { exploration: None, ominous_amplifier: None, stew_effects: Vec::new(), + names: Vec::new(), } } /// A generated loot stack as a Horizon item stack. @@ -3361,6 +3362,9 @@ fn from_loot(stack: &horizon_world::loot::LootStack) -> ItemStack { if let Some(amplifier) = stack.ominous_amplifier.filter(|a| *a > 0) { item.data_mut().ominous_amplifier = Some(amplifier as u8); } + for (id, text) in &stack.names { + item.data_mut().set_loot_name(id, text); + } item } impl Game<'_> { diff --git a/crates/horizon-client/src/item_data.rs b/crates/horizon-client/src/item_data.rs index ac624e85b..0c4adbd55 100644 --- a/crates/horizon-client/src/item_data.rs +++ b/crates/horizon-client/src/item_data.rs @@ -90,6 +90,16 @@ impl StackData { pub fn other(&self, id: &str) -> Option<&Tag> { self.other.iter().find(|(k, _)| k == id).map(|(_, v)| v) } + /// `SetNameFunction.run`: a loot text component stored as `custom_name` + /// (plain text, as modelled) or as the unmodelled `item_name`. + pub fn set_loot_name(&mut self, id: &str, text: &serde_json::Value) { + let tag = crate::scoreboard::json_to_tag(text); + if id == "minecraft:custom_name" { + self.custom_name = read_text(&tag); + } else { + self.set_other(id, Some(tag)); + } + } /// Set (or with None remove) an unmodelled component. pub fn set_other(&mut self, id: &str, value: Option) { self.other.retain(|(k, _)| k != id); diff --git a/crates/horizon-client/src/mobs/drops.rs b/crates/horizon-client/src/mobs/drops.rs index c7b1c77e1..4d4166e53 100644 --- a/crates/horizon-client/src/mobs/drops.rs +++ b/crates/horizon-client/src/mobs/drops.rs @@ -305,6 +305,7 @@ fn loot_stack(item: &str, count: i32) -> LootStack { exploration: None, ominous_amplifier: None, stew_effects: Vec::new(), + names: Vec::new(), } } /// `Mob.dropChances.byEquipment(slot)`: the recorded chance, or the one the diff --git a/crates/horizon-client/src/structure_blocks.rs b/crates/horizon-client/src/structure_blocks.rs index 604cf7925..909d69926 100644 --- a/crates/horizon-client/src/structure_blocks.rs +++ b/crates/horizon-client/src/structure_blocks.rs @@ -69,6 +69,9 @@ pub fn loot_item_with_components(s: &horizon_world::loot::LootStack) -> ItemStac .stew_effects .push((Arc::from(effect.as_str()), *duration)); } + for (id, text) in &s.names { + item.data_mut().set_loot_name(id, text); + } item } @@ -255,6 +258,7 @@ mod tests { exploration: None, ominous_amplifier: None, stew_effects: Vec::new(), + names: Vec::new(), }; // An End city chest's enchant_with_levels roll. let boots = loot_item_with_components(&stack( @@ -317,6 +321,7 @@ mod tests { })))), ominous_amplifier: None, stew_effects: Vec::new(), + names: Vec::new(), }; let mut item = loot_item(&stack); assert!(has_pending_map(&item)); diff --git a/crates/horizon-world/src/loot.rs b/crates/horizon-world/src/loot.rs index f98afa21f..19651ee99 100644 --- a/crates/horizon-world/src/loot.rs +++ b/crates/horizon-world/src/loot.rs @@ -34,6 +34,8 @@ pub struct LootStack { /// Added by set_stew_effect (the `suspicious_stew_effects` component): /// (effect id, duration in ticks). pub stew_effects: Vec<(String, i32)>, + /// Set by set_name: (`custom_name` or `item_name`, the text component). + pub names: Vec<(String, Value)>, } /// `ExplorationMapFunction`'s settings. #[derive(Clone, Debug, PartialEq, Eq)] @@ -78,6 +80,7 @@ impl LootStack { exploration: None, ominous_amplifier: None, stew_effects: Vec::new(), + names: Vec::new(), } } pub fn is_empty(&self) -> bool { @@ -1219,7 +1222,16 @@ impl Context<'_, R> { stack = smelted; } } - // Names, lore, components and other data without random draws. + // SetNameFunction: the component the target names (custom_name by + // default). The entity-resolved selectors and scores are not + // modelled; no table uses `entity`. + "minecraft:set_name" if !f["name"].is_null() => { + let target = f["target"].as_str().unwrap_or("custom_name"); + let id = format!("minecraft:{target}"); + stack.names.retain(|(k, _)| *k != id); + stack.names.push((id, f["name"].clone())); + } + // Lore, components and other data without random draws. _ => {} } stack