Botcraft 26.3
Loading...
Searching...
No Matches
InventoryManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include <memory>
5#include <shared_mutex>
6
8#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
10#endif
12
14
15namespace Botcraft
16{
17 class NetworkManager;
18
19#if PROTOCOL_VERSION < 755 /* < 1.17 */
20 enum class TransactionState
21 {
22 Waiting,
23 Accepted,
24 Refused
25 };
26#endif
27
29 {
30 std::shared_ptr<ProtocolCraft::ServerboundContainerClickPacket> packet;
31 std::map<short, ProtocolCraft::Slot> changed_slots;
33 };
34
35 class Window;
36
38 {
39 public:
40 InventoryManager(const std::shared_ptr<NetworkManager>& network_manager);
41
42 void SetIndexHotbarSelected(const short index);
43 std::shared_ptr<Window> GetWindow(const short window_id) const;
44 short GetFirstOpenedWindowId() const;
45 std::shared_ptr<Window> GetPlayerInventory() const;
46 short GetIndexHotbarSelected() const;
50 void EraseInventory(const short window_id);
51
52#if PROTOCOL_VERSION < 755 /* < 1.17 */
53 TransactionState GetTransactionState(const short window_id, const int transaction_id) const;
54#endif
55
56 // Set the right transaction id, add it to the inventory manager,
57 // update the next transaction id and send it to the server
58 // return the id of the transaction
59 int SendInventoryTransaction(const std::shared_ptr<ProtocolCraft::ServerboundContainerClickPacket>& transaction);
60
61#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
62 std::vector<ProtocolCraft::MerchantOffer> GetAvailableMerchantOffers() const;
63 void IncrementMerchantOfferUse(const int index);
64#endif
65
66 private:
67 void SetIndexHotbarSelectedLocal(const short index);
68 void SetCursor(const ProtocolCraft::Slot& c);
69
70 void AddInventory(const short window_id, const InventoryType window_type);
71 void SetSlot(const short window_id, const short index, const ProtocolCraft::Slot& slot);
72#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
73 void SynchronizeContainerPlayerInventory(const short window_id);
74#endif
75#if PROTOCOL_VERSION > 755 /* > 1.17 */
76 void SetStateId(const short window_id, const int state_id);
77#endif
78
79#if PROTOCOL_VERSION < 755 /* < 1.17 */
80 /// @brief Register a pending transaction, must be called with inventory_manager_mutex locked
81 /// @param transaction The transaction to register
82 void AddPendingTransactionImpl(const InventoryTransaction& transaction);
83#endif
84 /// @brief "think" about the changes made by this transaction, filling in the necessary values in the packet, must be called with inventory_manager_mutex locked
85 /// @param window The container the transaction takes place in
86 /// @param transaction The transaction to update with the modifications
87 /// @return An InventoryTransaction with the modified slots and carried item
88 InventoryTransaction PrepareTransactionImpl(const std::shared_ptr<Window>& window, const std::shared_ptr<ProtocolCraft::ServerboundContainerClickPacket>& transaction);
89
90
91 private:
92 virtual void Handle(ProtocolCraft::ClientboundContainerSetSlotPacket& packet) override;
94 virtual void Handle(ProtocolCraft::ClientboundOpenScreenPacket& packet) override;
95#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
96 virtual void Handle(ProtocolCraft::ClientboundSetCarriedItemPacket& packet) override;
97#else
98 virtual void Handle(ProtocolCraft::ClientboundSetHeldSlotPacket& packet) override;
99#endif
100#if PROTOCOL_VERSION < 755 /* < 1.17 */
101 virtual void Handle(ProtocolCraft::ClientboundContainerAckPacket& packet) override;
102#endif
103#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
104 virtual void Handle(ProtocolCraft::ClientboundMerchantOffersPacket& packet) override;
105#endif
106 virtual void Handle(ProtocolCraft::ClientboundContainerClosePacket& packet) override;
107#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
108 virtual void Handle(ProtocolCraft::ClientboundSetCursorItemPacket& packet) override;
109 virtual void Handle(ProtocolCraft::ClientboundSetPlayerInventoryPacket& packet) override;
110#endif
111
112 void ApplyTransactionImpl(const InventoryTransaction& transaction);
113
114 private:
115 mutable std::shared_mutex inventory_manager_mutex;
116
117 std::map<short, std::shared_ptr<Window> > inventories;
120
121#if PROTOCOL_VERSION < 755 /* < 1.17 */
122 // Storing all the transactions that have neither been accepted
123 // nor refused by the server yet
124 std::map<short, std::map<short, InventoryTransaction > > pending_transactions;
125 // Storing the old transactions (accepted/refused) for all opened windows
126 std::map<short, std::map<short, TransactionState> > transaction_states;
127#endif
128#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
130 std::vector<ProtocolCraft::MerchantOffer> available_trades;
131#endif
132
133 std::shared_ptr<NetworkManager> network_manager;
134 };
135} // Botcraft
ProtocolCraft::Slot GetOffHand() const
void SetStateId(const short window_id, const int state_id)
void AddInventory(const short window_id, const InventoryType window_type)
std::map< short, std::shared_ptr< Window > > inventories
void SynchronizeContainerPlayerInventory(const short window_id)
void SetSlot(const short window_id, const short index, const ProtocolCraft::Slot &slot)
std::vector< ProtocolCraft::MerchantOffer > available_trades
int SendInventoryTransaction(const std::shared_ptr< ProtocolCraft::ServerboundContainerClickPacket > &transaction)
virtual void Handle(ProtocolCraft::ClientboundContainerSetSlotPacket &packet) override
std::vector< ProtocolCraft::MerchantOffer > GetAvailableMerchantOffers() const
std::shared_mutex inventory_manager_mutex
std::shared_ptr< Window > GetWindow(const short window_id) const
ProtocolCraft::Slot GetHotbarSelected() const
std::shared_ptr< NetworkManager > network_manager
void SetCursor(const ProtocolCraft::Slot &c)
void ApplyTransactionImpl(const InventoryTransaction &transaction)
void EraseInventory(const short window_id)
ProtocolCraft::Slot GetCursor() const
InventoryTransaction PrepareTransactionImpl(const std::shared_ptr< Window > &window, const std::shared_ptr< ProtocolCraft::ServerboundContainerClickPacket > &transaction)
"think" about the changes made by this transaction, filling in the necessary values in the packet,...
std::shared_ptr< Window > GetPlayerInventory() const
void SetIndexHotbarSelectedLocal(const short index)
void SetIndexHotbarSelected(const short index)
void IncrementMerchantOfferUse(const int index)
InventoryType
Definition Enums.hpp:226
std::shared_ptr< ProtocolCraft::ServerboundContainerClickPacket > packet
std::map< short, ProtocolCraft::Slot > changed_slots