Skip to content

Commit

Permalink
fix item charge and releasing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
xNatsuri committed Dec 21, 2024
1 parent f801edd commit bbd3c19
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
8 changes: 5 additions & 3 deletions server/item/crossbow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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() {
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion server/item/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion server/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
8 changes: 8 additions & 0 deletions server/session/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit bbd3c19

Please sign in to comment.