diff options
| author | mat <github@matdoes.dev> | 2021-12-26 14:15:06 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-26 14:15:06 -0600 |
| commit | af28b0e57aeeca8790e3014f3e568c60ae892e39 (patch) | |
| tree | 497fa95e62393111f0b87d31926066153679bb52 /azalea-protocol/src/packets | |
| parent | 1cdd061a999bfa16907ebcc5ab38b1863839b5f1 (diff) | |
| download | azalea-drasl-af28b0e57aeeca8790e3014f3e568c60ae892e39.tar.xz | |
reading nbt in the protocol works
Diffstat (limited to 'azalea-protocol/src/packets')
| -rw-r--r-- | azalea-protocol/src/packets/game/clientbound_login_packet.rs | 77 | ||||
| -rw-r--r-- | azalea-protocol/src/packets/game/mod.rs | 19 | ||||
| -rw-r--r-- | azalea-protocol/src/packets/login/mod.rs | 4 |
3 files changed, 77 insertions, 23 deletions
diff --git a/azalea-protocol/src/packets/game/clientbound_login_packet.rs b/azalea-protocol/src/packets/game/clientbound_login_packet.rs index 54205c48..1b90ce14 100644 --- a/azalea-protocol/src/packets/game/clientbound_login_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_login_packet.rs @@ -1,6 +1,7 @@ use super::GamePacket; use crate::mc_buf::{Readable, Writable}; use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; +use tokio::io::AsyncReadExt; #[derive(Clone, Debug)] pub struct ClientboundLoginPacket { @@ -50,27 +51,73 @@ impl ClientboundLoginPacket { buf.write_byte(self.game_type.to_id())?; buf.write_byte(GameType::to_optional_id(&self.previous_game_type) as u8)?; buf.write_list(&self.levels, |buf, resource_location| { - buf.write_utf(&resource_location.to_string()) + buf.write_resource_location(resource_location) })?; - self.registry_holder - .write(buf) - .map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "write registry holder"))?; - + buf.write_nbt(&self.registry_holder)?; + buf.write_nbt(&self.dimension_type)?; + buf.write_resource_location(&self.dimension)?; + buf.write_long(self.seed)?; + buf.write_varint(self.max_players)?; + buf.write_varint(self.chunk_radius)?; + buf.write_varint(self.simulation_distance)?; + buf.write_boolean(self.reduced_debug_info)?; + buf.write_boolean(self.show_death_screen)?; + buf.write_boolean(self.is_debug)?; + buf.write_boolean(self.is_flat)?; Ok(()) } pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( buf: &mut T, ) -> Result<GamePacket, String> { - let transaction_id = buf.read_varint().await? as u32; - let identifier = ResourceLocation::new(&buf.read_utf().await?)?; - let data = buf.read_bytes(1048576).await?; - panic!("not implemented"); - // Ok(ClientboundLoginPacket { - // transaction_id, - // identifier, - // data, - // } - // .get()) + let player_id = buf.read_int().await?; + let hardcore = buf.read_boolean().await?; + let game_type = GameType::from_id(buf.read_byte().await?)?; + let previous_game_type = GameType::from_optional_id(buf.read_byte().await? as i8)?; + + let mut levels = Vec::new(); + let length = buf.read_varint().await?; + for _ in 0..length { + levels.push(buf.read_resource_location().await?); + } + + // println!("about to read nbt"); + // // read all the bytes into a buffer, print it, and panic + // let mut registry_holder_buf = Vec::new(); + // buf.read_to_end(&mut registry_holder_buf).await.unwrap(); + // println!("{:?}", String::from_utf8_lossy(®istry_holder_buf)); + // panic!(""); + + let registry_holder = buf.read_nbt().await?; + let dimension_type = buf.read_nbt().await?; + let dimension = buf.read_resource_location().await?; + let seed = buf.read_long().await?; + let max_players = buf.read_varint().await?; + let chunk_radius = buf.read_varint().await?; + let simulation_distance = buf.read_varint().await?; + let reduced_debug_info = buf.read_boolean().await?; + let show_death_screen = buf.read_boolean().await?; + let is_debug = buf.read_boolean().await?; + let is_flat = buf.read_boolean().await?; + + Ok(ClientboundLoginPacket { + player_id, + hardcore, + game_type, + previous_game_type, + levels, + registry_holder, + dimension_type, + dimension, + seed, + max_players, + chunk_radius, + simulation_distance, + reduced_debug_info, + show_death_screen, + is_debug, + is_flat, + } + .get()) } } diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 932435d9..5697a0ad 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -25,16 +25,23 @@ impl ProtocolPacket for GamePacket { /// Read a packet by its id, ConnectionProtocol, and flow async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( - _id: u32, + id: u32, flow: &PacketFlow, - _buf: &mut T, + buf: &mut T, ) -> Result<GamePacket, String> where Self: Sized, { - match flow { - PacketFlow::ServerToClient => Err("HandshakePacket::read not implemented".to_string()), - PacketFlow::ClientToServer => Err("HandshakePacket::read not implemented".to_string()), - } + Ok(match flow { + PacketFlow::ServerToClient => match id { + 0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?, + + _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), + }, + PacketFlow::ClientToServer => match id { + // 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?, + _ => return Err(format!("Unknown ClientToServer game packet id: {}", id)), + }, + }) } } diff --git a/azalea-protocol/src/packets/login/mod.rs b/azalea-protocol/src/packets/login/mod.rs index 65d94bed..4d490d08 100644 --- a/azalea-protocol/src/packets/login/mod.rs +++ b/azalea-protocol/src/packets/login/mod.rs @@ -68,11 +68,11 @@ impl ProtocolPacket for LoginPacket { ) .await? } - _ => return Err(format!("Unknown ServerToClient status packet id: {}", id)), + _ => return Err(format!("Unknown ServerToClient login packet id: {}", id)), }, PacketFlow::ClientToServer => match id { 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?, - _ => return Err(format!("Unknown ClientToServer status packet id: {}", id)), + _ => return Err(format!("Unknown ClientToServer login packet id: {}", id)), }, }) } |
