Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ namespace Botcraft
short GetFirstOpenedWindowId() const;
std::shared_ptr<Window> 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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions botcraft/include/botcraft/Game/ManagersClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions botcraft/src/Game/Inventory/InventoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ namespace Botcraft
return index_hotbar_selected;
}

void InventoryManager::SetIndexHotbarSelected(const short index)
{
std::scoped_lock<std::shared_mutex> lock(inventory_manager_mutex);
index_hotbar_selected = index;
}

short InventoryManager::GetFirstOpenedWindowId() const
{
std::shared_lock<std::shared_mutex> lock(inventory_manager_mutex);
Expand Down Expand Up @@ -228,12 +234,6 @@ namespace Botcraft
#endif
}

void InventoryManager::SetHotbarSelected(const short index)
{
std::scoped_lock<std::shared_mutex> lock(inventory_manager_mutex);
index_hotbar_selected = index;
}

Slot InventoryManager::GetCursor() const
{
std::shared_lock<std::shared_mutex> lock(inventory_manager_mutex);
Expand Down Expand Up @@ -585,7 +585,8 @@ namespace Botcraft
void InventoryManager::Handle(ClientboundSetHeldSlotPacket& packet)
#endif
{
SetHotbarSelected(packet.GetSlot());
std::scoped_lock<std::shared_mutex> lock(inventory_manager_mutex);
index_hotbar_selected = packet.GetSlot();
}

#if PROTOCOL_VERSION < 755 /* < 1.17 */
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions botcraft/src/Game/ManagersClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServerboundSetCarriedItemPacket>(packet));
}

int ManagersClient::SendInventoryTransaction(const std::shared_ptr<ServerboundContainerClickPacket>& transaction)
{
InventoryTransaction inventory_transaction = inventory_manager->PrepareTransaction(transaction);
Expand Down