From 569e2c9243f4a301ea19a48cf40ea92bfd8677e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavier=20Mart=C3=ADnez=20Torres?= Date: Wed, 27 Nov 2024 12:44:26 -0600 Subject: [PATCH] player/player.go: Reset food exhaustion and tick when food loss is cancelled (#903) Co-authored-by: CJ <80650734+CJMustard1452@users.noreply.github.com> Co-authored-by: jack --- server/player/hunger.go | 10 ++++++++++ server/player/player.go | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/server/player/hunger.go b/server/player/hunger.go index 3d937810c..de17fecc2 100644 --- a/server/player/hunger.go +++ b/server/player/hunger.go @@ -67,6 +67,16 @@ func (m *hungerManager) Reset() { m.foodTick = 0 } +// ResetExhaustion resets the player's exhaustion level to 0. +// Prevents the player's food level from decreasing after preventing food loss. +func (m *hungerManager) resetExhaustion() { + m.mu.Lock() + defer m.mu.Unlock() + m.exhaustionLevel = 0 + m.saturationLevel = 0 + m.foodTick = 0 +} + // exhaust exhausts the player by the amount of points passed. If the total exhaustion level exceeds 4, a // saturation point, or food point, if saturation is 0, will be subtracted. func (m *hungerManager) exhaust(points float64) { diff --git a/server/player/player.go b/server/player/player.go index b65088581..ce4b52d72 100644 --- a/server/player/player.go +++ b/server/player/player.go @@ -762,6 +762,12 @@ func (p *Player) Exhaust(points float64) { ctx := event.C(p) if p.Handler().HandleFoodLoss(ctx, before, &after); ctx.Cancelled() { + // Reset the exhaustion level if the event was cancelled. + // Because if we cancel this on some moments, and after a time we don't cancel it, + // the first food level going to decrease a bit faster than expected. + // An example is if we cancelled that on a world, and we change of world, our food decrease very fast on the next tick + p.hunger.resetExhaustion() + return } p.hunger.SetFood(after)