diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 6baab9863..7a66e1e7b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2852,7 +2852,7 @@ impl Game<'_> { // Block.playerDestroy: the mined statistic, the exhaustion and the drops. self.stats.add("mined", &facts.name, 1); if let Some(p) = self.player.as_mut() { - p.health.food.exhaustion = (p.health.food.exhaustion + 0.005).min(40.0); + p.cause_food_exhaustion(0.005); } let drops = self.block_loot_at(pos, state, held.as_ref()); let silk = held @@ -6514,7 +6514,7 @@ impl Game<'_> { if let Some(p) = self.player.as_mut() && !self.creative { - p.health.food.exhaustion = (p.health.food.exhaustion + 0.1).min(40.0); + p.cause_food_exhaustion(0.1); } let player = self.player.as_ref(); let from = player.map(|p| [p.position.x, p.position.y, p.position.z]); diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 5c85098e1..b00c74b8b 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -1592,9 +1592,7 @@ impl Game<'_> { if w.player.ground.on_ground && !on_ground && moved[1] > 0.0 { let sprinting = w.player.sprinting; w.player - .health - .food - .add_exhaustion(if sprinting { 0.2 } else { 0.05 }); + .cause_food_exhaustion(if sprinting { 0.2 } else { 0.05 }); } let landed = (|| -> horizon_client::Result<()> { w.set_on_ground_with_movement(on_ground, false, movement)?; diff --git a/crates/horizon-client/src/game/lan_pvp.rs b/crates/horizon-client/src/game/lan_pvp.rs index dba2ed8e6..01537cee6 100644 --- a/crates/horizon-client/src/game/lan_pvp.rs +++ b/crates/horizon-client/src/game/lan_pvp.rs @@ -81,7 +81,7 @@ impl Game<'_> { if let Some(p) = self.player.as_mut() && !self.creative { - p.health.food.exhaustion = (p.health.food.exhaustion + 0.1).min(40.0); + p.cause_food_exhaustion(0.1); } self.swing_arm(); Some(Strike { diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index 51cb1fc0a..52ab5060a 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -280,7 +280,7 @@ impl Game<'_> { } } EffectAction::Exhaust(amount) => { - p.health.food.exhaustion = (p.health.food.exhaustion + amount).min(40.0); + p.cause_food_exhaustion(amount); } EffectAction::Eat(nutrition, saturation) => { let food = &mut p.health.food; diff --git a/crates/horizon-client/src/hunger.rs b/crates/horizon-client/src/hunger.rs index fa1b62a5e..2f97ea293 100644 --- a/crates/horizon-client/src/hunger.rs +++ b/crates/horizon-client/src/hunger.rs @@ -14,6 +14,14 @@ use horizon_world::{ }; impl PlayerMotion { + /// Player.causeFoodExhaustion: creative and spectator abilities suppress + /// exhaustion from movement, attacks and effects. + pub fn cause_food_exhaustion(&mut self, amount: f32) { + if !self.invulnerable && !self.spectator { + self.health.food.add_exhaustion(amount); + } + } + /// ServerPlayer.tickRegeneration, dispatched from Player.aiStep before /// inventory and living movement. The separately invoked food tick follows /// movement in ServerPlayer.doTick. @@ -51,6 +59,7 @@ impl PlayerMotion { let mut world = TickWorld { resources, rules, + invulnerable: self.invulnerable || self.spectator, attributes: &mut self.attributes, data: &mut h.data, ids: h.ids, @@ -67,6 +76,7 @@ impl PlayerMotion { struct TickWorld<'a> { resources: &'a HealthResources, rules: HealthRules, + invulnerable: bool, attributes: &'a mut Attributes, data: &'a mut SyncedFloats, ids: PlayerFloatIds, @@ -118,6 +128,11 @@ impl FoodWorld for TickWorld<'_> { .expect("validated health field")) } fn hurt_starvation(&mut self, _: (), food: &mut FoodData, amount: f32) -> Result<()> { + // FoodData still ticks after a mode switch with an empty food bar, but + // Player.hurtServer rejects starvation for invulnerable abilities. + if self.invulnerable { + return Ok(()); + } let id = self .resources .damage diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index 6fca66c1d..f8052a747 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -1269,9 +1269,7 @@ impl MotionWorld<'_, '_, '_> { amount: 1, })); self.player - .health - .food - .add_exhaustion(if self.player.sprinting { 0.2 } else { 0.05 }); + .cause_food_exhaustion(if self.player.sprinting { 0.2 } else { 0.05 }); Ok(()) } /// LivingEntity.travelFallFlying with updateFallFlyingMovement. @@ -1423,7 +1421,7 @@ impl MovementStatisticsWorld for MotionWorld<'_, '_, '_> { Ok(()) } fn exhaust(&mut self, amount: f32) -> Result<()> { - self.player.health.food.add_exhaustion(amount); + self.player.cause_food_exhaustion(amount); Ok(()) } } @@ -2227,6 +2225,73 @@ mod tests { assert!(ladder.last().unwrap().1.y > 0.0); } + #[test] + fn creative_and_spectator_preserve_hunger_when_switching_from_survival() { + let root = std::env::var_os("HORIZON_TEST_RESOURCES") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3") + }); + if !root.join("movement-properties.nbt").exists() { + eprintln!("skipped hunger regression: prepared resources unavailable"); + return; + } + let assets = Assets::load(&root).unwrap(); + let resources = MotionResources::load(&root, &assets).unwrap(); + // Both abilities are set by creative/spectator mode changes for the + // resident player and for LAN/dedicated guests. + for spectator in [false, true] { + for food in [0, 1, 20] { + let mut player = PlayerMotion::new(&resources, Vec3::ZERO).unwrap(); + player.health.food.food = food; + player.health.food.saturation = 0.0; + player.health.food.exhaustion = 0.0; + player.health.food.timer = 79; + player.invulnerable = true; + player.spectator = spectator; + for _ in 0..160 { + player.cause_food_exhaustion(0.2); + player + .food_tick(&resources.health, Default::default()) + .unwrap(); + } + assert_eq!(player.health.food.food, food); + assert_eq!(player.health.food.saturation, 0.0); + assert_eq!(player.health.food.exhaustion, 0.0); + assert_eq!(player.health.health(), 20.0); + assert!(!player.health.dead_or_dying()); + assert!(player.events.is_empty()); + + // Pending exhaustion from survival is still processed, but an + // empty food bar cannot hurt the now-invulnerable player. + player.health.food.exhaustion = 5.0; + player.health.food.timer = 79; + player + .food_tick(&resources.health, Default::default()) + .unwrap(); + assert_eq!(player.health.food.food, (food - 1).max(0)); + assert_eq!(player.health.food.exhaustion, 1.0); + assert_eq!(player.health.health(), 20.0); + assert!(player.events.is_empty()); + + // Returning to survival resumes exhaustion and starvation. + player.health.food.food = food; + player.health.food.exhaustion = 0.0; + player.invulnerable = false; + player.spectator = false; + player.cause_food_exhaustion(0.2); + assert_eq!(player.health.food.exhaustion, 0.2); + player.health.food.exhaustion = 5.0; + player.health.food.timer = 79; + player + .food_tick(&resources.health, Default::default()) + .unwrap(); + assert_eq!(player.health.food.food, (food - 1).max(0)); + assert_eq!(player.health.health(), if food <= 1 { 19.0 } else { 20.0 }); + } + } + } + #[test] fn player_fire_damage_queues_hurt_sound_only_for_fresh_hits() { let root = std::env::var_os("HORIZON_TEST_RESOURCES")