diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index e5ffb30c4..c6b642345 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -8918,6 +8918,7 @@ impl Game<'_> { create: Option<&horizon_client::title::WorldOptions>, ) -> Result<()> { world_loading::world_data_used(); + self.mods.world_id = Some(format!("world:{folder}")); if self.defer_world_entry(PendingWorldEntry::Local { folder: folder.to_owned(), name: name.map(str::to_owned), diff --git a/crates/horizon-client/src/game/mods.rs b/crates/horizon-client/src/game/mods.rs index 3eef96aa3..ebdd18a24 100644 --- a/crates/horizon-client/src/game/mods.rs +++ b/crates/horizon-client/src/game/mods.rs @@ -38,6 +38,8 @@ use { #[derive(Default)] pub(super) struct ModsState { + /// The open world, for `game.world-id` (AirDef fork): its folder, or the server's address. + pub(super) world_id: Option, #[cfg(feature = "mods")] pub(super) mods: Mods, /// Capability `input`: a claimed item's use while the button is down. @@ -482,6 +484,7 @@ impl Game<'_> { known_recipes, placing: self.mod_recipe_screen(), creative: self.creative, + world: self.player.is_some().then(|| self.mods.world_id.clone()).flatten(), } } /// The first block along the player's view within diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 22b7c13df..89df0616b 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -342,6 +342,10 @@ impl Game<'_> { cookies: Option, ) -> Result<()> { self.perf.world_opening(); + // A server's own world; joining the world this window hosts keeps its folder. + if local.is_none() { + self.mods.world_id = Some(format!("server:{address}")); + } let root = self.options.resources.clone(); let builtin = Builtin::find(&root) .and_then(|p| Builtin::load(&p).ok()) diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit index e30f3a30e..b6758d9fb 100644 --- a/crates/horizon-mod-sdk/wit/horizon.wit +++ b/crates/horizon-mod-sdk/wit/horizon.wit @@ -47,6 +47,10 @@ interface game { get-player: func() -> option; /// None on the title screen. get-clock: func() -> option; + /// Which world is open (AirDef fork): `world:` for a saved + /// world, `server:
` on a server; none on the title screen. + /// Mods keep their state apart by it. + world-id: func() -> option; /// A block along the player's view. record hit { /// The block hit. diff --git a/crates/horizon-mods/src/api_level.rs b/crates/horizon-mods/src/api_level.rs index 9d15851b4..cf2993186 100644 --- a/crates/horizon-mods/src/api_level.rs +++ b/crates/horizon-mods/src/api_level.rs @@ -13,7 +13,7 @@ pub const LEVEL: u32 = 9; /// level. Level 0 is the contract of Horizon 0.2.67 to 0.2.69. A new /// function takes the next level and raises [`LEVEL`] (the `api_reference` /// test fails until it is listed here). -pub const FUNCTIONS: [(&str, u32); 55] = [ +pub const FUNCTIONS: [(&str, u32); 56] = [ ("log.write", 0), ("game.get-player", 0), ("game.get-clock", 0), @@ -70,6 +70,7 @@ pub const FUNCTIONS: [(&str, u32); 55] = [ ("render.clear-post", 8), ("render.set-params", 8), ("render.set-views", 9), + ("game.world-id", 9), ]; /// What the game's program carries so the launcher can read its level diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index 1389407be..abed5b31f 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -168,6 +168,8 @@ pub struct Snapshot { pub placing: bool, /// The player is in creative mode (`recipes.give`). pub creative: bool, + /// The open world (`game.world-id`, AirDef fork). + pub world: Option, } /// Capability `recipes`'s tables: `items` in the registry's order, which @@ -1475,6 +1477,9 @@ impl game::Host for State { fn get_clock(&mut self) -> Option { self.snapshot.clock } + fn world_id(&mut self) -> Option { + self.snapshot.world.clone() + } fn raycast(&mut self, distance: f32) -> Option { let (at, hit) = self.snapshot.view_hit.as_ref()?; // NaN compares false: no hit.