Files
airdef/horizon-patches/0009-Mods-game.world-id-so-mods-keep-state-per-world-API-.patch
T
2026-10-30 15:43:11 +03:00

127 lines
5.2 KiB
Diff

From 065d9fa02966238a5bd9ff460392a1a809db0b11 Mon Sep 17 00:00:00 2001
From: chexit <chexit@duck.com>
Date: Thu, 29 Oct 2026 11:25:45 +0300
Subject: [PATCH 09/36] Mods: game.world-id, so mods keep state per world (API
level 9)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
crates/horizon-client/src/game.rs | 1 +
crates/horizon-client/src/game/mods.rs | 3 +++
crates/horizon-client/src/game/remote.rs | 4 ++++
crates/horizon-mod-sdk/wit/horizon.wit | 4 ++++
crates/horizon-mods/src/api_level.rs | 3 ++-
crates/horizon-mods/src/runtime.rs | 5 +++++
6 files changed, 19 insertions(+), 1 deletion(-)
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<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
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<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())
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<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.
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<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.
--
2.54.0 (Apple Git-157)