From 4718fef2ab7b5607fa471731f0264bba83c6c991 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 19:53:23 +0200 Subject: [PATCH 1/2] LAN: chest lids open and close for every player A chest a guest opened opened on the host only, and never closed: the host ran the guest's open as its own player's (one opener, a local block event), nothing was sent to the guests, and the guest's close was not counted. Chests now count their openers like ContainerOpenersCounter (the host and each guest; a guest's close, a menu it replaces and its leaving take it off) and the block event goes to the guests too (shulker boxes as well). A guest's client handles ClientboundBlockEventPacket and ticks its block entities (ClientLevel.tickBlockEntities, without the server copy's bell sound and gateway ticks), so lids move on its screen. The control socket's block reply gives a chest's or shulker box's lid openness (lid, 0..1). Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 4 ++ crates/horizon-client/src/game/agent.rs | 13 ++++-- .../src/game/block_entity_anims.rs | 38 +++++++++++++----- crates/horizon-client/src/game/lan.rs | 6 +++ crates/horizon-client/src/game/lan_actions.rs | 40 +++++++++++++++---- crates/horizon-client/src/game/remote.rs | 14 +++++++ 6 files changed, 96 insertions(+), 19 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 0c5b0fdba..b37c07796 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -988,6 +988,9 @@ struct Game<'a> { /// position) for the guests. lan_block_events: Vec<([i32; 3], i32, i32, u16)>, lan_entity_events: Vec<(i32, i8, [f64; 3])>, + /// `ContainerOpenersCounter`: how many players (the host and the guests) + /// have each chest open. + lid_openers: HashMap<(i32, i32, i32), i32>, lan_acting: Option, /// Command feedback to forward to a guest. chat_capture: Option>, @@ -9848,6 +9851,7 @@ pub fn run() -> Result<()> { lan_events: Vec::new(), lan_block_events: Vec::new(), lan_entity_events: Vec::new(), + lid_openers: HashMap::new(), lan_acting: None, chat_capture: None, cmds: Default::default(), diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index 40d31c309..f6232dffd 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -4,7 +4,7 @@ //! take time run as a task advanced every frame and reply when they finish or //! time out, so `horizon-ctl break 10 64 3` returns what the block dropped. use super::{Game, Screen}; -use horizon_client::{Result, items::ItemStack, widgets}; +use horizon_client::{Result, block_entity_anim::Ticker, items::ItemStack, widgets}; use horizon_core::geometry::Vec3; use horizon_world::registry::BlockStateId; use serde_json::{Value, json}; @@ -307,12 +307,19 @@ impl Game<'_> { } fn block_json(&self, pos: [i32; 3]) -> Value { let state = self.world.block(pos[0], pos[1], pos[2]); - json!({ + let mut v = json!({ "pos": pos, "block": &*self.res.facts.get(state).name, "state": self.state_string(state), "loaded": self.world.chunks.contains_key(&(pos[0] >> 4, pos[2] >> 4)), - }) + }); + // How far a chest's or a shulker box's lid is open, 0..1, as drawn. + match self.be_anims.ticker(state.0) { + Some(Ticker::Lid) => v["lid"] = json!(self.chest_lid(pos, 1.0)), + Some(Ticker::Shulker { .. }) => v["lid"] = json!(self.block_anim(pos, 1.0).shulker.0), + _ => {} + } + v } pub(super) fn stack_value(&self, s: &ItemStack) -> Value { let mut v = json!({ "item": &*s.item, "count": s.count }); diff --git a/crates/horizon-client/src/game/block_entity_anims.rs b/crates/horizon-client/src/game/block_entity_anims.rs index c448767c1..446b56b55 100644 --- a/crates/horizon-client/src/game/block_entity_anims.rs +++ b/crates/horizon-client/src/game/block_entity_anims.rs @@ -69,17 +69,19 @@ impl Game<'_> { /// on both halves of a double chest (`CompoundContainer`). pub(super) fn container_opener_event(&mut self, key: (i32, i32, i32), open: bool) { // Spectators never count as openers. - if self.spectator() { - return; + if !self.spectator() { + self.container_openers_changed(key, open); } + } + /// `container_opener_event` for a player who is not a spectator (a guest). + pub(super) fn container_openers_changed(&mut self, key: (i32, i32, i32), open: bool) { let pos = [key.0, key.1, key.2]; let state = self.world.block(pos[0], pos[1], pos[2]).0; match self.be_anims.ticker(state) { Some(Ticker::Lid) => { - let count = i32::from(open); - self.block_entity_event(pos, 1, count); + self.lid_openers_changed(key, open); if let Some(other) = self.chest_other_half(key) { - self.block_entity_event([other.0, other.1, other.2], 1, count); + self.lid_openers_changed(other, open); } } Some(Ticker::Shulker { .. }) => { @@ -90,10 +92,26 @@ impl Game<'_> { shulker.stop_open() }; self.block_entity_event(pos, 1, count); + self.lan_block_event(pos, 1, count); } _ => {} } } + /// `ContainerOpenersCounter.incrementOpeners` / `decrementOpeners` of the + /// chest at `key` (the host and the guests are counted), then + /// `ChestBlockEntity.signalOpenCount`: block event 1 with the count, here + /// and for the guests. + fn lid_openers_changed(&mut self, key: (i32, i32, i32), open: bool) { + let count = self.lid_openers.entry(key).or_default(); + *count = (*count + if open { 1 } else { -1 }).max(0); + let count = *count; + if count == 0 { + self.lid_openers.remove(&key); + } + let pos = [key.0, key.1, key.2]; + self.block_entity_event(pos, 1, count); + self.lan_block_event(pos, 1, count); + } /// `BellBlockEntity.updateEntities`: the living entities whose bounding /// box meets the bell's block box inflated by 48. fn bell_gather(&self, pos: [i32; 3]) -> Vec { @@ -233,7 +251,8 @@ impl Game<'_> { f64::from(pos[1]) + 0.5, f64::from(pos[2]) + 0.5, ]; - if tick.resonate { + // On a server its own copy does this and sends the sound. + if tick.resonate && self.remote.is_none() { // The server's copy plays the resonance for everyone. self.audio .play("minecraft:block.bell.resonate", Some(centre), 1.0, 1.0); @@ -249,8 +268,8 @@ impl Game<'_> { }; self.particles.add(&options, p, [0.0; 3]); } - // makeRaidersGlow. - for m in self.mobs.iter_mut() { + // makeRaidersGlow (the server's effect). + for m in self.mobs.iter_mut().filter(|_| self.remote.is_none()) { if glow.contains(&u64::from(m.id)) { m.add_effect("minecraft:glowing", 60, 0); } @@ -292,7 +311,8 @@ impl Game<'_> { let initial = self.gateway_loaded(pos); let anims = &mut self.be_anims; let server = anims.gateway_servers.entry(pos).or_insert(initial); - let triggered = server.portal_tick(); + // A server sends its copy's block event instead. + let triggered = self.remote.is_none() && server.portal_tick(); let client = anims.gateways.entry(pos).or_insert(initial); if triggered { client.trigger_event(1, 0); diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index 1bce62bb8..83c87b89f 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -635,6 +635,12 @@ impl Game<'_> { } } HostEvent::Left(guest) => { + // The container they had open loses an opener. + if let Some(pos) = guest.menu.as_ref().and_then(|m| m.pos) + && !guest.spectator() + { + self.container_openers_changed(pos, false); + } playerdata::save(&guest, &self.world_root); host.models.remove(&guest.conn); if self.options.dedicated.is_some() && guest.placed { diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 7faba7f6c..192c08ee0 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -200,8 +200,12 @@ impl Game<'_> { host.hub.set_game_mode(conn, new_mode); } host.play.insert(conn, play); - if let Some(pos) = opened { - self.lan_open_container(host, conn, pos); + if let Some(pos) = opened + && !self.lan_open_container(host, conn, pos) + && !host.hub.guests[i].spectator() + { + // Nothing the guest can use opened: its lid closes again. + self.container_openers_changed(pos, false); } if let Some(station) = opened_station { self.lan_station_opened(host, conn, station); @@ -221,19 +225,20 @@ impl Game<'_> { self.creative = self.mode == GameMode::Creative; } - /// Hand a container the guest opened over (ServerPlayer.openMenu). - fn lan_open_container(&mut self, host: &mut LanHost, conn: u32, pos: (i32, i32, i32)) { + /// Hand a container the guest opened over (ServerPlayer.openMenu); + /// false when it is not one a guest can open. + fn lan_open_container(&mut self, host: &mut LanHost, conn: u32, pos: (i32, i32, i32)) -> bool { let Some(entity) = self.world.block_entities.get(&pos) else { - return; + return false; }; let (menu, title) = match entity { BlockEntity::Chest(items) => match items.len() { 27 => ("minecraft:generic_9x3", "container.chest"), 9 => ("minecraft:generic_3x3", "container.dispenser"), 5 => ("minecraft:hopper", "container.hopper"), - _ => return, + _ => return false, }, - _ => return, + _ => return false, }; let name = &self .res @@ -247,6 +252,8 @@ impl Game<'_> { _ => title, }; let items = entity.slots().to_vec(); + // ServerPlayer.openMenu closes the menu that was open. + self.guest_container_closed(host, conn); host.hub.open_container( conn, menu, @@ -254,6 +261,18 @@ impl Game<'_> { pos, items, ); + true + } + + /// The guest's open container is closing (`AbstractContainerMenu.removed`): + /// it no longer counts among the openers of the lid. + pub(super) fn guest_container_closed(&mut self, host: &mut LanHost, conn: u32) { + if let Some(g) = host.hub.guest(conn) + && let Some(pos) = g.menu.as_ref().and_then(|m| m.pos) + && !g.spectator() + { + self.container_openers_changed(pos, false); + } } /// A packet from a guest that the hub does not handle itself. @@ -405,6 +424,13 @@ impl Game<'_> { self.drop_guest_items(host, gi, drops); } S::ContainerClose(p) => { + if host.hub.guests[gi] + .menu + .as_ref() + .is_some_and(|m| m.window == p.container) + { + self.guest_container_closed(host, conn); + } host.hub.container_closed(conn, p.container); let g = &mut host.hub.guests[gi]; if let Some(c) = g.carried.take() { diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 6ad981151..b06a6e30e 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -831,6 +831,18 @@ impl Game<'_> { } } C::BlockChangedAck(_) => {} + // ClientLevel.blockEvent: BlockEntity.triggerEvent (chest lids, + // shulker boxes, bells) when the block is still the one it names. + C::BlockEvent(p) => { + let state = self.world.block(p.pos[0], p.pos[1], p.pos[2]); + let block = &self.res.facts.get(state).name; + let named = self.remote.as_ref().and_then(|r| { + r.builtin.get("minecraft:block")?.name(p.block).map(str::to_owned) + }); + if named.as_deref() == Some(&**block) { + self.block_entity_event(p.pos, i32::from(p.b0), i32::from(p.b1)); + } + } C::LevelEvent(p) if p.kind == 2015 => { self.dragon_egg_particles(p.pos, p.data); } @@ -1694,6 +1706,8 @@ impl Game<'_> { // ClientLevel.tick: the border's size change. self.border.tick(); self.tick_remote_entities(); + // ClientLevel.tickBlockEntities: chest lids, shulker boxes, bells. + self.tick_block_entity_anims(); self.tick_hand(); self.tick_particles(); let Some(player) = self.player.as_mut() else { From 8d6b8b3f03d3b5f828da3e7df5402de8d2466fd2 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:33:01 +0200 Subject: [PATCH 2/2] LAN: a trading screen a guest opens releases the chest it replaces Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/lan_trading.rs | 2 ++ crates/horizon-client/src/game/remote.rs | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game/lan_trading.rs b/crates/horizon-client/src/game/lan_trading.rs index 8b033f8b2..d6bfc4141 100644 --- a/crates/horizon-client/src/game/lan_trading.rs +++ b/crates/horizon-client/src/game/lan_trading.rs @@ -55,6 +55,8 @@ impl Game<'_> { state.session.update_special_prices(); } let title = self.merchant_display_name(&state); + // The container the trading screen replaces loses its opener. + self.guest_container_closed(host, conn); let Some(window) = host.hub.open_merchant_window(conn, title) else { self.merchant_closed(&mut state); return; diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index b06a6e30e..0a09ec70b 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -837,7 +837,10 @@ impl Game<'_> { let state = self.world.block(p.pos[0], p.pos[1], p.pos[2]); let block = &self.res.facts.get(state).name; let named = self.remote.as_ref().and_then(|r| { - r.builtin.get("minecraft:block")?.name(p.block).map(str::to_owned) + r.builtin + .get("minecraft:block")? + .name(p.block) + .map(str::to_owned) }); if named.as_deref() == Some(&**block) { self.block_entity_event(p.pos, i32::from(p.b0), i32::from(p.b1));