Skip to content

Commit

Permalink
player/hunger.go: Make mutex locking more consistent (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
CJMustard1452 authored Aug 15, 2024
1 parent 92cf5d5 commit 530372e
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions server/player/hunger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 530372e

Please sign in to comment.