Files
bilicraft-bot/patches/Botcraft-inventory-sync.patch
2026-11-24 12:43:25 +08:00

73 lines
3.1 KiB
Diff

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<short>(36 + index);
+ if (index >= 9 && index < 36) return static_cast<short>(index);
+ if (index >= 36 && index < 40) return static_cast<short>(44 - index);
+ if (index == 40) return Window::INVENTORY_OFFHAND_INDEX;
+ return -1;
+ }
+ }
+
InventoryManager::InventoryManager(const std::shared_ptr<NetworkManager>& network_manager) : network_manager(network_manager)
{
std::scoped_lock<std::shared_mutex> 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<short>(packet.GetSlot()), packet.GetContents());
+ const short slot = PlayerInventoryToMenuSlot(packet.GetSlot());
+ if (slot >= 0)
+ {
+ SetSlot(Window::PLAYER_INVENTORY_INDEX, slot, packet.GetContents());
+ }
}
#endif