From d93b347b417036c52fe1e6a361bc61a2ca5f16a0 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 00:00:26 +0200 Subject: [PATCH] Loaded spawners keep their mob; chunk block entities in vanilla's order A saved spawner's block entity id is minecraft:mob_spawner, not its block's name, so decode_block_entity found no block entity for it and dropped it: every spawner loaded from a save (a vanilla world's dungeons and mineshafts, or Horizon's own after a reload) lost its spawn data, and the server sent its chunk with no update tag. The id now maps to the spawner block. The chunk packet lists block entities in LevelChunk.blockEntities' order, which Horizon reproduces from the position hashes; positions sharing a bucket keep their insertion order, which for a loaded chunk is the save's block_entities list (SerializableChunkData -> pendingBlockEntities). That list order is now kept with the chunk and used as the insertion order. dedicated_server: 542 transcript lines identical (was 10 differing). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/lan/chunks.rs | 16 +++++++++++ crates/horizon-client/src/save.rs | 36 ++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/lan/chunks.rs b/crates/horizon-client/src/lan/chunks.rs index 54e385383..6ca321edd 100644 --- a/crates/horizon-client/src/lan/chunks.rs +++ b/crates/horizon-client/src/lan/chunks.rs @@ -263,6 +263,22 @@ impl ChunkEncoder { } sections.push(encoded); } + // A loaded chunk's block entities enter its maps in the save's order + // (ChunkAccess.pendingBlockEntities, then LevelChunk.blockEntities): + // keys that share a bucket keep that order. Others follow in scan order. + if let Some(order) = chunk.saved.as_ref().map(|s| &s.block_entity_order) + && !order.is_empty() + { + let at = |e: &BlockEntityInfo| { + let pos = ( + chunk.x * 16 + i32::from(e.packed_xz as u8 >> 4), + i32::from(e.y), + chunk.z * 16 + i32::from(e.packed_xz as u8 & 15), + ); + order.iter().position(|p| *p == pos).unwrap_or(usize::MAX) + }; + entity_infos.sort_by_key(at); + } java_hash_order(&mut entity_infos, |e| { ( chunk.x * 16 + i32::from(e.packed_xz as u8 >> 4), diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 7a90871c2..cce33949f 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -233,6 +233,9 @@ pub struct ChunkExtras { /// The NBT of the modelled block entities as loaded, to restore the fields and /// detail Horizon does not model when it saves them (`block_entity_nbt::overlay`). pub block_entity_sources: Vec<((i32, i32, i32), Tag)>, + /// Every saved block entity's position in the order of the save's list, + /// the order `SerializableChunkData` puts them in the chunk's maps. + pub block_entity_order: Vec<(i32, i32, i32)>, /// Scheduled ticks (`block_ticks`/`fluid_ticks`): delays relative to the /// save time. Registered with the level's tick scheduler on arrival and /// replaced with the scheduler's pending ticks when saving. @@ -532,7 +535,12 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain }; return Some((pos, BlockEntity::Beacon(Box::new(beacon)))); } - let mut entity = BlockEntity::for_block(&id)?; + // A spawner's block entity id is not its block's name. + let block = match id.as_str() { + "minecraft:mob_spawner" => "minecraft:spawner", + other => other, + }; + let mut entity = BlockEntity::for_block(block)?; match &mut entity { BlockEntity::Chest(items) => *items = decode_items(tag.get("Items"), items.len()), BlockEntity::Furnace(f) => { @@ -1030,6 +1038,10 @@ pub fn decode_with( }; if let Some(Tag::List(entities)) = tag.get("block_entities") { for e in entities { + let int = |k: &str| number(e.get(k)).map(|v| v as i32); + if let (Some(x), Some(y), Some(z)) = (int("x"), int("y"), int("z")) { + extras.block_entity_order.push((x, y, z)); + } match decode_block_entity(e) { Some(be) => { if let Some(name) = text(e.get("CustomName")) { @@ -2546,6 +2558,28 @@ mod tests { assert_eq!(back.redstone_signal(), 2); } #[test] + fn spawner_block_entity_round_trips() { + // A dungeon spawner as vanilla saves it (`id` is mob_spawner, not the block). + let saved = horizon_world::nbt::parse_snbt( + r#"{id:"minecraft:mob_spawner",x:67,y:-52,z:-64,Delay:20s,SpawnData:{entity:{id:"minecraft:zombie"}},SpawnPotentials:[]}"#, + ) + .unwrap() + .0; + let (pos, back) = decode_block_entity(&saved).expect("spawner"); + assert_eq!(pos, (67, -52, -64)); + let crate::containers::BlockEntity::Spawner(spawner) = &back else { + panic!("not a spawner"); + }; + let next = spawner.next_data().and_then(|d| d.id()); + assert_eq!(next.as_deref(), Some("minecraft:zombie")); + let tag = encode_block_entity(pos, &back, "minecraft:spawner"); + let (_, again) = decode_block_entity(&tag).expect("spawner again"); + let crate::containers::BlockEntity::Spawner(again) = again else { + panic!("not a spawner"); + }; + assert_eq!(again.update_tag(), spawner.update_tag()); + } + #[test] fn banner_block_entity_round_trips() { let banner = crate::containers::Banner { layers: vec![crate::banner::BannerLayer::new("minecraft:creeper", "lime")],