mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 06:16:04 +00:00
fix wrong ServerLinkKind and serialize hex colors correctly in nbt
This commit is contained in:
parent
a86d011d4a
commit
2dcfbe96c3
6 changed files with 44 additions and 23 deletions
|
@ -18,22 +18,14 @@ impl Serialize for TextColor {
|
|||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(
|
||||
&self
|
||||
.name
|
||||
.as_ref()
|
||||
.map(|n| n.to_ascii_lowercase())
|
||||
.unwrap_or_else(|| self.format()),
|
||||
)
|
||||
serializer.serialize_str(&self.serialize())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "simdnbt")]
|
||||
impl simdnbt::ToNbtTag for TextColor {
|
||||
fn to_nbt_tag(self) -> simdnbt::owned::NbtTag {
|
||||
self.name
|
||||
.map(|n| NbtTag::String(n.to_ascii_lowercase().into()))
|
||||
.unwrap_or_else(|| NbtTag::Int(self.value as i32))
|
||||
NbtTag::String(self.serialize().into())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -274,18 +266,22 @@ impl TextColor {
|
|||
Self { value, name }
|
||||
}
|
||||
|
||||
pub fn format(&self) -> String {
|
||||
fn serialize(&self) -> String {
|
||||
if let Some(name) = &self.name {
|
||||
name.clone()
|
||||
} else {
|
||||
self.format_value()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_value(&self) -> String {
|
||||
format!("#{:06X}", self.value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for TextColor {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
if let Some(name) = &self.name {
|
||||
write!(f, "{}", name.clone())
|
||||
} else {
|
||||
write!(f, "{}", self.format())
|
||||
}
|
||||
write!(f, "{}", self.serialize())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -47,8 +47,8 @@ pub fn send_packet_events(
|
|||
)) {
|
||||
Ok(packet) => packet,
|
||||
Err(err) => {
|
||||
error!("failed to read packet: {:?}", err);
|
||||
debug!("packet bytes: {:?}", raw_packet);
|
||||
error!("failed to read packet: {err:?}");
|
||||
debug!("packet bytes: {raw_packet:?}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -165,8 +165,8 @@ pub fn send_packet_events(
|
|||
{
|
||||
Ok(packet) => packet,
|
||||
Err(err) => {
|
||||
error!("failed to read packet: {:?}", err);
|
||||
debug!("packet bytes: {:?}", raw_packet);
|
||||
error!("failed to read packet: {err:?}");
|
||||
debug!("packet bytes: {raw_packet:?}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -9,8 +9,8 @@ pub struct ServerLinkEntry {
|
|||
|
||||
#[derive(Clone, Debug, AzBuf)]
|
||||
pub enum ServerLinkKind {
|
||||
Known(KnownLinkKind),
|
||||
Component(FormattedText),
|
||||
Known(KnownLinkKind),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, AzBuf)]
|
||||
|
|
|
@ -19,7 +19,6 @@ mod tests {
|
|||
use azalea_buf::AzaleaRead;
|
||||
|
||||
use super::ClientboundContainerSetContent;
|
||||
use crate::packets::ProtocolPacket;
|
||||
|
||||
#[test]
|
||||
fn test_read_write_container_set_content() {
|
||||
|
@ -29,7 +28,7 @@ mod tests {
|
|||
0, 0, 0, 0, 0, 0, 0, 1, 196, 6, 0, 0, 0,
|
||||
];
|
||||
let mut buf = Cursor::new(contents.as_slice());
|
||||
let packet = ClientboundContainerSetContent::read(&mut buf).unwrap();
|
||||
let packet = ClientboundContainerSetContent::azalea_read(&mut buf).unwrap();
|
||||
println!("{:?}", packet);
|
||||
|
||||
assert_eq!(buf.position(), contents.len() as u64);
|
||||
|
|
|
@ -7,3 +7,29 @@ use crate::common::server_links::ServerLinkEntry;
|
|||
pub struct ClientboundServerLinks {
|
||||
pub links: Vec<ServerLinkEntry>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Cursor;
|
||||
|
||||
use azalea_buf::AzaleaRead;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_read_server_links() {
|
||||
tracing_subscriber::fmt::try_init().ok();
|
||||
let contents = [
|
||||
1, 0, 10, 8, 0, 5, 99, 111, 108, 111, 114, 0, 7, 35, 48, 48, 70, 66, 57, 65, 8, 0, 4,
|
||||
116, 101, 120, 116, 0, 15, 65, 98, 111, 117, 116, 32, 86, 101, 108, 111, 99, 105, 116,
|
||||
97, 98, 0, 40, 104, 116, 116, 112, 115, 58, 47, 47, 119, 105, 108, 108, 105, 97, 109,
|
||||
50, 55, 56, 46, 110, 101, 116, 47, 112, 114, 111, 106, 101, 99, 116, 47, 118, 101, 108,
|
||||
111, 99, 105, 116, 97, 98,
|
||||
];
|
||||
let mut buf = Cursor::new(contents.as_slice());
|
||||
let packet = ClientboundServerLinks::azalea_read(&mut buf).unwrap();
|
||||
println!("{:?}", packet);
|
||||
|
||||
assert_eq!(buf.position(), contents.len() as u64);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue