Botcraft 1.21.4
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} //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
std::map< short, ProtocolCraft::Slot > GetSlots() const
Definition Window.cpp:35
const InventoryType type
Definition Window.hpp:57
void SetSlot(const short index, const ProtocolCraft::Slot &slot)
Definition Window.cpp:52
int GetStateId() const
Definition Window.cpp:74
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:54
std::map< short, ProtocolCraft::Slot > slots
Definition Window.hpp:56
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:217