Skip to content

Commit

Permalink
player/player.go: Reset food exhaustion and tick when food loss is ca…
Browse files Browse the repository at this point in the history
…ncelled (#903)

Co-authored-by: CJ <[email protected]>
Co-authored-by: jack <[email protected]>
  • Loading branch information
3 people authored Nov 27, 2024
1 parent 1aafb3a commit 569e2c9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions server/player/hunger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 569e2c9

Please sign in to comment.