Merge pull request #2392 from kacpherek/fix/loot-set-name

🤖 Agent: Proven in union R1 (branch union/cg19-r1, main + 83 of kacpherek's PRs): the 98 harnesses it can affect, clippy -D warnings, tests, lan_e2e in-frame and on the server thread.
This commit is contained in:
Ludovic Marie
2026-10-27 14:52:46 +01:00
committed by GitHub
6 changed files with 34 additions and 1 deletions
@@ -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"));
@@ -4189,6 +4189,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.
@@ -4208,6 +4209,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<'_> {
+10
View File
@@ -106,6 +106,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<Tag>) {
self.other.retain(|(k, _)| k != id);
+1
View File
@@ -311,6 +311,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
@@ -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
}
@@ -287,6 +290,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(
@@ -349,6 +353,7 @@ mod tests {
})))),
ominous_amplifier: None,
stew_effects: Vec::new(),
names: Vec::new(),
};
let mut item = loot_item(&stack);
assert!(has_pending_map(&item));
+13 -1
View File
@@ -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 {
@@ -1224,7 +1227,16 @@ impl<R: RandomSource> 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