From 148e2d7cb3a044702f3407542c27b71c9d38f7a1 Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Fri, 15 Nov 2024 00:32:26 +0500 Subject: [PATCH 01/13] session/handler_inventory_transaction.go: resend inventories if there are LegacySetItemSlots --- server/session/handler_inventory_transaction.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index 715b68e73..6a090dd39 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -16,6 +16,10 @@ type InventoryTransactionHandler struct{} func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error { pk := p.(*packet.InventoryTransaction) + if len(pk.LegacySetItemSlots) > 0 { + h.resendInventories(s) + } + switch data := pk.TransactionData.(type) { case *protocol.NormalTransactionData: h.resendInventories(s) From f28354cbdca85b49e7713ba61e558398faf5bfda Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Tue, 19 Nov 2024 04:40:55 +0500 Subject: [PATCH 02/13] session/handler_inventory_transaction.go: resend only slots requested by the client instead of all inventories. --- server/session/handler_inventory_transaction.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index 6a090dd39..d28d61d15 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -16,8 +16,18 @@ type InventoryTransactionHandler struct{} func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error { pk := p.(*packet.InventoryTransaction) - if len(pk.LegacySetItemSlots) > 0 { - h.resendInventories(s) + for _, slots := range pk.LegacySetItemSlots { + switch slots.ContainerID { + case protocol.ContainerOffhand: + s.sendInv(s.offHand, protocol.WindowIDOffHand) + case protocol.ContainerInventory: + for _, slot := range slots.Slots { + if i, err := s.inv.Item(int(slot)); err == nil { + s.sendItem(i, int(slot), protocol.WindowIDInventory) + } + } + default: + } } switch data := pk.TransactionData.(type) { From 3c0075e0336c4e4254c6f31e7582b4f6bad326b3 Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Sun, 15 Dec 2024 15:21:50 +0500 Subject: [PATCH 03/13] handler_inventory_transaction.go: remove default case --- server/session/handler_inventory_transaction.go | 1 - 1 file changed, 1 deletion(-) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index d28d61d15..ffe6255bc 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -26,7 +26,6 @@ func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error s.sendItem(i, int(slot), protocol.WindowIDInventory) } } - default: } } From c1a2567712dfab385cf814897ea67099c3daf470 Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Sun, 15 Dec 2024 15:28:07 +0500 Subject: [PATCH 04/13] handler_inventory_transaction.go: defer LegacySetSlots handling --- .../session/handler_inventory_transaction.go | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index ffe6255bc..ce5988865 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -16,18 +16,20 @@ type InventoryTransactionHandler struct{} func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error { pk := p.(*packet.InventoryTransaction) - for _, slots := range pk.LegacySetItemSlots { - switch slots.ContainerID { - case protocol.ContainerOffhand: - s.sendInv(s.offHand, protocol.WindowIDOffHand) - case protocol.ContainerInventory: - for _, slot := range slots.Slots { - if i, err := s.inv.Item(int(slot)); err == nil { - s.sendItem(i, int(slot), protocol.WindowIDInventory) + defer func() { + for _, slots := range pk.LegacySetItemSlots { + switch slots.ContainerID { + case protocol.ContainerOffhand: + s.sendInv(s.offHand, protocol.WindowIDOffHand) + case protocol.ContainerInventory: + for _, slot := range slots.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: From d30fb9711368d58e310ccf05554d44ad081d78de Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Sun, 15 Dec 2024 15:30:54 +0500 Subject: [PATCH 05/13] handler_inventory_transaction.go: rename slots to slot --- server/session/handler_inventory_transaction.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index ce5988865..9cb0dda22 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -17,12 +17,12 @@ func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error pk := p.(*packet.InventoryTransaction) defer func() { - for _, slots := range pk.LegacySetItemSlots { - switch slots.ContainerID { + for _, slot := range pk.LegacySetItemSlots { + switch slot.ContainerID { case protocol.ContainerOffhand: s.sendInv(s.offHand, protocol.WindowIDOffHand) case protocol.ContainerInventory: - for _, slot := range slots.Slots { + for _, slot := range slot.Slots { if i, err := s.inv.Item(int(slot)); err == nil { s.sendItem(i, int(slot), protocol.WindowIDInventory) } From abe3963a3cf9edcf1a72955c35ea00909f493829 Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Sun, 15 Dec 2024 15:39:11 +0500 Subject: [PATCH 06/13] handler_inventory_transaction.go: added a comment to better understand what's going on here --- server/session/handler_inventory_transaction.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index 9cb0dda22..97f69824a 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -17,6 +17,8 @@ func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error pk := p.(*packet.InventoryTransaction) defer func() { + // Client requested the slots to be resend even when they were not changed. Handling of this + // is needed so that the contents of the client's inventory are synchronised with the server's. for _, slot := range pk.LegacySetItemSlots { switch slot.ContainerID { case protocol.ContainerOffhand: From d704fbce051a9a795cb9a4f49f6a7f9a1fa535b4 Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Sun, 15 Dec 2024 17:22:05 +0500 Subject: [PATCH 07/13] handler_inventory_transaction.go: update comment --- server/session/handler_inventory_transaction.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index 97f69824a..c31311f22 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -17,8 +17,8 @@ func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error pk := p.(*packet.InventoryTransaction) defer func() { - // Client requested the slots to be resend even when they were not changed. Handling of this - // is needed so that the contents of the client's inventory are synchronised with the server's. + // 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 { switch slot.ContainerID { case protocol.ContainerOffhand: From b92f99064160c91b7566f3316037e8491ce8be45 Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Sun, 15 Dec 2024 17:31:05 +0500 Subject: [PATCH 08/13] handler_inventory_transaction.go: disconnect if LegacySetItemSlots' length bigger than 10 --- server/session/handler_inventory_transaction.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index c31311f22..a6178c803 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -16,6 +16,10 @@ type InventoryTransactionHandler struct{} func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error { pk := p.(*packet.InventoryTransaction) + if len(pk.LegacySetItemSlots) > 10 { + 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. From ca9d439295977fb5a1cf8aadf480d1372f279421 Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Sun, 15 Dec 2024 17:37:24 +0500 Subject: [PATCH 09/13] handler_inventory_transaction.go: limit LegacySetItemSlots' length 10 -> 2 --- server/session/handler_inventory_transaction.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index a6178c803..578b273f6 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -16,7 +16,7 @@ type InventoryTransactionHandler struct{} func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error { pk := p.(*packet.InventoryTransaction) - if len(pk.LegacySetItemSlots) > 10 { + if len(pk.LegacySetItemSlots) > 2 { return fmt.Errorf("too many slot sync requests in inventory transaction") } From 0534044cc46c3f0c42d2abde0cc3ec394349ac3c Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Sun, 15 Dec 2024 17:43:07 +0500 Subject: [PATCH 10/13] handler_inventory_transaction.go: check slots count in slot sync requests too --- .../session/handler_inventory_transaction.go | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index 578b273f6..00b3c5333 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -13,7 +13,7 @@ import ( type InventoryTransactionHandler struct{} // Handle ... -func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error { +func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) (err error) { pk := p.(*packet.InventoryTransaction) if len(pk.LegacySetItemSlots) > 2 { @@ -24,6 +24,10 @@ func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error // 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) @@ -46,24 +50,24 @@ func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) error s.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.UpdateHeldSlot(int(data.HotBarSlot), stackToItem(data.HeldItem.Stack)); err != nil { - return err + if err = s.UpdateHeldSlot(int(data.HotBarSlot), stackToItem(data.HeldItem.Stack)); err != nil { + return } return h.handleUseItemOnEntityTransaction(data, s) case *protocol.UseItemTransactionData: - if err := s.UpdateHeldSlot(int(data.HotBarSlot), stackToItem(data.HeldItem.Stack)); err != nil { - return err + if err = s.UpdateHeldSlot(int(data.HotBarSlot), stackToItem(data.HeldItem.Stack)); err != nil { + return } return h.handleUseItemTransaction(data, s) case *protocol.ReleaseItemTransactionData: - if err := s.UpdateHeldSlot(int(data.HotBarSlot), stackToItem(data.HeldItem.Stack)); err != nil { - return err + if err = s.UpdateHeldSlot(int(data.HotBarSlot), stackToItem(data.HeldItem.Stack)); err != nil { + return } return h.handleReleaseItemTransaction(s) } From 3d54f15012c030bb2efb7e016f7d51e6456b3a9b Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Sun, 15 Dec 2024 17:52:34 +0500 Subject: [PATCH 11/13] handler_inventory_transaction.go: remove redundant return --- server/session/handler_inventory_transaction.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index 00b3c5333..004832623 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -19,7 +19,7 @@ func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) (err e 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. @@ -48,7 +48,6 @@ func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session) (err e // transactions, so we're best off making sure the client and server stay in sync. if err := h.handleNormalTransaction(pk, s); err != nil { s.log.Debug("process packet: InventoryTransaction: verify Normal transaction actions: " + err.Error()) - return nil } return case *protocol.MismatchTransactionData: From 582115fd0e14c1792eeff81fff3b2929350e9666 Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Sun, 15 Dec 2024 18:06:07 +0500 Subject: [PATCH 12/13] handler_inventory_transaction.go: uhm... --- server/session/handler_inventory_transaction.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index a972a1fc9..40a9d0417 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -49,7 +49,7 @@ func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session, tx *wo // 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.log.Debug("process packet: InventoryTransaction: verify Normal transaction actions: " + err.Error()) + s.conf.log.Debug("process packet: InventoryTransaction: verify Normal transaction actions: " + err.Error()) } return case *protocol.MismatchTransactionData: From 3285d98dee439267c95db4bc297a1dab4f94bea3 Mon Sep 17 00:00:00 2001 From: Darya Markova <122279000+Dasciam@users.noreply.github.com> Date: Sun, 15 Dec 2024 18:07:00 +0500 Subject: [PATCH 13/13] handler_inventory_transaction.go: ... --- server/session/handler_inventory_transaction.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/session/handler_inventory_transaction.go b/server/session/handler_inventory_transaction.go index 40a9d0417..965c3ae9e 100644 --- a/server/session/handler_inventory_transaction.go +++ b/server/session/handler_inventory_transaction.go @@ -49,7 +49,7 @@ func (h *InventoryTransactionHandler) Handle(p packet.Packet, s *Session, tx *wo // 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()) + s.conf.Log.Debug("process packet: InventoryTransaction: verify Normal transaction actions: " + err.Error()) } return case *protocol.MismatchTransactionData: