Skip to content

Commit

Permalink
fix parsing textcomponents failing when it's translatable and has a p…
Browse files Browse the repository at this point in the history
…rimitive as an argument
  • Loading branch information
mat-1 committed Feb 24, 2024
1 parent 4eeda83 commit c389573
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 20 deletions.
74 changes: 56 additions & 18 deletions azalea-chat/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serde::{de, Deserialize, Deserializer, Serialize};
#[cfg(feature = "simdnbt")]
use simdnbt::{Deserialize as _, FromNbtTag as _, Serialize as _};
use std::fmt::Display;
use tracing::{trace, warn};

/// A chat component, basically anything you can see in chat.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Hash)]
Expand Down Expand Up @@ -313,22 +314,60 @@ impl simdnbt::FromNbtTag for FormattedText {
// if it's a string component with no styling and no siblings, just add
// a string to with_array otherwise add the
// component to the array
let c = FormattedText::from_nbt_tag(
if let Some(primitive) = item.get("") {
// minecraft does this sometimes, for example
// for the /give system messages
match primitive {
simdnbt::borrow::NbtTag::Byte(b) => {
// interpreted as boolean
with_array.push(StringOrComponent::String(
if *b != 0 { "true" } else { "false" }.to_string(),
));
}
simdnbt::borrow::NbtTag::Short(s) => {
with_array.push(StringOrComponent::String(s.to_string()));
}
simdnbt::borrow::NbtTag::Int(i) => {
with_array.push(StringOrComponent::String(i.to_string()));
}
simdnbt::borrow::NbtTag::Long(l) => {
with_array.push(StringOrComponent::String(l.to_string()));
}
simdnbt::borrow::NbtTag::Float(f) => {
with_array.push(StringOrComponent::String(f.to_string()));
}
simdnbt::borrow::NbtTag::Double(d) => {
with_array.push(StringOrComponent::String(d.to_string()));
}
simdnbt::borrow::NbtTag::String(s) => {
with_array.push(StringOrComponent::String(s.to_string()));
}
_ => {
warn!("couldn't parse {item:?} as FormattedText because it has a disallowed primitive");
with_array.push(StringOrComponent::String("?".to_string()));
}
}
} else if let Some(c) = FormattedText::from_nbt_tag(
&simdnbt::borrow::NbtTag::Compound(item.clone()),
)?;
if let FormattedText::Text(text_component) = c {
if text_component.base.siblings.is_empty()
&& text_component.base.style.is_empty()
{
with_array.push(StringOrComponent::String(text_component.text));
continue;
) {
if let FormattedText::Text(text_component) = c {
if text_component.base.siblings.is_empty()
&& text_component.base.style.is_empty()
{
with_array
.push(StringOrComponent::String(text_component.text));
continue;
}
}
with_array.push(StringOrComponent::FormattedText(
FormattedText::from_nbt_tag(
&simdnbt::borrow::NbtTag::Compound(item.clone()),
)?,
));
} else {
warn!("couldn't parse {item:?} as FormattedText");
with_array.push(StringOrComponent::String("?".to_string()));
}
with_array.push(StringOrComponent::FormattedText(
FormattedText::from_nbt_tag(&simdnbt::borrow::NbtTag::Compound(
item.clone(),
))?,
));
}
component = FormattedText::Translatable(TranslatableComponent::new(
translate, with_array,
Expand All @@ -344,22 +383,21 @@ impl simdnbt::FromNbtTag for FormattedText {
// object = GsonHelper.getAsJsonObject(jsonObject, "score");
if score.get("name").is_none() || score.get("objective").is_none() {
// A score component needs at least a name and an objective
tracing::trace!("A score component needs at least a name and an objective");
trace!("A score component needs at least a name and an objective");
return None;
}
// TODO, score text components aren't yet supported
return None;
} else if compound.get("selector").is_some() {
// selector text components aren't yet supported
tracing::trace!("selector text components aren't yet supported");
trace!("selector text components aren't yet supported");
return None;
} else if compound.get("keybind").is_some() {
// keybind text components aren't yet supported
tracing::trace!("keybind text components aren't yet supported");
trace!("keybind text components aren't yet supported");
return None;
} else {
let Some(_nbt) = compound.get("nbt") else {
// Don't know how to turn 'nbt' into a FormattedText
return None;
};

Check warning on line 402 in azalea-chat/src/component.rs

View workflow job for this annotation

GitHub Actions / clippy

this `let...else` may be rewritten with the `?` operator

warning: this `let...else` may be rewritten with the `?` operator --> azalea-chat/src/component.rs:400:21 | 400 | / let Some(_nbt) = compound.get("nbt") else { 401 | | return None; 402 | | }; | |______________________^ help: replace it with: `let _nbt = compound.get("nbt")?;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#question_mark = note: `#[warn(clippy::question_mark)]` on by default
let _separator = FormattedText::parse_separator_nbt(compound)?;
Expand All @@ -369,7 +407,7 @@ impl simdnbt::FromNbtTag for FormattedText {
None => false,
};
if let Some(_block) = compound.get("block") {}
// nbt text components aren't yet supported
trace!("nbt text components aren't yet supported");
return None;
}
if let Some(extra) = compound.get("extra") {
Expand Down
23 changes: 23 additions & 0 deletions azalea-protocol/src/packets/game/clientbound_system_chat_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,26 @@ pub struct ClientboundSystemChatPacket {
pub content: FormattedText,
pub overlay: bool,
}

#[cfg(test)]
mod tests {
use std::io::Cursor;

use azalea_buf::McBufReadable;

use super::*;

#[test]
fn test_clientbound_system_chat_packet() {
#[rustfmt::skip]
let bytes = [
10, 9, 0, 4, 119, 105, 116, 104, 10, 0, 0, 0, 2, 10, 0, 10, 104, 111, 118, 101, 114, 69, 118, 101, 110, 116, 10, 0, 8, 99, 111, 110, 116, 101, 110, 116, 115, 8, 0, 4, 110, 97, 109, 101, 0, 3, 112, 121, 53, 11, 0, 2, 105, 100, 0, 0, 0, 4, 101, 54, 191, 237, 134, 149, 72, 253, 131, 161, 236, 210, 76, 242, 160, 253, 8, 0, 4, 116, 121, 112, 101, 0, 16, 109, 105, 110, 101, 99, 114, 97, 102, 116, 58, 112, 108, 97, 121, 101, 114, 0, 8, 0, 6, 97, 99, 116, 105, 111, 110, 0, 11, 115, 104, 111, 119, 95, 101, 110, 116, 105, 116, 121, 0, 10, 0, 10, 99, 108, 105, 99, 107, 69, 118, 101, 110, 116, 8, 0, 6, 97, 99, 116, 105, 111, 110, 0, 15, 115, 117, 103, 103, 101, 115, 116, 95, 99, 111, 109, 109, 97, 110, 100, 8, 0, 5, 118, 97, 108, 117, 101, 0, 10, 47, 116, 101, 108, 108, 32, 112, 121, 53, 32, 0, 8, 0, 9, 105, 110, 115, 101, 114, 116, 105, 111, 110, 0, 3, 112, 121, 53, 8, 0, 4, 116, 101, 120, 116, 0, 3, 112, 121, 53, 0, 9, 0, 4, 119, 105, 116, 104, 10, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 1, 0, 10, 0, 10, 104, 111, 118, 101, 114, 69, 118, 101, 110, 116, 10, 0, 8, 99, 111, 110, 116, 101, 110, 116, 115, 8, 0, 2, 105, 100, 0, 25, 109, 105, 110, 101, 99, 114, 97, 102, 116, 58, 100, 105, 97, 109, 111, 110, 100, 95, 112, 105, 99, 107, 97, 120, 101, 8, 0, 3, 116, 97, 103, 0, 10, 123, 68, 97, 109, 97, 103, 101, 58, 48, 125, 0, 8, 0, 6, 97, 99, 116, 105, 111, 110, 0, 9, 115, 104, 111, 119, 95, 105, 116, 101, 109, 0, 9, 0, 4, 119, 105, 116, 104, 10, 0, 0, 0, 1, 9, 0, 5, 101, 120, 116, 114, 97, 10, 0, 0, 0, 1, 8, 0, 9, 116, 114, 97, 110, 115, 108, 97, 116, 101, 0, 30, 105, 116, 101, 109, 46, 109, 105, 110, 101, 99, 114, 97, 102, 116, 46, 100, 105, 97, 109, 111, 110, 100, 95, 112, 105, 99, 107, 97, 120, 101, 0, 8, 0, 4, 116, 101, 120, 116, 0, 0, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 5, 119, 104, 105, 116, 101, 8, 0, 9, 116, 114, 97, 110, 115, 108, 97, 116, 101, 0, 20, 99, 104, 97, 116, 46, 115, 113, 117, 97, 114, 101, 95, 98, 114, 97, 99, 107, 101, 116, 115, 0, 10, 0, 10, 104, 111, 118, 101, 114, 69, 118, 101, 110, 116, 10, 0, 8, 99, 111, 110, 116, 101, 110, 116, 115, 8, 0, 4, 110, 97, 109, 101, 0, 3, 112, 121, 53, 11, 0, 2, 105, 100, 0, 0, 0, 4, 101, 54, 191, 237, 134, 149, 72, 253, 131, 161, 236, 210, 76, 242, 160, 253, 8, 0, 4, 116, 121, 112, 101, 0, 16, 109, 105, 110, 101, 99, 114, 97, 102, 116, 58, 112, 108, 97, 121, 101, 114, 0, 8, 0, 6, 97, 99, 116, 105, 111, 110, 0, 11, 115, 104, 111, 119, 95, 101, 110, 116, 105, 116, 121, 0, 10, 0, 10, 99, 108, 105, 99, 107, 69, 118, 101, 110, 116, 8, 0, 6, 97, 99, 116, 105, 111, 110, 0, 15, 115, 117, 103, 103, 101, 115, 116, 95, 99, 111, 109, 109, 97, 110, 100, 8, 0, 5, 118, 97, 108, 117, 101, 0, 10, 47, 116, 101, 108, 108, 32, 112, 121, 53, 32, 0, 8, 0, 9, 105, 110, 115, 101, 114, 116, 105, 111, 110, 0, 3, 112, 121, 53, 8, 0, 4, 116, 101, 120, 116, 0, 3, 112, 121, 53, 0, 8, 0, 9, 116, 114, 97, 110, 115, 108, 97, 116, 101, 0, 28, 99, 111, 109, 109, 97, 110, 100, 115, 46, 103, 105, 118, 101, 46, 115, 117, 99, 99, 101, 115, 115, 46, 115, 105, 110, 103, 108, 101, 0, 8, 0, 5, 99, 111, 108, 111, 114, 0, 4, 103, 114, 97, 121, 1, 0, 6, 105, 116, 97, 108, 105, 99, 1, 8, 0, 9, 116, 114, 97, 110, 115, 108, 97, 116, 101, 0, 15, 99, 104, 97, 116, 46, 116, 121, 112, 101, 46, 97, 100, 109, 105, 110, 0, 0
];

let packet = ClientboundSystemChatPacket::read_from(&mut Cursor::new(&bytes)).unwrap();
assert_eq!(
packet.content.to_string(),
"[py5: Gave 1 [Diamond Pickaxe] to py5]".to_string()
);
}
}
4 changes: 2 additions & 2 deletions azalea-protocol/src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,10 @@ where
}

if log::log_enabled!(log::Level::Trace) {
const EXTRA_LARGE_LOGS: bool = false;
const DO_NOT_CUT_OFF_PACKET_LOGS: bool = false;

let buf_string: String = {
if !EXTRA_LARGE_LOGS && buf.len() > 500 {
if !DO_NOT_CUT_OFF_PACKET_LOGS && buf.len() > 500 {
let cut_off_buf = &buf[..500];
format!("{cut_off_buf:?}...")
} else {
Expand Down

0 comments on commit c389573

Please sign in to comment.