From f03990f663e658379dd51790342205a57b263871 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sat, 17 Oct 2026 01:45:15 +0200 Subject: [PATCH] Integrated server: control socket and chat read the server's side With single player on its server thread, the window is the server's client, and some of what it answered was its own empty side: - `horizon-ctl save` and `save-all` returned ok without saving: the window's save skips worlds it is connected to. They now save on the server thread (`Integrated::call`). - `horizon-ctl chat-log` read the window's local chat, which never showed what the server said (command feedback, joins). It reads the connection's chat, as the agent's chat query already did. - The game's own messages (Open to LAN, screenshots...) went to that local chat, which is not drawn while connected: they now join the connection's chat. - `state` names the world (the server thread's `LevelName`). - The client follows its permission level (EntityEvent 24..28, LocalPlayer.setPermissionLevel), which the commands the chat offers and checks depend on. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/agent.rs | 5 ++- crates/horizon-client/src/game/chat.rs | 6 ++++ crates/horizon-client/src/game/control.rs | 38 ++++++++++++++++------- crates/horizon-client/src/game/remote.rs | 10 +++++- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index cd39606b3..48b24bbb3 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -460,7 +460,10 @@ impl Game<'_> { json!({ "screen": self.screen_label(), "menu": self.title.as_ref().map(|_| self.title_value()), - "world": (self.title.is_none()).then(|| self.level.name.clone()), + "world": (self.title.is_none()).then(|| match &self.integrated { + Some(server) => server.world_name.clone(), + None => self.level.name.clone(), + }), "dimension": self.dim.id(), "gamemode": format!("{:?}", self.mode).to_lowercase(), "time": { "day_time": self.world.day_time, "day": self.world.day_time.div_euclid(24000), "time_of_day": self.world.day_time.rem_euclid(24000) }, diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index 8f6bed0ca..bcf6426e1 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -425,6 +425,12 @@ impl Game<'_> { capture.push(text); return; } + // Connected to a server (single player's own included), the chat on + // screen is the connection's: the game's own messages join it. + if self.remote.is_some() { + self.chat_line(text.plain()); + return; + } let focused = self.screen == Screen::Chat; let heard = self.mods.mods.is_running().then(|| text.plain()); self.ui.chat.add( diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index e1169b2c8..7b51fe8ee 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -475,7 +475,10 @@ impl Game<'_> { } "chat-log" => { let n = words.get(1).map_or(Ok(10), |n| n.parse())?; - Ok(Some(json!({ "chat": self.ui.chat.recent_lines(n) }))) + let lines = self + .remote_chat(n) + .unwrap_or_else(|| self.ui.chat.recent_lines(n)); + Ok(Some(json!({ "chat": lines }))) } "stats" => { let summary = self.frame_stats.summary(); @@ -549,18 +552,20 @@ impl Game<'_> { self.leave_server(None)?; ok() } - "save" => { + "save" | "save-all" => { self.require_world()?; - self.save_game()?; - ok() - } - "save-all" => { - // Like `save-all flush`: also rewrite chunks that were not modified - // (the save round-trip harness compares them with vanilla's files). - self.require_world()?; - let keys: Vec<_> = self.world.chunks.keys().copied().collect(); - self.world.modified.extend(keys); - self.save_game()?; + // `save-all` is like `save-all flush`: it also rewrites chunks + // that were not modified (the save round-trip harness compares + // them with vanilla's files). + let all = words[0] == "save-all"; + // Single player's world is its server thread's. + if let Some(server) = &self.integrated { + server + .call(move |g| g.control_save(all).map_err(|e| e.to_string())) + .ok_or("the integrated server stopped")??; + } else { + self.control_save(all)?; + } ok() } "dragon-hurt" => { @@ -581,6 +586,15 @@ impl Game<'_> { } } + /// `horizon-ctl save` and `save-all`. + fn control_save(&mut self, all: bool) -> Result<()> { + if all { + let keys: Vec<_> = self.world.chunks.keys().copied().collect(); + self.world.modified.extend(keys); + } + self.save_game() + } + fn require_world(&self) -> Result<()> { if self.title.is_some() { return Err("no world open; use: open WORLD [SEED] [creative]".into()); diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 8e4e973f2..c0829b43b 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -426,7 +426,7 @@ impl Game<'_> { } Some(stack) } - fn chat_line(&mut self, text: String) { + pub(super) fn chat_line(&mut self, text: String) { log::info!("[chat] {text}"); if let Some(r) = self.remote.as_mut() { r.chat.push((text, Instant::now())); @@ -1133,6 +1133,14 @@ impl Game<'_> { } } C::EntityEvent(p) => { + // EntityEvent.PERMISSION_LEVEL_ALL..OWNERS (24..=28) for this + // player: LocalPlayer.setPermissionLevel, which the commands + // the chat offers and checks follow. + if (24..=28).contains(&p.event) + && self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) + { + self.ui.registries.permission = (p.event - 24) as u8; + } if p.event == 1 && let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) && m.species() == horizon_client::mobs::Species::Rabbit