Botcraft 1.21.4
Loading...
Searching...
No Matches
ClientboundPlayerInfoUpdatePacket.hpp
Go to the documentation of this file.
1#if PROTOCOL_VERSION > 760 /* > 1.19.2 */
2#pragma once
3
4#include <string>
5#include <map>
6
13
14namespace ProtocolCraft
15{
17 {
18 AddPlayer = 0,
24#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
26#endif
27#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
29#endif
31 };
32
34 {
36
37 std::optional<RemoteChatSessionData> chat_session;
38
39 int game_mode = 0;
40
41 bool listed = false;
42
43 int latency = 0;
44
45#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
46 bool show_hat = false;
47#endif
48
49#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
50 int list_order = 0;
51#endif
52
53 std::optional<Chat> display_name;
54 };
55
56 class ClientboundPlayerInfoUpdatePacket : public BaseMessage<ClientboundPlayerInfoUpdatePacket>
57 {
58 public:
59 static constexpr std::string_view packet_name = "Player Info Update";
60
61 private:
62 std::vector<PlayerInfoUpdateAction> ReadActions(ReadIterator& iter, size_t& length) const
63 {
64 constexpr size_t bitset_size = static_cast<size_t>(PlayerInfoUpdateAction::NUM_PLAYERINFOUPDATEACTION);
65 std::bitset<bitset_size> bitset = ReadData<std::bitset<bitset_size>>(iter, length);
66 std::vector<PlayerInfoUpdateAction> actions;
67 for (size_t i = 0; i < bitset_size; ++i)
68 {
69 if (bitset[i])
70 {
71 actions.push_back(static_cast<PlayerInfoUpdateAction>(i));
72 }
73 }
74 return actions;
75 }
76
77 void WriteActions(const std::vector<PlayerInfoUpdateAction>& actions, WriteContainer& container) const
78 {
79 constexpr size_t bitset_size = static_cast<size_t>(PlayerInfoUpdateAction::NUM_PLAYERINFOUPDATEACTION);
80 std::bitset<bitset_size> bitset;
81 for (const auto a : actions)
82 {
83 bitset.set(static_cast<size_t>(a), true);
84 }
85 WriteData<std::bitset<bitset_size>>(bitset, container);
86 }
87
88 std::map<UUID, PlayerInfoUpdateEntry> ReadEntries(ReadIterator& iter, size_t& length) const
89 {
90 std::map<UUID, PlayerInfoUpdateEntry> entries;
91 const int entries_length = ReadData<VarInt>(iter, length);
92 for (int i = 0; i < entries_length; ++i)
93 {
94 const UUID uuid = ReadData<UUID>(iter, length);
95 PlayerInfoUpdateEntry& entry = entries[uuid];
96 for (const auto a : GetActions())
97 {
98 switch (a)
99 {
101 entry.game_profile.SetUuid(uuid);
102 entry.game_profile.SetName(ReadData<std::string>(iter, length));
103 entry.game_profile.SetProperties(ReadData<std::vector<GameProfileProperty>>(iter, length));
104 break;
106 entry.chat_session = ReadData<std::optional<RemoteChatSessionData>>(iter, length);
107 break;
109 entry.game_mode = ReadData<VarInt>(iter, length);
110 break;
112 entry.listed = ReadData<bool>(iter, length);
113 break;
115 entry.latency = ReadData<VarInt>(iter, length);
116 break;
118 entry.display_name = ReadData<std::optional<Chat>>(iter, length);
119 break;
120#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
122 entry.show_hat = ReadData<bool>(iter, length);
123 break;
124#endif
125#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
127 entry.list_order = ReadData<VarInt>(iter, length);
128 break;
129#endif
130 default:
131 break;
132 }
133 }
134 }
135 return entries;
136 }
137
138 void WriteEntries(const std::map<UUID, PlayerInfoUpdateEntry>& entries, WriteContainer& container) const
139 {
140 WriteData<VarInt>(static_cast<int>(entries.size()), container);
141 for (const auto& p : entries)
142 {
143 WriteData<UUID>(p.first, container);
144 for (const auto a : GetActions())
145 {
146 switch (a)
147 {
149 WriteData<std::string>(p.second.game_profile.GetName(), container);
150 WriteData<std::vector<GameProfileProperty>>(p.second.game_profile.GetProperties(), container);
151 break;
153 WriteData<std::optional<RemoteChatSessionData>>(p.second.chat_session, container);
154 break;
156 WriteData<VarInt>(p.second.game_mode, container);
157 break;
159 WriteData<bool>(p.second.listed, container);
160 break;
162 WriteData<VarInt>(p.second.latency, container);
163 break;
165 WriteData<std::optional<Chat>>(p.second.display_name, container);
166 break;
167#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
169 WriteData<bool>(p.second.show_hat, container);
170 break;
171#endif
172#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
174 WriteData<VarInt>(p.second.list_order, container);
175 break;
176#endif
177 default:
178 break;
179 }
180 }
181 }
182 }
183
184 std::optional<Json::Value> SerializeEntries(const std::map<UUID, PlayerInfoUpdateEntry>& entries) const
185 {
186 Json::Array output;
187 for (const auto& p : entries)
188 {
189 Json::Value entry = Json::Object();
190 entry["uuid"] = p.first;
191 for (const auto a : GetActions())
192 {
193 switch (a)
194 {
196 entry["game_profile"] = p.second.game_profile;
197 break;
199 if (p.second.chat_session.has_value())
200 {
201 entry["chat_session"] = p.second.chat_session.value();
202 }
203 break;
205 entry["game_mode"] = p.second.game_mode;
206 break;
208 entry["listed"] = p.second.listed;
209 break;
211 entry["latency"] = p.second.latency;
212 break;
214 if (p.second.display_name.has_value())
215 {
216 entry["display_name"] = p.second.display_name.value();
217 }
218 break;
219#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
221 entry["show_hat"] = p.second.show_hat;
222 break;
223#endif
224#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
226 entry["list_order"] = p.second.list_order;
227 break;
228#endif
229 default:
230 break;
231 }
232 }
233 output.push_back(entry);
234 }
235 return output;
236 }
237
238 SERIALIZED_FIELD(Actions, Internal::CustomType<std::vector<PlayerInfoUpdateAction>, &THIS::ReadActions, &THIS::WriteActions>);
240
242 };
243} //ProtocolCraft
244#endif
std::map< UUID, PlayerInfoUpdateEntry > ReadEntries(ReadIterator &iter, size_t &length) const
void WriteActions(const std::vector< PlayerInfoUpdateAction > &actions, WriteContainer &container) const
void WriteEntries(const std::map< UUID, PlayerInfoUpdateEntry > &entries, WriteContainer &container) const
std::optional< Json::Value > SerializeEntries(const std::map< UUID, PlayerInfoUpdateEntry > &entries) const
std::vector< PlayerInfoUpdateAction > ReadActions(ReadIterator &iter, size_t &length) const
SERIALIZED_FIELD(Entries, Internal::CustomType< std::map< UUID, PlayerInfoUpdateEntry >, &THIS::ReadEntries, &THIS::WriteEntries, &THIS::SerializeEntries >)
SERIALIZED_FIELD(Actions, Internal::CustomType< std::vector< PlayerInfoUpdateAction >, &THIS::ReadActions, &THIS::WriteActions >)
Real class declaration, just a derived class of std::vector<Value>
Definition Json.hpp:180
Real class declaration, just a derived class of std::map<std::string, Value>
Definition Json.hpp:186
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45
std::array< unsigned char, 16 > UUID
StorageType ReadData(ReadIterator &iter, size_t &length)
std::vector< unsigned char > WriteContainer
std::vector< unsigned char >::const_iterator ReadIterator
std::optional< RemoteChatSessionData > chat_session