Apply the set_name loot function to generated items

This commit is contained in:
Kacpherek
2026-10-26 19:37:51 +01:00
parent 7f8a6680a8
commit dc36089156
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"));
@@ -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<'_> {
+10
View File
@@ -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<Tag>) {
self.other.retain(|(k, _)| k != id);
+1
View File
@@ -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
@@ -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));
+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 {
@@ -1219,7 +1222,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