diff --git a/botcraft/include/botcraft/Game/Inventory/InventoryManager.hpp b/botcraft/include/botcraft/Game/Inventory/InventoryManager.hpp index d250ed24..5594872c 100644 --- a/botcraft/include/botcraft/Game/Inventory/InventoryManager.hpp +++ b/botcraft/include/botcraft/Game/Inventory/InventoryManager.hpp @@ -45,10 +45,14 @@ namespace Botcraft short GetFirstOpenedWindowId() const; std::shared_ptr GetPlayerInventory() const; short GetIndexHotbarSelected() const; + /// @brief Does not send a packet, just updates inventory state. + /// To send the packet, use ManagersClient::SetHotbarSelection(index). + void SetIndexHotbarSelected(const short index); ProtocolCraft::Slot GetHotbarSelected() const; ProtocolCraft::Slot GetOffHand() const; ProtocolCraft::Slot GetCursor() const; void EraseInventory(const short window_id); + #if PROTOCOL_VERSION < 755 /* < 1.17 */ TransactionState GetTransactionState(const short window_id, const int transaction_id) const; void AddPendingTransaction(const InventoryTransaction& transaction); @@ -67,7 +71,6 @@ namespace Botcraft #endif private: - void SetHotbarSelected(const short index); void SetCursor(const ProtocolCraft::Slot& c); void AddInventory(const short window_id, const InventoryType window_type); diff --git a/botcraft/include/botcraft/Game/ManagersClient.hpp b/botcraft/include/botcraft/Game/ManagersClient.hpp index b5931b02..055557d2 100644 --- a/botcraft/include/botcraft/Game/ManagersClient.hpp +++ b/botcraft/include/botcraft/Game/ManagersClient.hpp @@ -40,6 +40,8 @@ namespace Botcraft bool GetAutoRespawn() const; void SetAutoRespawn(const bool b); + void SetHotbarSelection(const short index); + // Set the right transaction id, add it to the inventory manager, // update the next transaction id and send it to the server // return the id of the transaction diff --git a/botcraft/src/Game/Inventory/InventoryManager.cpp b/botcraft/src/Game/Inventory/InventoryManager.cpp index 333548c0..fcd0e408 100644 --- a/botcraft/src/Game/Inventory/InventoryManager.cpp +++ b/botcraft/src/Game/Inventory/InventoryManager.cpp @@ -67,6 +67,12 @@ namespace Botcraft return index_hotbar_selected; } + void InventoryManager::SetIndexHotbarSelected(const short index) + { + std::scoped_lock lock(inventory_manager_mutex); + index_hotbar_selected = index; + } + short InventoryManager::GetFirstOpenedWindowId() const { std::shared_lock lock(inventory_manager_mutex); @@ -228,12 +234,6 @@ namespace Botcraft #endif } - void InventoryManager::SetHotbarSelected(const short index) - { - std::scoped_lock lock(inventory_manager_mutex); - index_hotbar_selected = index; - } - Slot InventoryManager::GetCursor() const { std::shared_lock lock(inventory_manager_mutex); @@ -585,7 +585,8 @@ namespace Botcraft void InventoryManager::Handle(ClientboundSetHeldSlotPacket& packet) #endif { - SetHotbarSelected(packet.GetSlot()); + std::scoped_lock lock(inventory_manager_mutex); + index_hotbar_selected = packet.GetSlot(); } #if PROTOCOL_VERSION < 755 /* < 1.17 */ @@ -646,8 +647,7 @@ namespace Botcraft #if PROTOCOL_VERSION > 767 /* > 1.21.1 */ void InventoryManager::Handle(ClientboundSetCursorItemPacket& packet) { - // Not sure about this one, I can't figure out when it's sent by the server - SetSlot(Window::PLAYER_INVENTORY_INDEX, Window::INVENTORY_HOTBAR_START + index_hotbar_selected, packet.GetContents()); + SetCursor(packet.GetContents()); } void InventoryManager::Handle(ProtocolCraft::ClientboundSetPlayerInventoryPacket& packet) diff --git a/botcraft/src/Game/ManagersClient.cpp b/botcraft/src/Game/ManagersClient.cpp index fae694d8..08ce69b9 100644 --- a/botcraft/src/Game/ManagersClient.cpp +++ b/botcraft/src/Game/ManagersClient.cpp @@ -97,6 +97,14 @@ namespace Botcraft return day_time; } + void ManagersClient::SetHotbarSelection(const short index) + { + inventory_manager->SetIndexHotbarSelected(index); + ServerboundSetCarriedItemPacket packet; + packet.SetSlot(index); + network_manager->Send(std::make_shared(packet)); + } + int ManagersClient::SendInventoryTransaction(const std::shared_ptr& transaction) { InventoryTransaction inventory_transaction = inventory_manager->PrepareTransaction(transaction);