diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 5adb24550..9bfa8d89a 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1635,6 +1635,8 @@ struct Game<'a> { /// The head of the bed while sleeping; the visual timer also survives waking. sleeping: Option<[i32; 3]>, sleep_timer: u32, + /// `SleepStatus`'s active and sleeping players at the last announcement. + sleep_seen: (i64, i64), dim: Dim, world_root: PathBuf, /// The world is superflat (`ServerLevel.isFlat`, for every dimension). @@ -5376,6 +5378,7 @@ impl Game<'_> { self.tick_friends(); horizon_client::spans::time("tick_hand", || self.tick_hand()); horizon_client::spans::time("tick_sleep", || self.tick_sleep()); + self.announce_sleep_status(); horizon_client::spans::time("tick_portal", || self.tick_nether_portal())?; if running { horizon_client::spans::time("tick_end", || self.tick_end())?; @@ -10892,6 +10895,8 @@ impl Game<'_> { self.wake_up(); } self.wake_guests(); + // SleepStatus.removeAllSleepers: waking everyone is not announced. + self.sleep_seen.1 = 0; } } fn creative_origin(batch: &HudBatch) -> (f32, f32) { @@ -13639,6 +13644,7 @@ impl<'a> Game<'a> { lightning_seen: [0; 2], sleeping: None, sleep_timer: 0, + sleep_seen: (0, 0), dim: Dim::Overworld, world_root: PathBuf::new(), world_flat: false, diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 8c1ce2d30..7f0a06a03 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -2278,11 +2278,10 @@ impl Game<'_> { } } - /// `SleepStatus.areEnoughSleeping` and `areEnoughDeepSleeping` over the - /// host's player and the guests: of the players not in spectator mode, - /// players_sleeping_percentage of them (at least one) are in bed, and as - /// many have slept 100 ticks. - pub(super) fn enough_slept(&self) -> bool { + /// `SleepStatus.update`'s counts over the host's player and the guests: + /// the players not in spectator mode, how many are in bed, and how many + /// of those have slept 100 ticks. + fn sleep_counts(&self) -> (i64, i64, i64) { let (mut active, mut sleeping, mut deep) = (0i64, 0i64, 0i64); let mut count = |in_bed: bool, timer: u32| { active += 1; @@ -2306,11 +2305,64 @@ impl Game<'_> { count(g.bed.is_some(), g.sleep_timer); } } + (active, sleeping, deep) + } + + /// `SleepStatus.sleepersNeeded`. + fn sleepers_needed(&self, active: i64) -> i64 { let percent = i64::from(self.ui.rules.int("players_sleeping_percentage").max(0)); - let needed = ((active * percent + 99) / 100).max(1); + ((active * percent + 99) / 100).max(1) + } + + /// `SleepStatus.areEnoughSleeping` and `areEnoughDeepSleeping` over the + /// host's player and the guests: of the players not in spectator mode, + /// players_sleeping_percentage of them (at least one) are in bed, and as + /// many have slept 100 ticks. + pub(super) fn enough_slept(&self) -> bool { + let (active, sleeping, deep) = self.sleep_counts(); + let needed = self.sleepers_needed(active); sleeping >= needed && deep >= needed } + /// `ServerLevel.updateSleepingPlayerList` and `announceSleepStatus`: when + /// the players or the sleepers change, a published or dedicated world + /// shows everyone "n/m players sleeping", or "Sleeping through this + /// night" once enough are in bed, on the action bar. + pub(super) fn announce_sleep_status(&mut self) { + let (active, sleeping, _) = self.sleep_counts(); + let (old_active, old_sleeping) = self.sleep_seen; + self.sleep_seen = (active, sleeping); + let changed = (old_sleeping > 0 || sleeping > 0) + && (old_active != active || old_sleeping != sleeping); + let published = self.lan.is_some() || self.options.dedicated.is_some(); + if !changed || !published { + return; + } + // ServerLevel.canSleepThroughNights. + if self.ui.rules.int("players_sleeping_percentage") > 100 { + return; + } + let needed = self.sleepers_needed(active); + let text = if sleeping >= needed { + horizon_client::text::Text::translate("sleep.skipping_night", &[]) + } else { + horizon_client::text::Text::translate( + "sleep.players_sleeping", + &[ + horizon_client::text::Text::literal(sleeping.to_string()), + horizon_client::text::Text::literal(needed.to_string()), + ], + ) + }; + if let Some(host) = self.lan.as_ref() { + host.hub + .overlay_message(horizon_client::lan::data::component_nbt(&text.component())); + } + if self.options.dedicated.is_none() { + self.titles.set_overlay(text, false); + } + } + /// `ServerLevel.wakeUpAllPlayers` for the guests. pub(super) fn wake_guests(&mut self) { if let Some(host) = self.lan.as_mut() { diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index 87a2ce4b0..aac80f8fb 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -493,6 +493,13 @@ impl Hub { overlay: false, })); } + /// `ServerPlayer.sendOverlayMessage` to every guest (the action bar). + pub fn overlay_message(&self, content: Tag) { + self.broadcast(&C::SystemChat(play::SystemChat { + content, + overlay: true, + })); + } /// A chat message from the host (a disguised chat: the host has no /// connection to sign with). pub fn chat(&self, sender: &str, text: &str) {