Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session/handler_inventory_transaction.go: resend slots from pk.LegacySetItemSlots #932

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions server/session/handler_inventory_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,60 @@ import (
type InventoryTransactionHandler struct{}

// Handle ...
func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session, tx *world.Tx, c Controllable) error {
func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session, tx *world.Tx, c Controllable) (err error) {
pk := p.(*packet.InventoryTransaction)

if len(pk.LegacySetItemSlots) > 2 {
return fmt.Errorf("too many slot sync requests in inventory transaction")
}

defer func() {
// The client has requested the server to resend the specified slots even if they haven't changed server-side.
// Handling these requests is necessary to ensure the client's inventory remains in sync with the server.
for _, slot := range pk.LegacySetItemSlots {
if len(slot.Slots) > 2 {
err = fmt.Errorf("too many slots in slot sync request")
return
}
switch slot.ContainerID {
case protocol.ContainerOffhand:
s.sendInv(s.offHand, protocol.WindowIDOffHand)
case protocol.ContainerInventory:
for _, slot := range slot.Slots {
if i, err := s.inv.Item(int(slot)); err == nil {
s.sendItem(i, int(slot), protocol.WindowIDInventory)
}
}
}
}
}()

switch data := pk.TransactionData.(type) {
case *protocol.NormalTransactionData:
h.resendInventories(s)
// Always resend inventories with normal transactions. Most of the time we do not use these
// transactions, so we're best off making sure the client and server stay in sync.
if err := h.handleNormalTransaction(pk, s, c); err != nil {
s.conf.Log.Debug("process packet: InventoryTransaction: verify Normal transaction actions: " + err.Error())
return nil
}
return nil
return
case *protocol.MismatchTransactionData:
// Just resend the inventory and don't do anything.
h.resendInventories(s)
return nil
return
case *protocol.UseItemOnEntityTransactionData:
if err := s.VerifyAndSetHeldSlot(int(data.HotBarSlot), stackToItem(data.HeldItem.Stack), c); err != nil {
return err
if err = s.VerifyAndSetHeldSlot(int(data.HotBarSlot), stackToItem(data.HeldItem.Stack), c); err != nil {
return
}
return h.handleUseItemOnEntityTransaction(data, s, tx, c)
case *protocol.UseItemTransactionData:
if err := s.VerifyAndSetHeldSlot(int(data.HotBarSlot), stackToItem(data.HeldItem.Stack), c); err != nil {
return err
if err = s.VerifyAndSetHeldSlot(int(data.HotBarSlot), stackToItem(data.HeldItem.Stack), c); err != nil {
return
}
return h.handleUseItemTransaction(data, s, c)
case *protocol.ReleaseItemTransactionData:
if err := s.VerifyAndSetHeldSlot(int(data.HotBarSlot), stackToItem(data.HeldItem.Stack), c); err != nil {
return err
if err = s.VerifyAndSetHeldSlot(int(data.HotBarSlot), stackToItem(data.HeldItem.Stack), c); err != nil {
return
}
return h.handleReleaseItemTransaction(c)
}
Expand Down
Loading