Content mods: add to other structure sets and pools (horizon:add_to)
A content mod's structure set or template pool file holding "horizon:add_to" appends its entries to the sets or pools it names (vanilla's, another mod's) instead of being one of its own, as lithostitched's add_structure_set_entries and add_template_pool_elements modifiers do on NeoForge and Fabric. The content root merges them after every mod's own files, in mod and file order; a name nobody defines is skipped. Vanilla data never has the key. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -697,6 +697,78 @@ fn worldgen_entry(file: &str) -> Option<(&'static str, String)> {
|
||||
Some((bundle, format!("{ns}:{}", path.strip_suffix(".json")?)))
|
||||
}
|
||||
|
||||
/// The key of a mod's structure set or template pool file that adds its
|
||||
/// entries to other sets or pools (vanilla's, another mod's) instead of
|
||||
/// being one of its own. Vanilla data never has it.
|
||||
const ADD_TO: &str = "horizon:add_to";
|
||||
/// The bundles a mod may add to, and the list each entry goes into.
|
||||
const ADDABLE: [(&str, &str); 2] = [
|
||||
("worldgen/structure_sets.json", "structures"),
|
||||
("worldgen/template_pools.json", "elements"),
|
||||
];
|
||||
|
||||
/// A mod file's additions (`horizon:add_to`): its entries appended to the
|
||||
/// list of each set or pool it names, as lithostitched's
|
||||
/// `add_structure_set_entries` and `add_template_pool_elements` do for
|
||||
/// NeoForge and Fabric packs.
|
||||
struct Addition {
|
||||
targets: Vec<String>,
|
||||
list: &'static str,
|
||||
entries: Vec<J>,
|
||||
}
|
||||
impl Addition {
|
||||
/// What a mod file of `bundle` adds, if it is an addition file.
|
||||
fn read(bundle: &str, text: &str) -> Result<Option<Self>> {
|
||||
let Some((_, list)) = ADDABLE.iter().find(|(b, _)| *b == bundle) else {
|
||||
return Ok(None);
|
||||
};
|
||||
if !text.contains(ADD_TO) {
|
||||
return Ok(None);
|
||||
}
|
||||
let doc: J = serde_json::from_str(text)?;
|
||||
let Some(targets) = doc.get(ADD_TO) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let targets = match targets {
|
||||
J::Str(s) => vec![s.clone()],
|
||||
J::Arr(a) => a
|
||||
.iter()
|
||||
.map(|v| match v {
|
||||
J::Str(s) => Some(s.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Option<_>>()
|
||||
.ok_or(format!("{ADD_TO}: not a list of ids"))?,
|
||||
_ => return Err(format!("{ADD_TO}: not an id or a list of ids").into()),
|
||||
};
|
||||
let Some(J::Arr(entries)) = doc.get(list) else {
|
||||
return Err(format!("{ADD_TO} without a {list} list").into());
|
||||
};
|
||||
Ok(Some(Self {
|
||||
targets,
|
||||
list,
|
||||
entries: entries.clone(),
|
||||
}))
|
||||
}
|
||||
/// Appends the entries to each target in the bundle `doc` (id → the
|
||||
/// file's text). A target nobody defines is skipped, as a tag entry
|
||||
/// naming an unknown id is.
|
||||
fn apply(&self, doc: &mut J) -> Result<()> {
|
||||
for target in &self.targets {
|
||||
let Some(J::Str(text)) = doc.get(target) else {
|
||||
continue;
|
||||
};
|
||||
let mut v: J = serde_json::from_str(text).map_err(|e| format!("{target}: {e}"))?;
|
||||
let Some(J::Arr(list)) = v.get_mut(self.list) else {
|
||||
continue;
|
||||
};
|
||||
list.extend(self.entries.iter().cloned());
|
||||
doc.set(target, J::Str(serde_json::to_string(&v)?));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// The overlay being written: `out` mirrors `base` through links, and a
|
||||
/// directory becomes real only when something inside it is written.
|
||||
struct Overlay<'a> {
|
||||
@@ -904,6 +976,7 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> {
|
||||
// Mod files, and every tag addition gathered per tag file.
|
||||
let mut tags: BTreeMap<String, Vec<J>> = BTreeMap::new();
|
||||
let mut worldgen: BTreeMap<&str, Vec<(String, String)>> = BTreeMap::new();
|
||||
let mut additions: BTreeMap<&str, Vec<Addition>> = BTreeMap::new();
|
||||
for m in mods {
|
||||
let declarations = format!("data/{}/horizon/", m.id);
|
||||
for file in &m.content.files {
|
||||
@@ -928,7 +1001,11 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> {
|
||||
overlay.write(file, &bytes)?;
|
||||
if let Some((bundle, id)) = worldgen_entry(file) {
|
||||
let text = String::from_utf8(bytes).map_err(|e| format!("{file}: {e}"))?;
|
||||
worldgen.entry(bundle).or_default().push((id, text));
|
||||
let entries = worldgen.entry(bundle).or_default();
|
||||
match Addition::read(bundle, &text).map_err(|e| format!("{file}: {e}"))? {
|
||||
Some(addition) => additions.entry(bundle).or_default().push(addition),
|
||||
None => entries.push((id, text)),
|
||||
}
|
||||
} else if let Some(template) = file
|
||||
.strip_prefix("data/")
|
||||
.and_then(|r| r.split_once("/structure/"))
|
||||
@@ -1002,7 +1079,8 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> {
|
||||
}
|
||||
overlay.write(BIOME_TAGS, &serde_json::to_vec(&doc)?)?;
|
||||
}
|
||||
// Structures and the pools, sets, processors and features they use.
|
||||
// Structures and the pools, sets, processors and features they use, then
|
||||
// what mods add to sets and pools, in mod and file order.
|
||||
for (bundle, entries) in &worldgen {
|
||||
if !base.join(bundle).exists() {
|
||||
continue;
|
||||
@@ -1011,6 +1089,11 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> {
|
||||
for (id, text) in entries {
|
||||
doc.set(id, J::Str(text.clone()));
|
||||
}
|
||||
for addition in additions.get(bundle).into_iter().flatten() {
|
||||
addition
|
||||
.apply(&mut doc)
|
||||
.map_err(|e| format!("{bundle}: {e}"))?;
|
||||
}
|
||||
overlay.write(bundle, &serde_json::to_vec(&doc)?)?;
|
||||
}
|
||||
// A block without its own blockstate looks like its template, so every
|
||||
|
||||
@@ -576,6 +576,18 @@ fn content_root_carries_a_structure_mod() {
|
||||
"data/huts/tags/worldgen/biome/has_hut.json",
|
||||
r##"{"values": ["#minecraft:is_overworld", "#c:is_plains", "othermod:glade", "minecraft:plains"]}"##,
|
||||
),
|
||||
// Additions to vanilla's set and pool; a target nobody defines is skipped.
|
||||
(
|
||||
"data/huts/worldgen/structure_set/added/villages.json",
|
||||
r#"{"horizon:add_to": "minecraft:villages",
|
||||
"structures": [{"structure": "huts:hut", "weight": 3}]}"#,
|
||||
),
|
||||
(
|
||||
"data/huts/worldgen/template_pool/added/houses.json",
|
||||
r#"{"horizon:add_to": ["othermod:houses", "minecraft:village/plains/houses"],
|
||||
"elements": [{"weight": 2, "element": {"element_type": "minecraft:single_pool_element",
|
||||
"location": "huts:hut", "projection": "rigid", "processors": "minecraft:empty"}}]}"#,
|
||||
),
|
||||
];
|
||||
for (name, text) in files {
|
||||
let path = dir.join(name);
|
||||
@@ -601,6 +613,37 @@ fn content_root_carries_a_structure_mod() {
|
||||
hut["values"],
|
||||
serde_json::json!(["#minecraft:is_overworld", "minecraft:plains"])
|
||||
);
|
||||
// The additions join vanilla's entries at the end and are no set or pool
|
||||
// of their own.
|
||||
let bundle = |name: &str, base: &Path| -> BTreeMap<String, String> {
|
||||
serde_json::from_slice(&fs::read(base.join("worldgen").join(name)).unwrap()).unwrap()
|
||||
};
|
||||
for (name, id, list, added) in [
|
||||
(
|
||||
"structure_sets.json",
|
||||
"minecraft:villages",
|
||||
"structures",
|
||||
serde_json::json!({"structure": "huts:hut", "weight": 3}),
|
||||
),
|
||||
(
|
||||
"template_pools.json",
|
||||
"minecraft:village/plains/houses",
|
||||
"elements",
|
||||
serde_json::json!({"weight": 2, "element": {"element_type": "minecraft:single_pool_element",
|
||||
"location": "huts:hut", "projection": "rigid", "processors": "minecraft:empty"}}),
|
||||
),
|
||||
] {
|
||||
let (ours, vanilla) = (bundle(name, &root), bundle(name, &base));
|
||||
let entries = |text: &str| -> Vec<serde_json::Value> {
|
||||
let v: serde_json::Value = serde_json::from_str(text).unwrap();
|
||||
v[list].as_array().unwrap().clone()
|
||||
};
|
||||
let mut expected = entries(&vanilla[id]);
|
||||
expected.push(added);
|
||||
assert_eq!(entries(&ours[id]), expected, "{id}");
|
||||
assert!(!ours.keys().any(|k| k.starts_with("huts:added/")), "{name}");
|
||||
assert!(!ours.contains_key("othermod:houses"));
|
||||
}
|
||||
|
||||
let assets = Assets::load(&root).unwrap();
|
||||
let biomes = crate::biome::Biomes::load(&root).unwrap();
|
||||
|
||||
@@ -287,6 +287,22 @@ its row. The game reads this content into its block and item tables
|
||||
nobody registers (a block a port left out, another mod's item) is dropped,
|
||||
so the rest of the tag still loads; vanilla would drop the whole tag.
|
||||
|
||||
A mod adds to structure sets and template pools that are not its own
|
||||
(vanilla's villages, another mod's houses) with a set or pool file in its own
|
||||
namespace holding `horizon:add_to`, a key vanilla data never has. Its entries
|
||||
are appended to each set or pool named, after every mod's own files, in mod
|
||||
and file order; the file is no set or pool itself. A name nobody defines is
|
||||
skipped.
|
||||
|
||||
```json
|
||||
{ "horizon:add_to": "minecraft:villages",
|
||||
"structures": [ { "structure": "mymod:village_beach", "weight": 10 } ] }
|
||||
```
|
||||
|
||||
A pool addition holds `"elements"` as a pool does. This is what lithostitched's
|
||||
`add_structure_set_entries` and `add_template_pool_elements` modifiers do on
|
||||
NeoForge and Fabric, and `horizon-port-pack carry` writes them from those.
|
||||
|
||||
## Versions and compatibility
|
||||
|
||||
- The API is the WIT package `horizon:mods@0.1.0`. New functions and
|
||||
|
||||
Reference in New Issue
Block a user