diff --git a/botcraft/src/Game/Inventory/InventoryManager.cpp b/botcraft/src/Game/Inventory/InventoryManager.cpp index b35f052b..fdc10155 100644 --- a/botcraft/src/Game/Inventory/InventoryManager.cpp +++ b/botcraft/src/Game/Inventory/InventoryManager.cpp @@ -8,6 +8,19 @@ using namespace ProtocolCraft; namespace Botcraft { + namespace + { + // Player Inventory indices differ from inventory-menu/window indices. + short PlayerInventoryToMenuSlot(const int index) + { + if (index >= 0 && index < 9) return static_cast(36 + index); + if (index >= 9 && index < 36) return static_cast(index); + if (index >= 36 && index < 40) return static_cast(44 - index); + if (index == 40) return Window::INVENTORY_OFFHAND_INDEX; + return -1; + } + } + InventoryManager::InventoryManager(const std::shared_ptr& network_manager) : network_manager(network_manager) { std::scoped_lock lock(inventory_manager_mutex); @@ -556,13 +569,10 @@ namespace Botcraft // Slot index is starting in the hotbar, THEN in the main storage in this case :) else if (packet.GetContainerId() == -2) { - if (packet.GetSlot() < Window::INVENTORY_OFFHAND_INDEX - Window::INVENTORY_HOTBAR_START) - { - SetSlot(Window::PLAYER_INVENTORY_INDEX, Window::INVENTORY_HOTBAR_START + packet.GetSlot(), packet.GetItemStack()); - } - else + const short slot = PlayerInventoryToMenuSlot(packet.GetSlot()); + if (slot >= 0) { - SetSlot(Window::PLAYER_INVENTORY_INDEX, packet.GetSlot() + Window::INVENTORY_OFFHAND_INDEX - Window::INVENTORY_HOTBAR_START, packet.GetItemStack()); + SetSlot(Window::PLAYER_INVENTORY_INDEX, slot, packet.GetItemStack()); } } else if (packet.GetContainerId() >= 0) @@ -586,6 +596,10 @@ namespace Botcraft window->SetContent(packet.GetItems()); } +#if PROTOCOL_VERSION > 755 + SetCursor(packet.GetCarriedItem()); +#endif + #if PROTOCOL_VERSION > 755 /* > 1.17 */ if (packet.GetContainerId() >= 0) { @@ -694,13 +708,16 @@ 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) { - SetSlot(Window::PLAYER_INVENTORY_INDEX, static_cast(packet.GetSlot()), packet.GetContents()); + const short slot = PlayerInventoryToMenuSlot(packet.GetSlot()); + if (slot >= 0) + { + SetSlot(Window::PLAYER_INVENTORY_INDEX, slot, packet.GetContents()); + } } #endif