Skip to content

Commit

Permalink
session/player.go: Ensure the player is not currently changing slots …
Browse files Browse the repository at this point in the history
…to prevent desync.
  • Loading branch information
JustTalDevelops committed Jul 20, 2022
1 parent ff01204 commit 1f7f770
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions server/session/handler_inventory_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,22 @@ func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error
h.resendInventories(s)
return nil
case *protocol.UseItemOnEntityTransactionData:
s.changingSlot.Store(true)
defer s.changingSlot.Store(false)
if err := s.c.SetHeldSlot(int(data.HotBarSlot)); err != nil {
return err
}
return h.handleUseItemOnEntityTransaction(data, s)
case *protocol.UseItemTransactionData:
s.changingSlot.Store(true)
defer s.changingSlot.Store(false)
if err := s.c.SetHeldSlot(int(data.HotBarSlot)); err != nil {
return err
}
return h.handleUseItemTransaction(data, s)
case *protocol.ReleaseItemTransactionData:
s.changingSlot.Store(true)
defer s.changingSlot.Store(false)
if err := s.c.SetHeldSlot(int(data.HotBarSlot)); err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions server/session/handler_mob_equipment.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func (*MobEquipmentHandler) Handle(p packet.Packet, s *Session) error {
// out of sync.
s.log.Debugf("failed processing packet from %v (%v): *packet.MobEquipment: client-side item must be identical to server-side item, but got differences: client: %v vs server: %v", s.conn.RemoteAddr(), s.c.Name(), clientSide, actual)
}
s.changingSlot.Store(true)
defer s.changingSlot.Store(false)
return s.c.SetHeldSlot(int(pk.InventorySlot))
default:
return fmt.Errorf("only main inventory should be involved in slot chnage, got window ID %v", pk.WindowID)
Expand Down
3 changes: 3 additions & 0 deletions server/session/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,9 @@ func (s *Session) SetHeldSlot(slot int) error {
if slot < 0 || slot > 8 {
return fmt.Errorf("slot exceeds hotbar range 0-8: slot is %v", slot)
}
if s.changingSlot.Load() {

This comment has been minimized.

Copy link
@T14Raptor

T14Raptor Jul 20, 2022

Member

Should do this after setting heldSlot

return nil
}

s.heldSlot.Store(uint32(slot))

Expand Down
1 change: 1 addition & 0 deletions server/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type Session struct {
openedWindow atomic.Value[*inventory.Inventory]
openedPos atomic.Value[cube.Pos]
swingingArm atomic.Bool
changingSlot atomic.Bool

recipes map[uint32]recipe.Recipe

Expand Down

0 comments on commit 1f7f770

Please sign in to comment.