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")],