diff options
| author | EightFactorial <murphkev000@gmail.com> | 2023-01-30 16:18:14 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-30 18:18:14 -0600 |
| commit | 6e818852d868eea963dd2b8489ba75b65c56fb1c (patch) | |
| tree | e786e919de7d4a1777d91e8e2fa890482970972d /azalea-protocol/src/packets/game/clientbound_commands_packet.rs | |
| parent | 2539f948c7a88a86b27b1878f6c28976285f348c (diff) | |
| download | azalea-drasl-6e818852d868eea963dd2b8489ba75b65c56fb1c.tar.xz | |
More packet fixes, tests, handle error (#61)
* Fix packet, fix tests, fixedbitsets
* Clippy: Nightmare Mode
* Fix mistake
* simplify impl Display and make thing pub
---------
Co-authored-by: mat <github@matdoes.dev>
Diffstat (limited to 'azalea-protocol/src/packets/game/clientbound_commands_packet.rs')
| -rwxr-xr-x | azalea-protocol/src/packets/game/clientbound_commands_packet.rs | 166 |
1 files changed, 112 insertions, 54 deletions
diff --git a/azalea-protocol/src/packets/game/clientbound_commands_packet.rs b/azalea-protocol/src/packets/game/clientbound_commands_packet.rs index 39e807cf..fa11a355 100755 --- a/azalea-protocol/src/packets/game/clientbound_commands_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_commands_packet.rs @@ -2,6 +2,7 @@ use azalea_buf::BufReadError; use azalea_buf::McBuf; use azalea_buf::McBufVarReadable; use azalea_buf::{McBufReadable, McBufVarWritable, McBufWritable}; +use azalea_core::FixedBitSet; use azalea_core::ResourceLocation; use azalea_protocol_macros::ClientboundGamePacket; use log::warn; @@ -15,7 +16,7 @@ pub struct ClientboundCommandsPacket { pub root_index: u32, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct BrigadierNodeStub { pub is_executable: bool, pub children: Vec<u32>, @@ -23,7 +24,7 @@ pub struct BrigadierNodeStub { pub node_type: NodeType, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq)] pub struct BrigadierNumber<T> { pub min: Option<T>, pub max: Option<T>, @@ -33,15 +34,29 @@ impl<T> BrigadierNumber<T> { BrigadierNumber { min, max } } } +impl<T: PartialEq> PartialEq for BrigadierNumber<T> { + fn eq(&self, other: &Self) -> bool { + match (&self.min, &self.max, &other.min, &other.max) { + (Some(f_min), None, Some(s_min), None) => f_min == s_min, + (None, Some(f_max), None, Some(s_max)) => f_max == s_max, + (Some(f_min), Some(f_max), Some(s_min), Some(s_max)) => { + f_min == s_min && f_max == s_max + } + (None, None, None, None) => true, + _ => false, + } + } +} + impl<T: McBufReadable> McBufReadable for BrigadierNumber<T> { fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let flags = u8::read_from(buf)?; - let min = if flags & 0x01 != 0 { + let flags = FixedBitSet::<2>::read_from(buf)?; + let min = if flags.index(0) { Some(T::read_from(buf)?) } else { None }; - let max = if flags & 0x02 != 0 { + let max = if flags.index(1) { Some(T::read_from(buf)?) } else { None @@ -51,12 +66,12 @@ impl<T: McBufReadable> McBufReadable for BrigadierNumber<T> { } impl<T: McBufWritable> McBufWritable for BrigadierNumber<T> { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - let mut flags: u8 = 0; + let mut flags = FixedBitSet::<2>::new(); if self.min.is_some() { - flags |= 0x01; + flags.set(0); } if self.max.is_some() { - flags |= 0x02; + flags.set(1); } flags.write_into(buf)?; if let Some(min) = &self.min { @@ -69,7 +84,7 @@ impl<T: McBufWritable> McBufWritable for BrigadierNumber<T> { } } -#[derive(Debug, Clone, Copy, McBuf)] +#[derive(Debug, Clone, Copy, McBuf, PartialEq, Eq)] pub enum BrigadierString { /// Reads a single word SingleWord = 0, @@ -80,7 +95,7 @@ pub enum BrigadierString { GreedyPhrase = 2, } -#[derive(Debug, Clone, McBuf)] +#[derive(Debug, Clone, McBuf, PartialEq)] pub enum BrigadierParser { Bool, Float(BrigadierNumber<f32>), @@ -132,28 +147,28 @@ pub enum BrigadierParser { Uuid, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct EntityParser { pub single: bool, pub players_only: bool, } impl McBufReadable for EntityParser { fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let flags = u8::read_from(buf)?; + let flags = FixedBitSet::<2>::read_from(buf)?; Ok(EntityParser { - single: flags & 0x01 != 0, - players_only: flags & 0x02 != 0, + single: flags.index(0), + players_only: flags.index(1), }) } } impl McBufWritable for EntityParser { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - let mut flags: u8 = 0; + let mut flags = FixedBitSet::<2>::new(); if self.single { - flags |= 0x01; + flags.set(0); } if self.players_only { - flags |= 0x02; + flags.set(1); } flags.write_into(buf)?; Ok(()) @@ -163,17 +178,15 @@ impl McBufWritable for EntityParser { // TODO: BrigadierNodeStub should have more stuff impl McBufReadable for BrigadierNodeStub { fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> { - let flags = u8::read_from(buf)?; - if flags > 31 { - warn!( - "Warning: The flags from a Brigadier node are over 31 ({flags}; {flags:#b}). This is probably a bug.", - ); + let flags = FixedBitSet::<8>::read_from(buf)?; + if flags.index(5) || flags.index(6) || flags.index(7) { + warn!("Warning: The flags from a Brigadier node are over 31. This is probably a bug.",); } - let node_type = flags & 0x03; - let is_executable = flags & 0x04 != 0; - let has_redirect = flags & 0x08 != 0; - let has_suggestions_type = flags & 0x10 != 0; + let node_type = u8::from(flags.index(0)) + (u8::from(flags.index(1)) * 2); + let is_executable = flags.index(2); + let has_redirect = flags.index(3); + let has_suggestions_type = flags.index(4); let children = Vec::<u32>::var_read_from(buf)?; let redirect_node = if has_redirect { @@ -224,16 +237,17 @@ impl McBufReadable for BrigadierNodeStub { impl McBufWritable for BrigadierNodeStub { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + let mut flags = FixedBitSet::<4>::new(); + if self.is_executable { + flags.set(2); + } + if self.redirect_node.is_some() { + flags.set(3); + } + match &self.node_type { NodeType::Root => { - let mut flags = 0x00; - if self.is_executable { - flags |= 0x04; - } - if self.redirect_node.is_some() { - flags |= 0x08; - } - flags.var_write_into(buf)?; + flags.write_into(buf)?; self.children.var_write_into(buf)?; @@ -242,14 +256,8 @@ impl McBufWritable for BrigadierNodeStub { } } NodeType::Literal { name } => { - let mut flags = 0x01; - if self.is_executable { - flags |= 0x04; - } - if self.redirect_node.is_some() { - flags |= 0x08; - } - flags.var_write_into(buf)?; + flags.set(0); + flags.write_into(buf)?; self.children.var_write_into(buf)?; @@ -264,17 +272,11 @@ impl McBufWritable for BrigadierNodeStub { parser, suggestions_type, } => { - let mut flags = 0x02; - if self.is_executable { - flags |= 0x04; - } - if self.redirect_node.is_some() { - flags |= 0x08; - } + flags.set(1); if suggestions_type.is_some() { - flags |= 0x10; + flags.set(4); } - flags.var_write_into(buf)?; + flags.write_into(buf)?; self.children.var_write_into(buf)?; @@ -294,7 +296,7 @@ impl McBufWritable for BrigadierNodeStub { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum NodeType { Root, Literal { @@ -308,11 +310,67 @@ pub enum NodeType { } impl BrigadierNodeStub { + #[must_use] pub fn name(&self) -> Option<&str> { match &self.node_type { NodeType::Root => None, - NodeType::Literal { name } => Some(name), - NodeType::Argument { name, .. } => Some(name), + NodeType::Literal { name } | NodeType::Argument { name, .. } => Some(name), } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_brigadier_node_stub_root() { + let data = BrigadierNodeStub { + is_executable: false, + children: vec![1, 2], + redirect_node: None, + node_type: NodeType::Root, + }; + let mut buf = Vec::new(); + data.write_into(&mut buf).unwrap(); + let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf); + let read_data = BrigadierNodeStub::read_from(&mut data_cursor).unwrap(); + assert_eq!(data, read_data); + } + + #[test] + fn test_brigadier_node_stub_literal() { + let data = BrigadierNodeStub { + is_executable: true, + children: vec![], + redirect_node: None, + node_type: NodeType::Literal { + name: "String".to_string(), + }, + }; + let mut buf = Vec::new(); + data.write_into(&mut buf).unwrap(); + let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf); + let read_data = BrigadierNodeStub::read_from(&mut data_cursor).unwrap(); + assert_eq!(data, read_data); + } + + #[test] + fn test_brigadier_node_stub_argument() { + let data = BrigadierNodeStub { + is_executable: false, + children: vec![6, 9], + redirect_node: Some(5), + node_type: NodeType::Argument { + name: "position".to_string(), + parser: BrigadierParser::Vec3, + suggestions_type: Some(ResourceLocation::new("minecraft:test_suggestion").unwrap()), + }, + }; + let mut buf = Vec::new(); + data.write_into(&mut buf).unwrap(); + let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf); + let read_data = BrigadierNodeStub::read_from(&mut data_cursor).unwrap(); + assert_eq!(data, read_data); + } +} |
