aboutsummaryrefslogtreecommitdiff
path: root/azalea-chat/src/translatable_component.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2023-12-05 10:59:05 -0600
committerGitHub <noreply@github.com>2023-12-05 10:59:05 -0600
commit7857a014b92e64361ee237ceae7ef1acc185ac46 (patch)
tree5d70ea6b41943493873810e6a03c3483ff90a235 /azalea-chat/src/translatable_component.rs
parentea3e8600126a58f5666d50fbf70dff8209d8979f (diff)
downloadazalea-drasl-7857a014b92e64361ee237ceae7ef1acc185ac46.tar.xz
1.20.3 (#110)
* 23w40a * 23w41a * 23w42a * 23w43a * 23w44a * serialize FormattedText as nbt in network * use azalea-nbt/serde in azalea-chat * 23w45a * fix 23w45a to compile * handle Object in codegen * 1.20.3-pre2 * remove unused clientbound_resource_pack_packet.rs * merge main and make azalea-chat use simdnbt * 1.20.3-rc1 * fix tests * use simdnbt 0.3 * fix ServerboundSetJigsawBlockPacket * 1.20.3
Diffstat (limited to 'azalea-chat/src/translatable_component.rs')
-rwxr-xr-xazalea-chat/src/translatable_component.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/azalea-chat/src/translatable_component.rs b/azalea-chat/src/translatable_component.rs
index 56c6507e..912271ae 100755
--- a/azalea-chat/src/translatable_component.rs
+++ b/azalea-chat/src/translatable_component.rs
@@ -4,6 +4,8 @@ use crate::{
base_component::BaseComponent, style::Style, text_component::TextComponent, FormattedText,
};
use serde::{ser::SerializeMap, Serialize, Serializer, __private::ser::FlatMapSerializer};
+#[cfg(feature = "simdnbt")]
+use simdnbt::Serialize as _;
#[derive(Clone, Debug, PartialEq, Serialize, Eq, Hash)]
#[serde(untagged)]
@@ -12,6 +14,16 @@ pub enum StringOrComponent {
FormattedText(FormattedText),
}
+#[cfg(feature = "simdnbt")]
+impl simdnbt::ToNbtTag for StringOrComponent {
+ fn to_nbt_tag(self) -> simdnbt::owned::NbtTag {
+ match self {
+ StringOrComponent::String(s) => s.to_nbt_tag(),
+ StringOrComponent::FormattedText(c) => c.to_nbt_tag(),
+ }
+ }
+}
+
/// A message whose content depends on the client's language.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TranslatableComponent {
@@ -33,6 +45,54 @@ impl Serialize for TranslatableComponent {
}
}
+#[cfg(feature = "simdnbt")]
+fn serialize_args_as_nbt(args: &[StringOrComponent]) -> simdnbt::owned::NbtList {
+ // if it's all strings then make it a string list
+ // if it's all components then make it a compound list
+ // if it's a mix then return an error
+
+ let mut string_list = Vec::new();
+ let mut compound_list = Vec::new();
+
+ for arg in args {
+ match arg {
+ StringOrComponent::String(s) => {
+ string_list.push(s.clone());
+ }
+ StringOrComponent::FormattedText(c) => {
+ compound_list.push(c.clone().to_compound());
+ }
+ }
+ }
+
+ if !string_list.is_empty() && !compound_list.is_empty() {
+ // i'm actually not sure what vanilla does here, so i just made it return the
+ // string list
+ tracing::debug!(
+ "Tried to serialize a TranslatableComponent with a mix of strings and components."
+ );
+ return string_list.into();
+ }
+
+ if !string_list.is_empty() {
+ return string_list.into();
+ }
+
+ compound_list.into()
+}
+
+#[cfg(feature = "simdnbt")]
+impl simdnbt::Serialize for TranslatableComponent {
+ fn to_compound(self) -> simdnbt::owned::NbtCompound {
+ let mut compound = simdnbt::owned::NbtCompound::new();
+ compound.insert("translate", self.key);
+ compound.extend(self.base.style.to_compound());
+
+ compound.insert("with", serialize_args_as_nbt(&self.args));
+ compound
+ }
+}
+
impl TranslatableComponent {
pub fn new(key: String, args: Vec<StringOrComponent>) -> Self {
Self {