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