From bbd3c19f4b96ebd6f2c0b43e66fa093f1ff4bb37 Mon Sep 17 00:00:00 2001 From: Jon Date: Sat, 21 Dec 2024 09:39:06 -0800 Subject: [PATCH] fix item charge and releasing issue --- server/item/crossbow.go | 8 +++++--- server/item/item.go | 2 +- server/player/player.go | 4 +++- server/session/player.go | 8 ++++++++ 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/server/item/crossbow.go b/server/item/crossbow.go index 001c56855..2cc08e690 100644 --- a/server/item/crossbow.go +++ b/server/item/crossbow.go @@ -14,9 +14,9 @@ type Crossbow struct { } // Charge starts the charging process and checks if the charge duration meets the required duration. -func (c Crossbow) Charge(releaser Releaser, tx *world.Tx, ctx *UseContext, duration time.Duration) { +func (c Crossbow) Charge(releaser Releaser, tx *world.Tx, ctx *UseContext, duration time.Duration) bool { if !c.Item.Empty() { - return + return false } creative := releaser.GameMode().CreativeInventory() @@ -45,7 +45,7 @@ func (c Crossbow) Charge(releaser Releaser, tx *world.Tx, ctx *UseContext, durat }) if !ok && !creative { - return + return false } if projectileItem.Empty() { @@ -60,7 +60,9 @@ func (c Crossbow) Charge(releaser Releaser, tx *world.Tx, ctx *UseContext, durat crossbow := newCrossbowWith(held, c) releaser.SetHeldItems(crossbow, left) + return true } + return false } // ReleaseCharge checks if the item is fully charged and, if so, releases it. diff --git a/server/item/item.go b/server/item/item.go index 17202dfea..55c53b14d 100644 --- a/server/item/item.go +++ b/server/item/item.go @@ -158,7 +158,7 @@ type Releasable interface { // Chargeable represents an item that can be charged. type Chargeable interface { // Charge is called when an item is being used. - Charge(releaser Releaser, tx *world.Tx, ctx *UseContext, duration time.Duration) + Charge(releaser Releaser, tx *world.Tx, ctx *UseContext, duration time.Duration) bool // ReleaseCharge is called when an item is being released. ReleaseCharge(releaser Releaser, tx *world.Tx, ctx *UseContext) bool } diff --git a/server/player/player.go b/server/player/player.go index 925573ad5..76c793360 100644 --- a/server/player/player.go +++ b/server/player/player.go @@ -1379,7 +1379,9 @@ func (p *Player) UseItem() { // Stop charging and determine if the item is ready. p.usingItem = false - chargeable.Charge(p, p.tx, p.useContext(), time.Since(p.usingSince)) + if chargeable.Charge(p, p.tx, p.useContext(), time.Since(p.usingSince)) { + p.session().SendCrossbowChargeComplete() + } p.updateState() } diff --git a/server/session/player.go b/server/session/player.go index 60f8fcaf3..91b3406d2 100644 --- a/server/session/player.go +++ b/server/session/player.go @@ -725,6 +725,14 @@ func (s *Session) SendExperience(level int, progress float64) { }) } +// SendCrossbowChargeComplete sends a packet to indicate that the crossbow charging process has been completed. +func (s *Session) SendCrossbowChargeComplete() { + s.writePacket(&packet.ActorEvent{ + EntityRuntimeID: selfEntityRuntimeID, + EventType: packet.ActorEventFinishedChargingItem, + }) +} + // stackFromItem converts an item.Stack to its network ItemStack representation. func stackFromItem(it item.Stack) protocol.ItemStack { if it.Empty() {