diff --git a/crates/horizon-client/src/entities.rs b/crates/horizon-client/src/entities.rs index 44f07dc82..14cd946c2 100644 --- a/crates/horizon-client/src/entities.rs +++ b/crates/horizon-client/src/entities.rs @@ -582,17 +582,21 @@ impl Experience { 7 + level * 2 } } - /// Returns true when a level was gained. - pub fn give(&mut self, points: i32) -> bool { - let start = self.level; + /// Returns the first multiple of 5 the player reached, where each level + /// gained would play the level-up sound (`Player.giveExperienceLevels`). + pub fn give(&mut self, points: i32) -> Option { + let mut sound_level = None; self.progress += points as f32 / Self::needed(self.level) as f32; self.total = self.total.saturating_add(points).max(0); while self.progress >= 1.0 { self.progress = (self.progress - 1.0) * Self::needed(self.level) as f32; self.level += 1; + if self.level % 5 == 0 { + sound_level.get_or_insert(self.level); + } self.progress /= Self::needed(self.level) as f32; } - self.level > start + sound_level } } /// A painting (`Painting`, a `HangingEntity`): the block it hangs in, the diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 96bf98429..de820a84a 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -9757,16 +9757,13 @@ impl Game<'_> { /// sound every fifth level (at most every 100 ticks). pub(super) fn give_orb_points(&mut self, gained: u32) { let at = self.eye_position(); - if self.experience.give(gained as i32) - && self.experience.level % 5 == 0 + // Player.giveExperienceLevels runs per level, so the first multiple of 5 + // crossed plays the sound (and starts the cooldown for the rest). + if let Some(level) = self.experience.give(gained as i32) && self.ticks > self.last_level_up + 100 { self.last_level_up = self.ticks; - let volume = if self.experience.level > 30 { - 1.0 - } else { - self.experience.level as f32 / 30.0 - }; + let volume = if level > 30 { 1.0 } else { level as f32 / 30.0 }; self.audio .play("minecraft:entity.player.levelup", at, volume * 0.75, 1.0); }