Skip to content

Commit

Permalink
player/player.go: Take offhand into account on releasing items
Browse files Browse the repository at this point in the history
Close #965

Co-authored-by: Jon <[email protected]>
  • Loading branch information
DaPigGuy and xNatsuri committed Dec 29, 2024
1 parent e324031 commit 81748a9
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ func (p *Player) ReleaseItem() {

// canRelease returns whether the player can release the item currently held in the main hand.
func (p *Player) canRelease() bool {
held, _ := p.HeldItems()
held, left := p.HeldItems()
if _, consumable := held.Item().(item.Consumable); consumable {
return true
}
Expand All @@ -1463,10 +1463,18 @@ func (p *Player) canRelease() bool {
return true
}
for _, req := range releasable.Requirements() {
reqName, _ := req.Item().EncodeItem()

if !left.Empty() {
leftName, _ := left.Item().EncodeItem()
if leftName == reqName {
continue
}
}

_, found := p.Inventory().FirstFunc(func(stack item.Stack) bool {
name, _ := stack.Item().EncodeItem()
otherName, _ := req.Item().EncodeItem()
return name == otherName
return name == reqName
})
if !found {
return false
Expand All @@ -1482,7 +1490,12 @@ func (p *Player) handleUseContext(ctx *item.UseContext) {
p.SetHeldItems(p.subtractItem(p.damageItem(i, ctx.Damage), ctx.CountSub), left)
p.addNewItem(ctx)
for _, it := range ctx.ConsumedItems {
_ = p.Inventory().RemoveItem(it)
_ = p.offHand.RemoveItem(it)
it = it.Grow(-left.Count())

if !it.Empty() {
_ = p.Inventory().RemoveItem(it)
}
}
}

Expand Down Expand Up @@ -2944,6 +2957,10 @@ func (p *Player) useContext() *item.UseContext {
}
},
FirstFunc: func(comparable func(item.Stack) bool) (item.Stack, bool) {
_, left := p.HeldItems()
if !left.Empty() && comparable(left) {
return left, true
}
inv := p.Inventory()
s, ok := inv.FirstFunc(comparable)
if !ok {
Expand Down

0 comments on commit 81748a9

Please sign in to comment.