Botcraft 26.3
Loading...
Searching...
No Matches
Window.cpp
Go to the documentation of this file.
3
4using namespace ProtocolCraft;
5
6namespace Botcraft
7{
8 Window::Window(const InventoryType type_) : type(type_)
9 {
10 Init();
11 }
12
14 {
15 std::scoped_lock<std::shared_mutex> lock(window_mutex);
16 slots.clear();
17#if PROTOCOL_VERSION < 755 /* < 1.17 */
18 next_transaction_id = 1;
19#elif PROTOCOL_VERSION > 755 /* > 1.17 */
20 state_id = 1;
21#endif
22 }
23
24 Slot Window::GetSlot(const short index) const
25 {
26 std::shared_lock<std::shared_mutex> lock(window_mutex);
27 if (slots.find(index) == slots.end())
28 {
29 return Slot();
30 }
31
32 return slots.at(index);
33 }
34
35 std::map<short, Slot> Window::GetSlots() const
36 {
37 std::shared_lock<std::shared_mutex> lock(window_mutex);
38 return slots;
39 }
40
45
47 {
48 std::shared_lock<std::shared_mutex> lock(window_mutex);
49 return type;
50 }
51
52 void Window::SetSlot(const short index, const ProtocolCraft::Slot& slot)
53 {
54 std::scoped_lock<std::shared_mutex> lock(window_mutex);
55 slots[index] = slot;
56 }
57
58 void Window::SetContent(const std::vector<ProtocolCraft::Slot>& slots_)
59 {
60 std::scoped_lock<std::shared_mutex> lock(window_mutex);
61 for (size_t i = 0; i < slots_.size(); ++i)
62 {
63 slots[i] = slots_[i];
64 }
65 }
66
67#if PROTOCOL_VERSION < 755 /* < 1.17 */
68 int Window::GetNextTransactionId()
69 {
70 std::scoped_lock<std::shared_mutex> lock(window_mutex);
71 return next_transaction_id++;
72 }
73#elif PROTOCOL_VERSION > 755 /* > 1.17 */
75 {
76 std::shared_lock<std::shared_mutex> lock(window_mutex);
77 return state_id;
78 }
79
80 void Window::SetStateId(const int state_id_)
81 {
82 std::scoped_lock<std::shared_mutex> lock(window_mutex);
83 state_id = state_id_;
84 }
85#endif
86
88 {
89 switch (type)
90 {
93 return 1;
96 return 2;
104#if PROTOCOL_VERSION < 763 /* < 1.20 */
106#endif
107 return 3;
109#if PROTOCOL_VERSION > 762 /* > 1.19.4 */
111#endif
112 return 4;
115 return 5;
119#if PROTOCOL_VERSION > 764 /* > 1.20.2 */
121#endif
122 return 9;
124 return 10;
126 return 27;
133 return (static_cast<short>(type) + 1) * 9;
134 default:
135 LOG_WARNING("First slot asked for unknown InventoryType " << static_cast<int>(type));
136 return 9;
137 }
138 }
139
140 std::optional<short> Window::FindSlotMatching(const std::function<bool(const short, const ProtocolCraft::Slot&)>& predicate, const short first_index) const
141 {
142 std::shared_lock<std::shared_mutex> lock(window_mutex);
143 for (auto it = slots.lower_bound(first_index); it != slots.end(); ++it)
144 {
145 if (predicate(it->first, it->second))
146 {
147 return it->first;
148 }
149 }
150 return std::nullopt;
151 }
152
153#if PROTOCOL_VERSION < 347 /* < 1.13 */
154 int Window::CountItem(const std::pair<int, unsigned char>& item_id, const short first_index) const
155#else
156 int Window::CountItem(const int item_id, const short first_index) const
157#endif
158 {
159 std::shared_lock<std::shared_mutex> lock(window_mutex);
160 int output = 0;
161 for (auto it = slots.lower_bound(first_index); it != slots.end(); ++it)
162 {
163 if (!it->second.IsEmptySlot() && it->second.GetItemId() == item_id)
164 {
165 output += it->second.GetItemCount();
166 }
167 }
168 return output;
169 }
170} //Botcraft
#define LOG_WARNING(osstream)
Definition Logger.hpp:44
Mutex protected reference, will be locked until destroyed.
short GetFirstPlayerInventorySlot() const
Definition Window.cpp:87
void SetContent(const std::vector< ProtocolCraft::Slot > &slots_)
Definition Window.cpp:58
int CountItem(const int item_id, const short first_index=0) const
Total count of item_id in slots >= first_index.
Definition Window.cpp:156
std::map< short, ProtocolCraft::Slot > GetSlots() const
Definition Window.cpp:35
const InventoryType type
Definition Window.hpp:71
void SetSlot(const short index, const ProtocolCraft::Slot &slot)
Definition Window.cpp:52
int GetStateId() const
Definition Window.cpp:74
std::optional< short > FindSlotMatching(const std::function< bool(const short, const ProtocolCraft::Slot &)> &predicate, const short first_index=0) const
First slot with index >= first_index and for which predicate is true, thread-safe.
Definition Window.cpp:140
void SetStateId(const int state_id_)
Definition Window.cpp:80
ProtocolCraft::Slot GetSlot(const short index) const
Definition Window.cpp:24
std::shared_mutex window_mutex
Definition Window.hpp:68
std::map< short, ProtocolCraft::Slot > slots
Definition Window.hpp:70
Window(const InventoryType type_=InventoryType::Default)
Definition Window.cpp:8
InventoryType GetType() const
Definition Window.cpp:46
Utilities::ScopeLockedWrapper< const std::map< short, ProtocolCraft::Slot >, std::shared_mutex, std::shared_lock > GetLockedSlots() const
Get a read-only locked version of all the slots.
Definition Window.cpp:41
InventoryType
Definition Enums.hpp:226