Structure templates read without a node cap, as vanilla does

Vanilla reads structure templates with NbtAccounter.unlimitedHeap()
(TemplateSource.readStructure). Horizon used the default 1,000,000-node
cap, so a template past it loaded as empty: YUNG's Better Ocean
Monuments' start piece (115x64x126 blocks) never started. Limits::templates
keeps the 256 MiB byte cap and drops the node cap, for worldgen and /place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-18 09:54:31 +02:00
parent c1b4ccd571
commit 571066158e
3 changed files with 27 additions and 12 deletions
@@ -53,13 +53,7 @@ impl Game<'_> {
let Ok(raw) = Compression::Gzip.decode(&bytes, 1 << 28) else {
continue;
};
let Ok((named, _)) = nbt::read_named(
&raw,
Limits {
max_bytes: 1 << 28,
..Limits::default()
},
) else {
let Ok((named, _)) = nbt::read_named(&raw, Limits::templates()) else {
continue;
};
return Some(Arc::new(StructureTemplate::from_nbt(
+25
View File
@@ -110,6 +110,19 @@ impl Limits {
max_depth: 512,
}
}
/// Structure templates (`structure/*.nbt`): vanilla reads them with
/// `NbtAccounter.unlimitedHeap()` (TemplateSource.readStructure), and
/// mods ship templates past the default node cap (YUNG's Better Ocean
/// Monuments' start piece, 115x64x126 blocks, holds millions of tags).
/// The byte cap stays as the only guard on the decompressed file.
pub fn templates() -> Self {
Self {
max_bytes: 1 << 28,
max_nodes: usize::MAX,
max_depth: 512,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -836,6 +849,18 @@ mod tests {
assert!(read_network(&bytes, limits).is_err());
}
#[test]
fn templates_read_past_the_default_node_cap() {
// A big mod template (YUNG's monument start) holds more tags than the
// default cap; vanilla reads templates with an unlimited accounter.
let root = NamedTag {
name: JavaString::default(),
tag: Tag::compound([("blocks", Tag::List(vec![Tag::Byte(1); 1_000_001]))]),
};
let bytes = write_named(&root, Limits::templates()).unwrap();
assert!(read_named(&bytes, Limits::default()).is_err());
assert_eq!(read_named(&bytes, Limits::templates()).unwrap().0, root);
}
#[test]
fn depth_boundary_and_every_truncated_prefix() {
let mut tag = Tag::Int(1);
for _ in 0..512 {
@@ -342,11 +342,7 @@ impl StructureResources {
flate2::read::GzDecoder::new(bytes.as_slice())
.read_to_end(&mut raw)
.ok()?;
let limits = Limits {
max_bytes: 1 << 28,
..Limits::default()
};
let tag = nbt::read_named(&raw, limits).ok()?.0.tag;
let tag = nbt::read_named(&raw, Limits::templates()).ok()?.0.tag;
Some(Arc::new(StructureTemplate::from_nbt(
&tag,
self.registry,