From 67633fc7891a3c6f2bcd5443499a11ec0ed36360 Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Sat, 17 Oct 2026 01:46:07 -0400 Subject: [PATCH] Reset player experience on respawn and award capped death XP --- crates/horizon-client/src/entities.rs | 45 +++++++++++++++++++ crates/horizon-client/src/game.rs | 11 ++--- crates/horizon-client/src/game/lan_actions.rs | 14 ++++++ crates/horizon-client/src/game/screens.rs | 4 ++ 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/crates/horizon-client/src/entities.rs b/crates/horizon-client/src/entities.rs index a1e18076e..5e6a3336a 100644 --- a/crates/horizon-client/src/entities.rs +++ b/crates/horizon-client/src/entities.rs @@ -301,6 +301,20 @@ pub struct Experience { pub total: i32, } impl Experience { + /// `Player.getBaseExperienceReward`: keep-inventory and spectators drop none. + pub fn death_reward(&self, keep: bool) -> u32 { + if keep { + 0 + } else { + self.level.wrapping_mul(7).clamp(0, 100) as u32 + } + } + /// `ServerPlayer.restoreFrom`: only keep-inventory and spectators retain XP. + pub fn respawn(&mut self, keep: bool) { + if !keep { + *self = Self::default(); + } + } pub fn needed(level: i32) -> i32 { if level >= 30 { 112 + (level - 30) * 9 @@ -540,6 +554,37 @@ impl ArmorStand { } } +#[cfg(test)] +mod experience_tests { + use super::*; + + #[test] + fn death_reward_uses_levels_and_caps_the_recoverable_points() { + for (level, reward) in [(0, 0), (1, 7), (14, 98), (15, 100), (30, 100)] { + let xp = Experience { + level, + progress: 0.75, + total: 10_000, + }; + assert_eq!(xp.death_reward(false), reward); + assert_eq!(xp.death_reward(true), 0); + } + } + + #[test] + fn respawn_clears_all_xp_unless_it_is_kept() { + let mut xp = Experience::default(); + xp.give(155); + let before = (xp.level, xp.progress, xp.total); + xp.respawn(true); + assert_eq!((xp.level, xp.progress, xp.total), before); + xp.respawn(false); + assert_eq!((xp.level, xp.progress, xp.total), (0, 0.0, 0)); + xp.give(7); + assert_eq!((xp.level, xp.progress, xp.total), (1, 0.0, 7)); + } +} + #[cfg(test)] mod fire_environment_tests { use super::*; diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 73fcbbf52..2680aa3c6 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3419,11 +3419,8 @@ impl Game<'_> { } if player.health.dead_or_dying() && self.screen != Screen::Dead { // Inventory.dropAll: every stack thrown in a random direction. - let at = [ - player.position.x, - player.position.y + 1.32, - player.position.z, - ]; + let feet = [player.position.x, player.position.y, player.position.z]; + let at = [feet[0], feet[1] + 1.32, feet[2]]; let killer = self.combat_killer_kind(); self.death_message(); // ServerPlayer.die: deaths, and the time-since counters restart. @@ -3439,6 +3436,10 @@ impl Game<'_> { } self.stats.reset("custom", "minecraft:time_since_death"); self.stats.reset("custom", "minecraft:time_since_rest"); + let reward = self + .experience + .death_reward(self.ui.rules.bool("keep_inventory") || self.spectator()); + self.spawn_orbs(feet, reward); if !self.creative && !self.ui.rules.bool("keep_inventory") { // Inventory.dropAll: slots 0..36, armor feet to head (36..39), off hand (40). let mut stacks: Vec = diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index ec4ac6cee..af7ff10f8 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -822,6 +822,13 @@ impl Game<'_> { g.saturation = 5.0; g.sent_health = None; g.motion = None; + let mut xp = Experience { + level: g.xp_level, + progress: g.xp_progress, + total: g.xp_total, + }; + xp.respawn(self.ui.rules.bool("keep_inventory") || g.game_mode == SPECTATOR); + (g.xp_level, g.xp_progress, g.xp_total) = (xp.level, xp.progress, xp.total); // A new body: nothing given to the old one carries over. if let Some(play) = host.play.get_mut(&conn) { play.potions = Default::default(); @@ -898,6 +905,12 @@ impl Game<'_> { g.dead = true; g.last_death = Some(g.position.map(|v| v.floor() as i32)); let (id, at) = (g.entity_id, g.position); + let reward = Experience { + level: g.xp_level, + progress: g.xp_progress, + total: g.xp_total, + } + .death_reward(self.ui.rules.bool("keep_inventory") || g.game_mode == SPECTATOR); let message = literal(&format!("{} died", g.name)); host.hub.send_to( conn, @@ -905,6 +918,7 @@ impl Game<'_> { ); // LivingEntity.die: the death animation, the dying player included. host.hub.entity_event(id, 3, at); + self.spawn_orbs(at, reward); } } } diff --git a/crates/horizon-client/src/game/screens.rs b/crates/horizon-client/src/game/screens.rs index 54012ca03..3c6f90099 100644 --- a/crates/horizon-client/src/game/screens.rs +++ b/crates/horizon-client/src/game/screens.rs @@ -260,6 +260,10 @@ impl Game<'_> { .into_iter() .find(|d| ui::hovered(self.cursor, d.0, d.1, 200.0, 20.0)) .map(|d| d.3); + if hit == Some(0) && self.remote.is_none() { + let keep = self.ui.rules.bool("keep_inventory") || self.spectator(); + self.experience.respawn(keep); + } match hit { Some(0) if self.remote.is_some() => { self.ui.death_text = None;