Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/reset food saturation #903

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions server/player/hunger.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ func (m *hungerManager) Reset() {
m.mu.Unlock()
}

// ResetExhaustion resets the exhaustion level of the player to 0.
aabstractt marked this conversation as resolved.
Show resolved Hide resolved
// Prevents the player food level decrease after non cancelling the food loss
aabstractt marked this conversation as resolved.
Show resolved Hide resolved
func (m *hungerManager) resetExhaustion() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function cause race condition plz fix

aabstractt marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -818,6 +818,12 @@ func (p *Player) Exhaust(points float64) {

ctx := event.C()
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
Loading