diff --git a/crates/horizon-client/src/game/block_entity_ticks.rs b/crates/horizon-client/src/game/block_entity_ticks.rs index 094aed6ef..389807d9b 100644 --- a/crates/horizon-client/src/game/block_entity_ticks.rs +++ b/crates/horizon-client/src/game/block_entity_ticks.rs @@ -15,6 +15,7 @@ use horizon_world::registry::BlockStateId; impl Game<'_> { pub(super) fn tick_block_entities(&mut self) -> Result<()> { self.tick_mount_menu(); + self.tick_container_reach(); self.load_entity_types(); if !self.world.block_entities.has_states() { let states = self.ticking_states(); diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 2c7019c73..c6d1b5302 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -812,6 +812,31 @@ impl Game<'_> { } } + /// `ServerPlayer.tick`'s `stillValid`: a guest's block container closes + /// once the guest is out of reach of its block (a removed block closes it + /// in the hub's `sync_containers`). + fn lan_containers_tick(&mut self, host: &mut LanHost) { + let far: Vec = host + .hub + .guests + .iter() + .filter(|g| host.hub.here(g)) + .filter(|g| { + g.menu.as_ref().is_some_and(|m| { + m.slots.valid(&self.world) && !m.slots.still_valid(&self.world, g, m.pos) + }) + }) + .map(|g| g.conn) + .collect(); + for conn in far { + self.guest_container_closed(host, conn); + host.hub.close_container(conn); + if let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) { + self.guest_carried_back(host, gi); + } + } + } + /// A packet from a guest that the hub does not handle itself. pub(super) fn lan_guest_packet(&mut self, host: &mut LanHost, conn: u32, packet: S) { let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else { @@ -1680,6 +1705,7 @@ impl Game<'_> { pub(super) fn tick_guests(&mut self, host: &mut LanHost) { self.lan_merchant_tick(host); self.lan_stations_tick(host); + self.lan_containers_tick(host); self.tick_lan_mount_menus(host); self.announce_guest_advancements(host); let conns: Vec = host diff --git a/crates/horizon-client/src/game/lan_stations.rs b/crates/horizon-client/src/game/lan_stations.rs index 2efcfc705..49a90b7b9 100644 --- a/crates/horizon-client/src/game/lan_stations.rs +++ b/crates/horizon-client/src/game/lan_stations.rs @@ -665,13 +665,5 @@ pub(super) fn station_inputs(gs: &GuestStation) -> Vec { /// its block interaction range (4.5, creative 5) and a 4-block buffer of the /// block's box. pub(super) fn within_reach(g: &horizon_client::lan::guest::Guest, pos: [i32; 3]) -> bool { - let eye = [g.position[0], g.position[1] + g.eye_height(), g.position[2]]; - let reach = if g.creative() { 5.0 } else { 4.5 } + 4.0; - (0..3) - .map(|k| { - let lo = f64::from(pos[k]); - (lo - eye[k]).max(eye[k] - lo - 1.0).max(0.0).powi(2) - }) - .sum::() - < reach * reach + g.within_block_range(pos, 4.0) } diff --git a/crates/horizon-client/src/game/storage.rs b/crates/horizon-client/src/game/storage.rs index 9b28ce26b..9132c02a8 100644 --- a/crates/horizon-client/src/game/storage.rs +++ b/crates/horizon-client/src/game/storage.rs @@ -127,6 +127,42 @@ impl Game<'_> { None => vec![own], } } + /// `Container.stillValidBlockEntity`: the open block container closes once + /// the player's eyes are out of reach of any of its blocks (a 4-block buffer). + pub(super) fn tick_container_reach(&mut self) { + // A server's menu ends when the server closes it. + if self.remote.is_some() { + return; + } + let Some(eye) = self.eye_position() else { + return; + }; + if self + .container + .is_none_or(|k| k.1 == super::minecarts::CART_KEY_Y) + { + return; + } + let range = self.reach() + 4.0; + let near = self.container_parts().iter().all(|((x, y, z), _)| { + let d = [*x, *y, *z] + .iter() + .zip(eye) + .map(|(&lo, e)| { + (f64::from(lo) - e) + .max(e - f64::from(lo) - 1.0) + .max(0.0) + .powi(2) + }) + .sum::(); + d < range * range + }); + if !near { + self.close_container(); + self.screen = super::Screen::Playing; + self.capture(true); + } + } /// The number of slots of the open container. pub(super) fn container_size(&self) -> usize { self.container_parts().iter().map(|p| p.1).sum() diff --git a/crates/horizon-client/src/lan/containers.rs b/crates/horizon-client/src/lan/containers.rs index d43cb5c27..d30d3a494 100644 --- a/crates/horizon-client/src/lan/containers.rs +++ b/crates/horizon-client/src/lan/containers.rs @@ -343,6 +343,12 @@ impl Hub { if !window_ok || self.guests[i].spectator() { return Vec::new(); } + // A menu that is no longer valid takes no clicks. + if let Some(m) = self.guests[i].menu.as_ref().filter(|_| in_menu) + && !m.slots.still_valid(world, &self.guests[i], m.pos) + { + return Vec::new(); + } let source = self.guests[i].menu.as_ref().map(|m| m.slots); let mut drag = if in_menu { self.guests[i].menu.as_mut().and_then(|m| m.drag.take()) diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index 440b58284..dc10096e3 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -404,6 +404,23 @@ impl Guest { pub fn eye_height(&self) -> f64 { if self.sneaking { 1.27 } else { 1.62 } } + /// `Player.isWithinBlockInteractionRange(pos, buffer)`: the eyes within the + /// block interaction range (4.5, creative 5) plus `buffer` of the block's box. + pub fn within_block_range(&self, pos: [i32; 3], buffer: f64) -> bool { + let eye = [ + self.position[0], + self.position[1] + self.eye_height(), + self.position[2], + ]; + let reach = if self.creative() { 5.0 } else { 4.5 } + buffer; + (0..3) + .map(|k| { + let lo = f64::from(pos[k]); + (lo - eye[k]).max(eye[k] - lo - 1.0).max(0.0).powi(2) + }) + .sum::() + < reach * reach + } /// The inventory menu's 46 slots (ServerPlayer.inventoryMenu): 0 crafting /// result, 1..=4 crafting grid, 5..=8 armor, 9..=35 main, 36..=44 hotbar, /// 45 off hand. diff --git a/crates/horizon-client/src/lan/menu.rs b/crates/horizon-client/src/lan/menu.rs index de9fb9608..b3f72da7e 100644 --- a/crates/horizon-client/src/lan/menu.rs +++ b/crates/horizon-client/src/lan/menu.rs @@ -64,6 +64,19 @@ impl Slots { .iter() .all(|k| world.block_entities.contains_key(k)) } + /// `Container.stillValid` for the guest: the block entities are there + /// and the guest is within reach of each (`stillValidBlockEntity`). The + /// ender chest's block is `at` (`PlayerEnderChestContainer.activeChest`). + pub fn still_valid(self, world: &World, guest: &Guest, at: Option<(i32, i32, i32)>) -> bool { + let blocks = match self { + Slots::Ender => at.into_iter().collect(), + _ => self.halves(), + }; + self.valid(world) + && blocks + .iter() + .all(|k| guest.within_block_range([k.0, k.1, k.2], 4.0)) + } /// Container slot `n`. pub fn get(self, world: &World, guest: &Guest, mut n: usize) -> Option { if self == Slots::Ender {