Botcraft 26.2
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 // In 1.21.5+, ServerboundContainerClickPacket doesn't have the components data
32 // anymore, just hashed version so we need to store them somewhere
33#if PROTOCOL_VERSION < 755 /* < 1.17 */ || PROTOCOL_VERSION > 769 /* > 1.21.4 */
34 std::map<short, ProtocolCraft::Slot> changed_slots;
36#endif
37 };
38
39 class Window;
40
42 {
43 public:
44 InventoryManager(const std::shared_ptr<NetworkManager>& network_manager);
45
46 void SetIndexHotbarSelected(const short index);
47 std::shared_ptr<Window> GetWindow(const short window_id) const;
48 short GetFirstOpenedWindowId() const;
49 std::shared_ptr<Window> GetPlayerInventory() const;
50 short GetIndexHotbarSelected() const;
54 void EraseInventory(const short window_id);
55
56#if PROTOCOL_VERSION < 755 /* < 1.17 */
57 TransactionState GetTransactionState(const short window_id, const int transaction_id) const;
58#endif
59
60 // Set the right transaction id, add it to the inventory manager,
61 // update the next transaction id and send it to the server
62 // return the id of the transaction
63 int SendInventoryTransaction(const std::shared_ptr<ProtocolCraft::ServerboundContainerClickPacket>& transaction);
64
65#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
66 std::vector<ProtocolCraft::MerchantOffer> GetAvailableMerchantOffers() const;
67 void IncrementMerchantOfferUse(const int index);
68#endif
69
70 private:
71 void SetIndexHotbarSelectedLocal(const short index);
72 void SetCursor(const ProtocolCraft::Slot& c);
73
74 void AddInventory(const short window_id, const InventoryType window_type);
75 void SetSlot(const short window_id, const short index, const ProtocolCraft::Slot& slot);
76#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
77 void SynchronizeContainerPlayerInventory(const short window_id);
78#endif
79#if PROTOCOL_VERSION > 755 /* > 1.17 */
80 void SetStateId(const short window_id, const int state_id);
81#endif
82
83#if PROTOCOL_VERSION < 755 /* < 1.17 */
84 void AddPendingTransaction(const InventoryTransaction& transaction);
85#endif
86 /// @brief "think" about the changes made by this transaction, filling in the necessary values in the packet
87 /// @param transaction The transaction to update with the modifications
88 /// @return An InventoryTransaction with various info, depending on the version
89 InventoryTransaction PrepareTransaction(const std::shared_ptr<ProtocolCraft::ServerboundContainerClickPacket>& transaction);
90
91 /// @brief Apply a given transaction to a container
92 /// @param transaction The transaction to apply
93 void ApplyTransaction(const InventoryTransaction& transaction);
94
95 private:
96 virtual void Handle(ProtocolCraft::ClientboundContainerSetSlotPacket& packet) override;
98 virtual void Handle(ProtocolCraft::ClientboundOpenScreenPacket& packet) override;
99#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
100 virtual void Handle(ProtocolCraft::ClientboundSetCarriedItemPacket& packet) override;
101#else
102 virtual void Handle(ProtocolCraft::ClientboundSetHeldSlotPacket& packet) override;
103#endif
104#if PROTOCOL_VERSION < 755 /* < 1.17 */
105 virtual void Handle(ProtocolCraft::ClientboundContainerAckPacket& packet) override;
106#endif
107#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
108 virtual void Handle(ProtocolCraft::ClientboundMerchantOffersPacket& packet) override;
109#endif
110 virtual void Handle(ProtocolCraft::ClientboundContainerClosePacket& packet) override;
111#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
112 virtual void Handle(ProtocolCraft::ClientboundSetCursorItemPacket& packet) override;
113 virtual void Handle(ProtocolCraft::ClientboundSetPlayerInventoryPacket& packet) override;
114#endif
115
116 void ApplyTransactionImpl(const InventoryTransaction& transaction);
117
118 private:
119 mutable std::shared_mutex inventory_manager_mutex;
120
121 std::map<short, std::shared_ptr<Window> > inventories;
124
125#if PROTOCOL_VERSION < 755 /* < 1.17 */
126 // Storing all the transactions that have neither been accepted
127 // nor refused by the server yet
128 std::map<short, std::map<short, InventoryTransaction > > pending_transactions;
129 // Storing the old transactions (accepted/refused) for all opened windows
130 std::map<short, std::map<short, TransactionState> > transaction_states;
131#endif
132#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
134 std::vector<ProtocolCraft::MerchantOffer> available_trades;
135#endif
136
137 std::shared_ptr<NetworkManager> network_manager;
138 };
139} // 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
InventoryTransaction PrepareTransaction(const std::shared_ptr< ProtocolCraft::ServerboundContainerClickPacket > &transaction)
"think" about the changes made by this transaction, filling in the necessary values in the packet
void ApplyTransaction(const InventoryTransaction &transaction)
Apply a given transaction to a container.
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
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:223
std::shared_ptr< ProtocolCraft::ServerboundContainerClickPacket > packet
std::map< short, ProtocolCraft::Slot > changed_slots