From 530372eaa3662a2af189701a8b85bd2b50a3cd21 Mon Sep 17 00:00:00 2001 From: CJ <80650734+CJMustard1452@users.noreply.github.com> Date: Thu, 15 Aug 2024 04:45:13 -0500 Subject: [PATCH] player/hunger.go: Make mutex locking more consistent (#904) --- server/player/hunger.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/server/player/hunger.go b/server/player/hunger.go index da94a8c47..c6471b475 100644 --- a/server/player/hunger.go +++ b/server/player/hunger.go @@ -30,13 +30,14 @@ func (m *hungerManager) Food() int { // SetFood sets the food level of a player. The level passed must be in a range of 0-20. If the level passed // is negative, the food level will be set to 0. If the level exceeds 20, the food level will be set to 20. func (m *hungerManager) SetFood(level int) { + m.mu.Lock() + defer m.mu.Unlock() + if level < 0 { level = 0 } else if level > 20 { level = 20 } - m.mu.Lock() - defer m.mu.Unlock() m.foodLevel = level } @@ -58,11 +59,12 @@ func (m *hungerManager) AddFood(points int) { // using newHungerManager. func (m *hungerManager) Reset() { m.mu.Lock() + defer m.mu.Unlock() + m.foodLevel = 20 m.saturationLevel = 5 m.exhaustionLevel = 0 m.foodTick = 0 - m.mu.Unlock() } // exhaust exhausts the player by the amount of points passed. If the total exhaustion level exceeds 4, a @@ -87,6 +89,7 @@ func (m *hungerManager) exhaust(points float64) { // saturation will never exceed the total food value. func (m *hungerManager) saturate(food int, saturation float64) { m.mu.Lock() + defer m.mu.Unlock() level := m.foodLevel + food if level < 0 { @@ -106,7 +109,6 @@ func (m *hungerManager) saturate(food int, saturation float64) { sat = float64(m.foodLevel) } m.saturationLevel = sat - m.mu.Unlock() } // desaturate removes one saturation point from the player. If the saturation level of the player is already