From 83bdb31eeae4199e89e573cfba8cab0b18ffbf02 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Tue, 27 Oct 2026 12:09:52 +0100 Subject: [PATCH] refactor: SavedProto lives in chunk.rs streaming's ChunkGenerator trait named save::SavedProto, which made streaming depend on save, one of the edges that keep the client lib in a single cycle. The plain data type moves beside ChunkExtras in chunk.rs and save re-exports it. No behaviour change. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/chunk.rs | 124 ++++++++++++++----------- crates/horizon-client/src/save.rs | 17 +--- crates/horizon-client/src/streaming.rs | 4 +- 3 files changed, 73 insertions(+), 72 deletions(-) diff --git a/crates/horizon-client/src/chunk.rs b/crates/horizon-client/src/chunk.rs index f3df1ea14..b0dd377c7 100644 --- a/crates/horizon-client/src/chunk.rs +++ b/crates/horizon-client/src/chunk.rs @@ -310,6 +310,76 @@ impl Chunk { } } +/// A proto chunk saved before it became final. Decoration writes into a +/// chunk's neighbours, and decorated chunks wait for theirs, so the chunks +/// past the finished ones hold writes that generating them again would lose +/// (whole structure pieces, trees, ores). Vanilla saves its proto chunks too. +pub struct SavedProto { + /// Blocks, scheduled ticks (in `saved`) and post-processing marks. + pub column: Chunk, + /// Decorated itself (`features`), not only written to by its neighbours + /// (`terrain`). + pub decorated: bool, + /// Block entity data in placement order, with `x`, `y` and `z`. + pub block_entities: Vec, + /// Entities placed by structures, with absolute `Pos`. + pub entities: Vec, +} + +/// A container's loot table and what Horizon rolled from it. Vanilla keeps +/// `LootTable` and `LootTableSeed` until the container is first opened; +/// Horizon rolls when it generates or loads the chunk, and saves the table +/// again while the container still holds exactly the rolled items. +#[derive(Clone, Debug, PartialEq)] +pub struct RolledLoot { + pub pos: (i32, i32, i32), + pub table: String, + /// The saved `LootTableSeed`, written back unless 0 (trySaveLootTable). + pub seed: Option, + /// The rolled `Items`. + pub items: Tag, +} +impl RolledLoot { + /// Puts the loot table back in place of `Items` while they are still the + /// roll; false once the container holds anything else. + pub fn restore(&self, map: &mut Compound) -> bool { + if map.get(&"Items".into()) != Some(&self.items) { + return false; + } + map.remove(&"Items".into()); + map.insert("LootTable".into(), Tag::String(self.table.as_str().into())); + if let Some(seed) = self.seed.filter(|s| *s != 0) { + map.insert("LootTableSeed".into(), Tag::Long(seed)); + } + true + } +} + +/// Save data loaded with a chunk that Horizon keeps but does not simulate. +#[derive(Clone, Debug, Default)] +pub struct ChunkExtras { + /// Chunk root keys written back as loaded (structures, PostProcessing, + /// InhabitedTime, blending data, ...). + pub root: Compound, + /// Block entities Horizon does not model. + pub block_entities: Vec, + /// 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. + pub block_ticks: Vec, + pub fluid_ticks: Vec, + /// Game time written as `LastUpdate`. + pub last_update: i64, + /// Containers still holding their loot table's roll. + pub rolled_loot: Vec, +} + #[cfg(test)] mod tests { use super::*; @@ -433,57 +503,3 @@ mod tests { } } } - -/// A container's loot table and what Horizon rolled from it. Vanilla keeps -/// `LootTable` and `LootTableSeed` until the container is first opened; -/// Horizon rolls when it generates or loads the chunk, and saves the table -/// again while the container still holds exactly the rolled items. -#[derive(Clone, Debug, PartialEq)] -pub struct RolledLoot { - pub pos: (i32, i32, i32), - pub table: String, - /// The saved `LootTableSeed`, written back unless 0 (trySaveLootTable). - pub seed: Option, - /// The rolled `Items`. - pub items: Tag, -} -impl RolledLoot { - /// Puts the loot table back in place of `Items` while they are still the - /// roll; false once the container holds anything else. - pub fn restore(&self, map: &mut Compound) -> bool { - if map.get(&"Items".into()) != Some(&self.items) { - return false; - } - map.remove(&"Items".into()); - map.insert("LootTable".into(), Tag::String(self.table.as_str().into())); - if let Some(seed) = self.seed.filter(|s| *s != 0) { - map.insert("LootTableSeed".into(), Tag::Long(seed)); - } - true - } -} - -/// Save data loaded with a chunk that Horizon keeps but does not simulate. -#[derive(Clone, Debug, Default)] -pub struct ChunkExtras { - /// Chunk root keys written back as loaded (structures, PostProcessing, - /// InhabitedTime, blending data, ...). - pub root: Compound, - /// Block entities Horizon does not model. - pub block_entities: Vec, - /// 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. - pub block_ticks: Vec, - pub fluid_ticks: Vec, - /// Game time written as `LastUpdate`. - pub last_update: i64, - /// Containers still holding their loot table's roll. - pub rolled_loot: Vec, -} diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index a325a455f..9d47f819c 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -339,7 +339,7 @@ impl SaveTables { } } -pub use crate::chunk::{ChunkExtras, RolledLoot}; +pub use crate::chunk::{ChunkExtras, RolledLoot, SavedProto}; fn tick_list(ticks: &[SavedTick], name: impl Fn(u32) -> Option) -> Tag { Tag::List( @@ -1476,21 +1476,6 @@ fn decode_column( Some(chunk) } -/// A proto chunk saved before it became final. Decoration writes into a -/// chunk's neighbours, and decorated chunks wait for theirs, so the chunks -/// past the finished ones hold writes that generating them again would lose -/// (whole structure pieces, trees, ores). Vanilla saves its proto chunks too. -pub struct SavedProto { - /// Blocks, scheduled ticks (in `saved`) and post-processing marks. - pub column: Chunk, - /// Decorated itself (`features`), not only written to by its neighbours - /// (`terrain`). - pub decorated: bool, - /// Block entity data in placement order, with `x`, `y` and `z`. - pub block_entities: Vec, - /// Entities placed by structures, with absolute `Pos`. - pub entities: Vec, -} pub fn encode_proto( proto: &SavedProto, registry: &BlockRegistry, diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 619a0202c..6588ac32d 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -75,11 +75,11 @@ pub trait ChunkGenerator: Send + Sync + 'static { fn light_view(&self, _proto: &Proto) -> Option { None } - fn saved_proto(&self, _proto: &Proto) -> Option { + fn saved_proto(&self, _proto: &Proto) -> Option { None } /// Put a saved proto chunk's state back onto `proto`, generated again. - fn restore_proto(&self, _proto: &mut Proto, _saved: crate::save::SavedProto) {} + fn restore_proto(&self, _proto: &mut Proto, _saved: crate::chunk::SavedProto) {} /// Save proto chunks decoration wrote to, so that they come back as they /// were: generating them again would lose those writes. fn save_protos(&self, _protos: &[&Proto]) {}