Mods: game.world-id, so mods keep state per world (API level 9)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 11:25:45 +03:00
committed by airdef
parent 9914ddf58b
commit 69987c52dc
6 changed files with 19 additions and 1 deletions
+1
View File
@@ -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),
+3
View File
@@ -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<String>,
#[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
+4
View File
@@ -342,6 +342,10 @@ impl Game<'_> {
cookies: Option<horizon_client::net::Cookies>,
) -> 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())
+4
View File
@@ -47,6 +47,10 @@ interface game {
get-player: func() -> option<player>;
/// None on the title screen.
get-clock: func() -> option<clock>;
/// Which world is open (AirDef fork): `world:<folder>` for a saved
/// world, `server:<address>` on a server; none on the title screen.
/// Mods keep their state apart by it.
world-id: func() -> option<string>;
/// A block along the player's view.
record hit {
/// The block hit.
+2 -1
View File
@@ -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
+5
View File
@@ -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<String>,
}
/// 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<Clock> {
self.snapshot.clock
}
fn world_id(&mut self) -> Option<String> {
self.snapshot.world.clone()
}
fn raycast(&mut self, distance: f32) -> Option<Hit> {
let (at, hit) = self.snapshot.view_hit.as_ref()?;
// NaN compares false: no hit.