diff options
Diffstat (limited to 'azalea-protocol/src/packets')
| -rw-r--r-- | azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs | 137 |
1 files changed, 108 insertions, 29 deletions
diff --git a/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs index f0f33b36..f7adcafa 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs @@ -1,11 +1,8 @@ -use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable}; +use azalea_buf::McBuf; use azalea_chat::component::Component; use azalea_core::{ResourceLocation, Slot}; use azalea_protocol_macros::ClientboundGamePacket; -use std::{ - collections::HashMap, - io::{Read, Write}, -}; +use std::collections::HashMap; #[derive(Clone, Debug, McBuf, ClientboundGamePacket)] pub struct ClientboundUpdateAdvancementsPacket { @@ -25,40 +22,28 @@ pub struct Advancement { // requirements_strategy: RequirementsStrategy.AND } -#[derive(Clone, Debug, McBuf)] +#[derive(Clone, Debug)] pub struct DisplayInfo { pub title: Component, pub description: Component, pub icon: Slot, pub frame: FrameType, - pub flags: DisplayFlags, + pub show_toast: bool, + pub hidden: bool, pub background: Option<ResourceLocation>, pub x: f32, pub y: f32, } -#[derive(Clone, Debug)] -pub struct DisplayFlags { - pub background: bool, - pub show_toast: bool, - pub hidden: bool, -} +impl azalea_buf::McBufWritable for DisplayInfo { + fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { + self.title.write_into(buf)?; + self.description.write_into(buf)?; + self.icon.write_into(buf)?; + self.frame.write_into(buf)?; -impl McBufReadable for DisplayFlags { - fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> { - let data = u32::read_from(buf)?; - Ok(DisplayFlags { - background: (data & 0b1) != 0, - show_toast: (data & 0b10) != 0, - hidden: (data & 0b100) != 0, - }) - } -} - -impl McBufWritable for DisplayFlags { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - let mut data = 0; - if self.background { + let mut data: u32 = 0; + if self.background.is_some() { data |= 0b1; } if self.show_toast { @@ -67,7 +52,46 @@ impl McBufWritable for DisplayFlags { if self.hidden { data |= 0b100; } - u32::write_into(&data, buf) + data.write_into(buf)?; + + if let Some(background) = &self.background { + background.write_into(buf)?; + } + self.x.write_into(buf)?; + self.y.write_into(buf)?; + Ok(()) + } +} +impl azalea_buf::McBufReadable for DisplayInfo { + fn read_from(buf: &mut impl std::io::Read) -> Result<Self, azalea_buf::BufReadError> { + let title = azalea_buf::McBufReadable::read_from(buf)?; + let description = azalea_buf::McBufReadable::read_from(buf)?; + let icon = azalea_buf::McBufReadable::read_from(buf)?; + let frame = azalea_buf::McBufReadable::read_from(buf)?; + + let data = u32::read_from(buf)?; + let has_background = (data & 0b1) != 0; + let show_toast = (data & 0b10) != 0; + let hidden = (data & 0b100) != 0; + + let background = if has_background { + Some(ResourceLocation::read_from(buf)?) + } else { + None + }; + let x = azalea_buf::McBufReadable::read_from(buf)?; + let y = azalea_buf::McBufReadable::read_from(buf)?; + Ok(DisplayInfo { + title, + description, + icon, + frame, + background, + x, + y, + hidden, + show_toast, + }) } } @@ -88,3 +112,58 @@ pub type AdvancementProgress = HashMap<ResourceLocation, CriterionProgress>; pub struct CriterionProgress { date: Option<u64>, } + +// #[cfg(test)] +// mod tests { +// use super::*; +// use azalea_buf::{McBufReadable, McBufWritable}; +// use azalea_core::ResourceLocation; +// use azalea_protocol_macros::ClientboundGamePacket; +// use std::io::Cursor; + +// #[test] +// fn test() { +// let mut buf = Cursor::new(Vec::new()); +// let packet = ClientboundUpdateAdvancementsPacket { +// reset: true, +// added: [( +// ResourceLocation::new("minecraft:test").unwrap(), +// Advancement { +// parent_id: None, +// display: Some(DisplayInfo { +// title: Component::from("title".to_string()), +// description: Component::from("description".to_string()), +// icon: Slot::Empty, +// frame: FrameType::Task, +// show_toast: true, +// hidden: false, +// background: None, +// x: 0.0, +// y: 0.0, +// }), +// criteria: HashMap::new(), +// requirements: Vec::new(), +// }, +// )] +// .into_iter() +// .collect(), +// removed: vec![ResourceLocation::new("minecraft:test2").unwrap()], +// progress: [( +// ResourceLocation::new("minecraft:test3").unwrap(), +// [( +// ResourceLocation::new("minecraft:test4").unwrap(), +// CriterionProgress { +// date: Some(123456789), +// }, +// )] +// .into_iter() +// .collect(), +// )] +// .into_iter() +// .collect(), +// }; +// packet.write_into(&mut buf).unwrap(); +// let packet = ClientboundUpdateAdvancementsPacket::read_from(&mut buf).unwrap(); +// assert_eq!(packet.reset, true); +// } +// } |
