forked from Quozul/PicoLimbo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.rs
More file actions
177 lines (148 loc) · 4.35 KB
/
component.rs
File metadata and controls
177 lines (148 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use minecraft_protocol::prelude::{BinaryWriter, BinaryWriterError, EncodePacket, ProtocolVersion};
use pico_nbt::prelude::Nbt;
use serde::Serialize;
#[derive(Serialize, PartialEq, Debug, Default, Clone)]
pub struct Component {
#[serde(default)]
pub text: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub color: Option<String>,
#[serde(skip_serializing_if = "is_false", default)]
pub bold: bool,
#[serde(skip_serializing_if = "is_false", default)]
pub italic: bool,
#[serde(skip_serializing_if = "is_false", default)]
pub underlined: bool,
#[serde(skip_serializing_if = "is_false", default)]
pub strikethrough: bool,
#[serde(skip_serializing_if = "is_false", default)]
pub obfuscated: bool,
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub extra: Vec<Component>,
}
const fn is_false(b: &bool) -> bool {
!*b
}
impl Component {
pub fn new<S>(content: S) -> Self
where
S: Into<String>,
{
Self {
text: content.into(),
..Default::default()
}
}
pub fn to_json(&self) -> String {
serde_json::to_string(&self).unwrap_or_default()
}
pub fn to_nbt(&self) -> Nbt {
let mut compound = vec![Nbt::string("text", &self.text)];
if let Some(color) = &self.color {
compound.push(Nbt::string("color", color));
}
if self.bold {
compound.push(Nbt::byte("bold", 1));
}
if self.italic {
compound.push(Nbt::byte("italic", 1));
}
if self.underlined {
compound.push(Nbt::byte("underlined", 1));
}
if self.strikethrough {
compound.push(Nbt::byte("strikethrough", 1));
}
if self.obfuscated {
compound.push(Nbt::byte("obfuscated", 1));
}
if !self.extra.is_empty() {
let mut extras = Vec::with_capacity(self.extra.len());
for extra in &self.extra {
extras.push(extra.to_nbt());
}
compound.push(Nbt::compound_list("extra", extras));
}
Nbt::compound("", compound)
}
pub fn to_legacy(&self) -> String {
#[derive(Serialize)]
struct TextComponent {
#[serde(default)]
text: String,
}
serde_json::to_string(&TextComponent {
text: self.to_legacy_impl(true),
})
.unwrap_or_default()
}
fn to_legacy_impl(&self, is_root: bool) -> String {
let mut s = String::new();
if !is_root {
s.push('§');
s.push('r');
}
if let Some(color) = &self.color {
let color_letter = match color.as_str() {
"black" => '0',
"dark_blue" => '1',
"dark_green" => '2',
"dark_aqua" => '3',
"dark_red" => '4',
"dark_purple" => '5',
"gold" => '6',
"gray" => '7',
"dark_gray" => '8',
"blue" => '9',
"green" => 'a',
"aqua" => 'b',
"red" => 'c',
"light_purple" => 'd',
"yellow" => 'e',
"white" => 'f',
_ => 'f',
};
s.push('§');
s.push(color_letter);
}
if self.bold {
s.push('§');
s.push('l');
}
if self.italic {
s.push('§');
s.push('o');
}
if self.underlined {
s.push('§');
s.push('n');
}
if self.strikethrough {
s.push('§');
s.push('m');
}
if self.obfuscated {
s.push('§');
s.push('k');
}
s.push_str(&self.text);
for extra in &self.extra {
s.push_str(&extra.to_legacy_impl(false));
}
s
}
}
impl EncodePacket for Component {
fn encode(
&self,
writer: &mut BinaryWriter,
protocol_version: ProtocolVersion,
) -> Result<(), BinaryWriterError> {
if protocol_version.is_after_inclusive(ProtocolVersion::V1_20_3) {
self.to_nbt().encode(writer, protocol_version)?;
} else {
self.to_json().encode(writer, protocol_version)?;
}
Ok(())
}
}