Proto chunks: save placed block entities with their id

Block entity data a structure places can lack its type: a brushable
block's is only its loot table. Finished chunks get the id from the block,
saved proto chunks did not. Vanilla and Paper skip a pending block entity
without one when they finish the chunk ("Skipping block entity with
invalid type"), so suspicious sand and gravel at the edge of a
pre-generated area lost their archaeology loot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
FusselTV
2026-10-25 19:45:42 +01:00
parent 57c5f81299
commit 9a0fcda3a9
2 changed files with 75 additions and 0 deletions
+8
View File
@@ -434,6 +434,14 @@ impl ChunkGenerator for VanillaTerrain {
.iter()
.map(|(pos, nbt)| {
let mut nbt = nbt.clone();
// Placed data can lack its type (a brushable block's loot table
// alone); vanilla saves every pending block entity with its id,
// and skips one without when the chunk finishes.
if let Some(id) =
crate::lan::chunks::block_entity_type(chunk.block_name(pos[0], pos[1], pos[2]))
{
nbt.insert("id".into(), Tag::String(id.into()));
}
for (key, v) in ["x", "y", "z"].into_iter().zip(pos) {
nbt.insert(key.into(), Tag::Int(*v));
}
@@ -315,3 +315,70 @@ fn saved_proto_chunks_restore_exactly() {
drop(save);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn saved_proto_block_entities_keep_their_id() {
use horizon_client::{
save::{Persistent, SaveDir},
vanillagen::VanillaTerrain,
};
use horizon_world::{
nbt::{Limits, Tag},
region::RegionFile,
};
let Some(root) = resources() else {
return;
};
let assets = Arc::new(Assets::load(&root).unwrap());
let biomes = Arc::new(Biomes::load(&root).unwrap());
let terrain = VanillaTerrain::for_dimension(
&root.join("worldgen"),
assets.registry,
&biomes,
12345,
"minecraft:overworld",
)
.unwrap();
let dir = std::env::temp_dir().join(format!("horizon-proto-ids-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let save = Arc::new(SaveDir::new(dir.clone()));
let generator = Persistent {
inner: Arc::new(terrain),
save: save.clone(),
registry: assets.clone(),
biomes,
};
let mut worker = generator.worker();
// A cold ocean ruin of seed 12345: its suspicious gravel is placed with a
// loot table and no type.
let keys: Vec<(i32, i32)> = (0..9).map(|i| (43 + i % 3 - 1, 21 + i / 3 - 1)).collect();
let mut region: [Proto; 9] =
std::array::from_fn(|i| generator.proto(&mut worker, keys[i].0, keys[i].1));
generator.decorate(&mut worker, &mut region);
generator.save_protos(&region.iter().collect::<Vec<_>>());
save.flush().unwrap();
let mut file = RegionFile::open(dir.join("region/r.1.0.mca")).unwrap();
let mut suspicious = 0;
for &(x, z) in &keys {
let Some(record) = file
.read(horizon_core::position::ChunkPos::new(x, z))
.unwrap()
else {
continue;
};
let root = record.nbt(Limits::default()).unwrap();
let Some(Tag::List(entities)) = root.tag.get("block_entities") else {
continue;
};
for e in entities {
assert!(e.get("id").is_some(), "chunk {x} {z}: {e:?}");
if let Some(Tag::String(table)) = e.get("LootTable") {
suspicious += usize::from(table.to_string_lossy().contains("archaeology/"));
}
}
}
assert!(suspicious > 0, "the ruin placed no suspicious blocks");
drop(generator);
drop(save);
let _ = std::fs::remove_dir_all(&dir);
}