Botcraft 26.3
Loading...
Searching...
No Matches
Chat.cpp
Go to the documentation of this file.
2
3#if PROTOCOL_VERSION > 764 /* > 1.20.2 */
4#include <cctype>
5#include <sstream>
6#endif
7
8namespace ProtocolCraft
9{
10#if PROTOCOL_VERSION < 765 /* < 1.20.3 */
11 std::string Chat::ParseChat(const Json::Value& raw_json)
12 {
13 if (raw_json.is_object())
14 {
15 std::string output = "";
16
17 if (raw_json.contains("text"))
18 {
19 // It should always be text but just in case
20 if (raw_json["text"].is_string())
21 {
22 output += raw_json["text"].get_string();
23 }
24 }
25 // TODO: deal with other translate types for completeness
26 else if (raw_json.contains("translate") && raw_json["translate"].is_string() && raw_json["translate"].get_string() == "chat.type.text")
27 {
28 // It *should* be <%s> %s, so we only need with[1]
29 if (raw_json.contains("with") && raw_json["with"].is_array() && raw_json["with"].size() > 1)
30 {
31 output += ParseChat(raw_json["with"][1]);
32 }
33 }
34 else
35 {
36 // TODO: deal with other type of content (NBT, scoreboard, selector, keybind)
37 }
38
39 // Add extra
40 if (raw_json.contains("extra") && raw_json["extra"].is_array())
41 {
42 output += ParseChat(raw_json["extra"]);
43 }
44
45 return output;
46 }
47
48 if (raw_json.is_null())
49 {
50 return "";
51 }
52
53 if (raw_json.is_string())
54 {
55 return raw_json.get_string();
56 }
57
58 if (raw_json.is_bool())
59 {
60 return raw_json.get<bool>() ? "true" : "false";
61 }
62
63 if (raw_json.is_number())
64 {
65 return std::to_string(raw_json.get_number());
66 }
67
68 if (raw_json.is_array())
69 {
70 std::string output = "";
71
72 for (const auto& s : raw_json.get_array())
73 {
74 output += ParseChat(s);
75 }
76 return output;
77 }
78
79 return "";
80 }
81#else
83 {
84 std::string output = "";
85
86 if (raw.contains("text") && raw["text"].is<NBT::TagString>())
87 {
88 output += raw["text"].get<NBT::TagString>();
89 }
90 // Weird TagList with mixed types : each element is wrapped in a { "", X } compound
91 else if (raw.size() == 1 && raw.contains(""))
92 {
93 output += ParseChat(raw[""]);
94 }
95 // Translation key
96 else if (raw.contains("translate") && raw["translate"].is<NBT::TagString>())
97 {
98 const std::vector<std::string> args = raw.contains("with") ? ParseChatParts(raw["with"]) : std::vector<std::string>();
99 if (raw.contains("fallback") && raw["fallback"].is<NBT::TagString>())
100 {
101 // Fill %s etc... arguments
102 const std::string& format = raw["fallback"].get<NBT::TagString>();
103 std::string formatted;
104 size_t next_arg = 0;
105 bool malformed = false;
106 for (size_t i = 0; i < format.size(); ++i)
107 {
108 if (format[i] != '%')
109 {
110 formatted += format[i];
111 continue;
112 }
113
114 // This is a special %XXX character
115 size_t j = i + 1;
116 if (j < format.size() && std::isdigit(static_cast<unsigned char>(format[j])))
117 {
118 // Positional argument, %n$s
119 size_t n = 0;
120 while (j < format.size() && std::isdigit(static_cast<unsigned char>(format[j])))
121 {
122 n *= 10;
123 n += format[j] - '0';
124 ++j;
125 }
126 if (n == 0 || n > args.size() || j + 1 >= format.size() || format[j] != '$' || format[j + 1] != 's')
127 {
128 // This probably shouldn't happen
129 malformed = true;
130 break;
131 }
132 else
133 {
134 formatted += args[n - 1];
135 i = j + 1;
136 }
137 }
138 // escaped %%
139 else if (j < format.size() && format[j] == '%')
140 {
141 formatted += '%';
142 i = j;
143 }
144 // replace string with arg
145 else if (j < format.size() && format[j] == 's' && next_arg < args.size())
146 {
147 formatted += args[next_arg++];
148 i = j;
149 }
150 else
151 {
152 // This probably shouldn't happen
153 malformed = true;
154 break;
155 }
156 }
157 output += malformed ? format : formatted;
158 }
159 else
160 {
161 // We don't have any translation available, keep the translation key
162 output += raw["translate"].get<NBT::TagString>();
163 if (!args.empty())
164 {
165 output += "[";
166 for (size_t i = 0; i < args.size(); ++i)
167 {
168 output += (i == 0 ? "" : ", ") + args[i];
169 }
170 output += "]";
171 }
172 }
173 }
174 else if (raw.contains("keybind") && raw["keybind"].is<NBT::TagString>())
175 {
176 output += raw["keybind"].get<NBT::TagString>();
177 }
178 // Should be processed server side I think?
179 else if (raw.contains("score") || raw.contains("nbt"))
180 {
181
182 }
183 else if (raw.contains("selector") && raw["selector"].is<NBT::TagString>())
184 {
185 output += raw["selector"].get<NBT::TagString>();
186 }
187 // Icon
188 else if (raw.contains("object") || raw.contains("sprite") || raw.contains("player"))
189 {
190 if (raw.contains("fallback"))
191 {
192 output += ParseChat(raw["fallback"]);
193 }
194 else if (raw.contains("player") && raw["player"].is<NBT::TagString>())
195 {
196 output += "<" + raw["player"].get<NBT::TagString>() + ">";
197 }
198 else if (raw.contains("player") && raw["player"].contains("name") && raw["player"]["name"].is<NBT::TagString>())
199 {
200 output += "<" + raw["player"]["name"].get<NBT::TagString>() + ">";
201 }
202 }
203
204 // Add extra
205 if (raw.contains("extra"))
206 {
207 output += Chat::ParseChat(raw["extra"]);
208 }
209
210 return output;
211 }
212
213 namespace
214 {
215 template<typename T>
216 void AppendNumbersIfTypeMatch(const NBT::Tag& tag, std::vector<std::string>& parts)
217 {
218 if (tag.is<T>())
219 {
220 std::ostringstream output;
221 if constexpr (std::is_same_v<T, NBT::TagByte>)
222 {
223 output << static_cast<int>(tag.get<T>());
224 }
225 else
226 {
227 output << tag.get<T>();
228 }
229 parts.push_back(output.str());
230 }
231 else if (tag.is_list_of<T>())
232 {
233 for (const T value : tag.as_list_of<T>())
234 {
235 std::ostringstream output;
236 if constexpr (std::is_same_v<T, NBT::TagByte>)
237 {
238 output << static_cast<int>(value);
239 }
240 else
241 {
242 output << value;
243 }
244 parts.push_back(output.str());
245 }
246 }
247 }
248 }
249
250 std::vector<std::string> Chat::ParseChatParts(const NBT::Tag& tag)
251 {
252 if (tag.is<NBT::TagString>())
253 {
254 return { tag.get<NBT::TagString>() };
255 }
256
257 if (tag.is<NBT::TagCompound>())
258 {
259 return { ParseChatCompound(tag.get<NBT::TagCompound>()) };
260 }
261
262 if (tag.is_list_of<NBT::TagString>())
263 {
264 return tag.as_list_of<NBT::TagString>();
265 }
266
267 std::vector<std::string> parts;
268 if (tag.is_list_of<NBT::TagCompound>())
269 {
270 for (const auto& c : tag.as_list_of<NBT::TagCompound>())
271 {
272 parts.push_back(ParseChatCompound(c));
273 }
274 }
275 // Numbers for translation arguments
276 else
277 {
278 AppendNumbersIfTypeMatch<NBT::TagByte>(tag, parts);
279 AppendNumbersIfTypeMatch<NBT::TagShort>(tag, parts);
280 AppendNumbersIfTypeMatch<NBT::TagInt>(tag, parts);
281 AppendNumbersIfTypeMatch<NBT::TagLong>(tag, parts);
282 AppendNumbersIfTypeMatch<NBT::TagFloat>(tag, parts);
283 AppendNumbersIfTypeMatch<NBT::TagDouble>(tag, parts);
284 }
285
286 return parts;
287 }
288
289 std::string Chat::ParseChat(const NBT::Tag& tag)
290 {
291 std::string output = "";
292 for (const std::string& part : ParseChatParts(tag))
293 {
294 output += part;
295 }
296 return output;
297 }
298#endif
299}
static std::vector< std::string > ParseChatParts(const NBT::Tag &tag)
Definition Chat.cpp:250
static std::string ParseChat(const NBT::Tag &tag)
Definition Chat.cpp:289
static std::string ParseChatCompound(const NBT::TagCompound &raw)
Definition Chat.cpp:82
bool contains(const std::string &s) const
Definition Tag.cpp:442
bool is() const
Definition Tag.hpp:198
const T & get() const
Definition Tag.hpp:224
bool is_list_of() const
Definition Tag.hpp:218
const std::vector< T > & as_list_of() const
Definition Tag.hpp:251
std::string TagString
Definition Tag.hpp:24