Botcraft 1.21.4
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#if PROTOCOL_VERSION < 755 /* < 1.17 */
18 enum class TransactionState
19 {
20 Waiting,
21 Accepted,
22 Refused
23 };
24#endif
25
27 {
28 std::shared_ptr<ProtocolCraft::ServerboundContainerClickPacket> msg;
29#if PROTOCOL_VERSION < 755 /* < 1.17 */
30 std::map<short, ProtocolCraft::Slot> changed_slots;
31 ProtocolCraft::Slot carried_item;
32#endif
33 };
34
35 class Window;
36
38 {
39 public:
41
42 std::shared_ptr<Window> GetWindow(const short window_id) const;
43 short GetFirstOpenedWindowId() const;
44 std::shared_ptr<Window> GetPlayerInventory() const;
45 short GetIndexHotbarSelected() const;
49 void EraseInventory(const short window_id);
50#if PROTOCOL_VERSION < 755 /* < 1.17 */
51 TransactionState GetTransactionState(const short window_id, const int transaction_id) const;
52 void AddPendingTransaction(const InventoryTransaction& transaction);
53#endif
54 /// @brief "think" about the changes made by this transaction, filling in the necessary values in the msg
55 /// @param transaction The transaction to update with the modifications
56 /// @return An InventoryTransaction with various info, depending on the version
57 InventoryTransaction PrepareTransaction(const std::shared_ptr<ProtocolCraft::ServerboundContainerClickPacket>& transaction);
58
59 /// @brief Apply a given transaction to a container
60 /// @param transaction The transaction to apply
61 void ApplyTransaction(const InventoryTransaction& transaction);
62#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
63 std::vector<ProtocolCraft::MerchantOffer> GetAvailableMerchantOffers() const;
64 void IncrementMerchantOfferUse(const int index);
65#endif
66
67 private:
68 void SetHotbarSelected(const short index);
69 void SetCursor(const ProtocolCraft::Slot& c);
70
71 void AddInventory(const short window_id, const InventoryType window_type);
72 void SetSlot(const short window_id, const short index, const ProtocolCraft::Slot& slot);
73#if PROTOCOL_VERSION > 754 /* > 1.16.5 */
74 void SynchronizeContainerPlayerInventory(const short window_id);
75#endif
76#if PROTOCOL_VERSION > 755 /* > 1.17 */
77 void SetStateId(const short window_id, const int state_id);
78#endif
79
80 private:
83 virtual void Handle(ProtocolCraft::ClientboundOpenScreenPacket& msg) override;
84#if PROTOCOL_VERSION < 768 /* < 1.21.2 */
85 virtual void Handle(ProtocolCraft::ClientboundSetCarriedItemPacket& msg) override;
86#else
87 virtual void Handle(ProtocolCraft::ClientboundSetHeldSlotPacket& msg) override;
88#endif
89#if PROTOCOL_VERSION < 755 /* < 1.17 */
90 virtual void Handle(ProtocolCraft::ClientboundContainerAckPacket& msg) override;
91#endif
92#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
94#endif
96#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
97 virtual void Handle(ProtocolCraft::ClientboundSetCursorItemPacket& msg) override;
99#endif
100
101 void ApplyTransactionImpl(const InventoryTransaction& transaction);
102
103 private:
104 mutable std::shared_mutex inventory_manager_mutex;
105
106 std::map<short, std::shared_ptr<Window> > inventories;
109
110#if PROTOCOL_VERSION < 755 /* < 1.17 */
111 // Storing all the transactions that have neither been accepted
112 // nor refused by the server yet
113 std::map<short, std::map<short, InventoryTransaction > > pending_transactions;
114 // Storing the old transactions (accepted/refused) for all opened windows
115 std::map<short, std::map<short, TransactionState> > transaction_states;
116#endif
117#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
119 std::vector<ProtocolCraft::MerchantOffer> available_trades;
120#endif
121 };
122} // Botcraft
ProtocolCraft::Slot GetOffHand() const
void SetStateId(const short window_id, const int state_id)
void SetHotbarSelected(const short index)
void AddInventory(const short window_id, const InventoryType window_type)
virtual void Handle(ProtocolCraft::ClientboundContainerSetSlotPacket &msg) override
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
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 msg
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
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 IncrementMerchantOfferUse(const int index)
InventoryType
Definition Enums.hpp:217
std::shared_ptr< ProtocolCraft::ServerboundContainerClickPacket > msg