Skip to content

Commit

Permalink
player/player.go: Fix eating in creative mode
Browse files Browse the repository at this point in the history
This is effectively a revert of 414ed1b
  • Loading branch information
DaPigGuy committed Dec 29, 2024
1 parent 9601e5c commit 53948e3
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -1404,9 +1404,23 @@ func (p *Player) UseItem() {
p.updateState()
return
}
// The player is currently using the item held. This is a signal the
// item was consumed, so we release it.
p.ReleaseItem()
// The player is currently using the item held. This is a signal the item was consumed, so we
// consume it and start using it again.
useCtx, dur := p.useContext(), p.useDuration()
if dur < usable.ConsumeDuration() {
// The required duration for consuming this item was not met, so we don't consume it.
return
}
ctx := event.C(p)
if p.Handler().HandleItemConsume(ctx, i); ctx.Cancelled() {
// Consuming was cancelled, but the client will continue consuming the next item.
p.usingSince = time.Now()
return
}
useCtx.CountSub, useCtx.NewItem = 1, usable.Consume(p.tx, p)
p.handleUseContext(useCtx)
p.usingSince = time.Now()
p.tx.PlaySound(p.Position().Add(mgl64.Vec3{0, 1.5}), sound.Burp{})
}
}

Expand All @@ -1424,37 +1438,18 @@ func (p *Player) ReleaseItem() {

useCtx, dur := p.useContext(), p.useDuration()
i, _ := p.HeldItems()
switch it := i.Item().(type) {
case item.Releasable:
ctx := event.C(p)
if p.Handler().HandleItemRelease(ctx, i, dur); ctx.Cancelled() {
return
}
it.Release(p, p.tx, useCtx, dur)
case item.Consumable:
if dur < it.ConsumeDuration() {
// The required duration for consuming this item was not met, so we don't consume it.
return
}
ctx := event.C(p)
if p.Handler().HandleItemConsume(ctx, i); ctx.Cancelled() {
// Consuming was cancelled, but the client will continue consuming the next item.
p.usingSince = time.Now()
return
}
useCtx.CountSub, useCtx.NewItem = 1, it.Consume(p.tx, p)
p.tx.PlaySound(p.Position().Add(mgl64.Vec3{0, 1.5}), sound.Burp{})
ctx := event.C(p)
if p.Handler().HandleItemRelease(ctx, i, dur); ctx.Cancelled() {
return
}
i.Item().(item.Releasable).Release(p, p.tx, useCtx, dur)
p.handleUseContext(useCtx)
p.updateState()
}

// canRelease returns whether the player can release the item currently held in the main hand.
func (p *Player) canRelease() bool {
held, left := p.HeldItems()
if _, consumable := held.Item().(item.Consumable); consumable {
return true
}
releasable, ok := held.Item().(item.Releasable)
if !ok {
return false
Expand Down

0 comments on commit 53948e3

Please sign in to comment.