Skip to content

Commit

Permalink
Text component children + fix nbt serialization for sequences of maps (
Browse files Browse the repository at this point in the history
…#349)

* Fix list serialization when elements are maps

* Add extra field and add_child method to TextComponent
  • Loading branch information
vyPal authored Nov 30, 2024
1 parent 136c0fc commit 51365c4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
24 changes: 24 additions & 0 deletions pumpkin-core/src/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,32 @@ pub struct TextComponent<'a> {
/// Also has `ClickEvent
#[serde(flatten)]
pub style: Style<'a>,
/// Extra text components
pub extra: Vec<TextComponent<'a>>,
}

impl<'a> TextComponent<'a> {
pub fn text(text: &'a str) -> Self {
Self {
content: TextContent::Text { text: text.into() },
style: Style::default(),
extra: vec![],
}
}

pub fn text_string(text: String) -> Self {
Self {
content: TextContent::Text { text: text.into() },
style: Style::default(),
extra: vec![],
}
}

pub fn add_child(mut self, child: TextComponent<'a>) -> Self {
self.extra.push(child);
self
}

pub fn to_pretty_console(self) -> String {
let style = self.style;
let color = style.color;
Expand Down Expand Up @@ -174,12 +183,27 @@ impl<'a> TextComponent<'a> {
text: &'a TextContent<'a>,
#[serde(flatten)]
style: &'a Style<'a>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[serde(rename = "extra")]
extra: Vec<&'a TempStruct<'a>>,
}
let temp_extra: Vec<TempStruct> = self
.extra
.iter()
.map(|x| TempStruct {
text: &x.content,
style: &x.style,
extra: vec![],
})
.collect();
let temp_extra_refs: Vec<&TempStruct> = temp_extra.iter().collect();
let astruct = TempStruct {
text: &self.content,
style: &self.style,
extra: temp_extra_refs,
};
// dbg!(&serde_json::to_string(&astruct));
// dbg!(pumpkin_nbt::serializer::to_bytes_unnamed(&astruct).unwrap().to_vec());

// TODO
pumpkin_nbt::serializer::to_bytes_unnamed(&astruct)
Expand Down
8 changes: 7 additions & 1 deletion pumpkin-nbt/src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,13 @@ impl ser::Serializer for &mut Serializer {
}

fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
self.output.put_u8(COMPOUND_ID);
if let State::FirstListElement { .. } = self.state {
self.parse_state(COMPOUND_ID)?;
} else if let State::ListElement = self.state {
return Ok(self);
} else {
self.output.put_u8(COMPOUND_ID);
}
Ok(self)
}

Expand Down

0 comments on commit 51365c4

Please sign in to comment.