Botcraft 1.21.4
Loading...
Searching...
No Matches
Chat.cpp
Go to the documentation of this file.
2
3namespace ProtocolCraft
4{
5#if PROTOCOL_VERSION < 765 /* < 1.20.3 */
6 std::string Chat::ParseChat(const Json::Value& raw_json)
7 {
8 if (raw_json.is_object())
9 {
10 std::string output = "";
11
12 if (raw_json.contains("text"))
13 {
14 // It should always be text but just in case
15 if (raw_json["text"].is_string())
16 {
17 output += raw_json["text"].get_string();
18 }
19 }
20 // TODO: deal with other translate types for completeness
21 else if (raw_json.contains("translate") && raw_json["translate"].is_string() && raw_json["translate"].get_string() == "chat.type.text")
22 {
23 // It *should* be <%s> %s, so we only need with[1]
24 if (raw_json.contains("with") && raw_json["with"].is_array() && raw_json["with"].size() > 1)
25 {
26 output += ParseChat(raw_json["with"][1]);
27 }
28 }
29 else
30 {
31 // TODO: deal with other type of content (NBT, scoreboard, selector, keybind)
32 }
33
34 // Add extra
35 if (raw_json.contains("extra") && raw_json["extra"].is_array())
36 {
37 output += ParseChat(raw_json["extra"]);
38 }
39
40 return output;
41 }
42
43 if (raw_json.is_null())
44 {
45 return "";
46 }
47
48 if (raw_json.is_string())
49 {
50 return raw_json.get_string();
51 }
52
53 if (raw_json.is_bool())
54 {
55 return raw_json.get<bool>() ? "true" : "false";
56 }
57
58 if (raw_json.is_number())
59 {
60 return std::to_string(raw_json.get_number());
61 }
62
63 if (raw_json.is_array())
64 {
65 std::string output = "";
66
67 for (const auto& s : raw_json.get_array())
68 {
69 output += ParseChat(s);
70 }
71 return output;
72 }
73
74 return "";
75 }
76#else
77 std::string Chat::ParseChat(const NBT::TagCompound& raw)
78 {
79 std::string output = "";
80
81 if (raw.contains("text") && raw["text"].is<NBT::TagString>())
82 {
83 output += raw["text"].get<NBT::TagString>();
84 }
85 else if (raw.size() == 1 && raw.contains("") && raw[""].is<NBT::TagString>())
86 {
87 output += raw[""].get<NBT::TagString>();
88 }
89 else
90 {
91 // TODO: do we need to process other types of items to get parsed text?
92 }
93
94 // Add extra
95 if (raw.contains("extra") && raw["extra"].is_list_of<NBT::TagCompound>())
96 {
97 for (const auto& t : raw["extra"].as_list_of<NBT::TagCompound>())
98 output += ParseChat(t);
99 }
100
101 return output;
102 }
103#endif
104}
std::string ParseChat(const NBT::TagCompound &raw)
Definition Chat.cpp:77
bool contains(const std::string &s) const
Definition Tag.cpp:567
std::string TagString
Definition Tag.hpp:23