From e81b85dd5bdd6d42ee84f24ed4a142f6141f170e Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 1 Jan 2022 19:44:51 -0600 Subject: [PATCH] add a couple more packets --- azalea-client/src/connect.rs | 6 ++++ azalea-protocol/src/mc_buf.rs | 16 ++++++++-- .../game/clientbound_custom_payload_packet.rs | 30 +++++++++++++++++++ .../packets/game/clientbound_login_packet.rs | 17 ----------- ...clientbound_update_view_distance_packet.rs | 28 +++++++++++++++++ azalea-protocol/src/packets/game/mod.rs | 17 +++++++++-- .../login/clientbound_custom_query_packet.rs | 2 +- 7 files changed, 93 insertions(+), 23 deletions(-) create mode 100644 azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs create mode 100644 azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs index 90cb3ad7..c04126dc 100644 --- a/azalea-client/src/connect.rs +++ b/azalea-client/src/connect.rs @@ -64,6 +64,12 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> { GamePacket::ClientboundLoginPacket(p) => { println!("Got login packet {:?}", p); } + GamePacket::ClientboundUpdateViewDistancePacket(p) => { + println!("Got view distance packet {:?}", p); + } + GamePacket::ClientboundCustomPayloadPacket(p) => { + println!("Got custom payload packet {:?}", p); + } // GamePacket::ClientboundKeepAlivePacket(p) => { // println!("Got keep alive packet {:?}", p.keep_alive_id); // } diff --git a/azalea-protocol/src/mc_buf.rs b/azalea-protocol/src/mc_buf.rs index 538fc212..860f61f2 100644 --- a/azalea-protocol/src/mc_buf.rs +++ b/azalea-protocol/src/mc_buf.rs @@ -166,7 +166,8 @@ pub trait Readable { fn get_varint_size(&mut self, value: i32) -> u8; fn get_varlong_size(&mut self, value: i32) -> u8; async fn read_byte_array(&mut self) -> Result, String>; - async fn read_bytes(&mut self, n: usize) -> Result, String>; + async fn read_bytes_with_len(&mut self, n: usize) -> Result, String>; + async fn read_bytes(&mut self) -> Result, String>; async fn read_utf(&mut self) -> Result; async fn read_utf_with_len(&mut self, max_length: u32) -> Result; async fn read_byte(&mut self) -> Result; @@ -230,10 +231,10 @@ where async fn read_byte_array(&mut self) -> Result, String> { let length = self.read_varint().await? as usize; - Ok(self.read_bytes(length).await?) + Ok(self.read_bytes_with_len(length).await?) } - async fn read_bytes(&mut self, n: usize) -> Result, String> { + async fn read_bytes_with_len(&mut self, n: usize) -> Result, String> { let mut bytes = vec![0; n]; match AsyncReadExt::read_exact(self, &mut bytes).await { Ok(_) => Ok(bytes), @@ -241,6 +242,15 @@ where } } + async fn read_bytes(&mut self) -> Result, String> { + // read to end of the buffer + let mut bytes = vec![]; + AsyncReadExt::read_to_end(self, &mut bytes) + .await + .map_err(|_| "Error reading bytes".to_string())?; + Ok(bytes) + } + async fn read_utf(&mut self) -> Result { self.read_utf_with_len(MAX_STRING_LENGTH.into()).await } diff --git a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs new file mode 100644 index 00000000..63047801 --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs @@ -0,0 +1,30 @@ +use super::GamePacket; +use crate::mc_buf::{Readable, Writable}; +use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; + +#[derive(Clone, Debug)] +pub struct ClientboundCustomPayloadPacket { + pub identifier: ResourceLocation, + pub data: Vec, +} + +impl ClientboundCustomPayloadPacket { + pub fn get(self) -> GamePacket { + GamePacket::ClientboundCustomPayloadPacket(self) + } + + pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_resource_location(&self.identifier)?; + buf.write_bytes(&self.data)?; + Ok(()) + } + + pub async fn read( + buf: &mut T, + ) -> Result { + let identifier = buf.read_resource_location().await?; + let data = buf.read_bytes().await?; + + Ok(ClientboundCustomPayloadPacket { identifier, data }.get()) + } +} diff --git a/azalea-protocol/src/packets/game/clientbound_login_packet.rs b/azalea-protocol/src/packets/game/clientbound_login_packet.rs index 9043fa1a..0286fce4 100644 --- a/azalea-protocol/src/packets/game/clientbound_login_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_login_packet.rs @@ -4,23 +4,6 @@ use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; #[derive(Clone, Debug)] pub struct ClientboundLoginPacket { - // private final int playerId; - // private final boolean hardcore; - // private final GameType gameType; - // @Nullable - // private final GameType previousGameType; - // private final Set> levels; - // private final RegistryAccess.RegistryHolder registryHolder; - // private final DimensionType dimensionType; - // private final ResourceKey dimension; - // private final long seed; - // private final int maxPlayers; - // private final int chunkRadius; - // private final int simulationDistance; - // private final boolean reducedDebugInfo; - // private final boolean showDeathScreen; - // private final boolean isDebug; - // private final boolean isFlat; pub player_id: i32, pub hardcore: bool, pub game_type: GameType, diff --git a/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs new file mode 100644 index 00000000..562f8fc2 --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs @@ -0,0 +1,28 @@ +// i don't know the actual name of this packet, i couldn't find it in the source code! + +use super::GamePacket; +use crate::mc_buf::{Readable, Writable}; + +#[derive(Clone, Debug)] +pub struct ClientboundUpdateViewDistancePacket { + pub view_distance: i32, +} + +impl ClientboundUpdateViewDistancePacket { + pub fn get(self) -> GamePacket { + GamePacket::ClientboundUpdateViewDistancePacket(self) + } + + pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_varint(self.view_distance)?; + Ok(()) + } + + pub async fn read( + buf: &mut T, + ) -> Result { + let view_distance = buf.read_varint().await?; + + Ok(ClientboundUpdateViewDistancePacket { view_distance }.get()) + } +} diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 00fa1d75..ab5ca7e8 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -1,4 +1,6 @@ +pub mod clientbound_custom_payload_packet; pub mod clientbound_login_packet; +pub mod clientbound_update_view_distance_packet; use super::ProtocolPacket; use crate::connect::PacketFlow; @@ -10,13 +12,21 @@ where Self: Sized, { ClientboundLoginPacket(clientbound_login_packet::ClientboundLoginPacket), + ClientboundUpdateViewDistancePacket( + clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, + ), + ClientboundCustomPayloadPacket( + clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, + ), } #[async_trait] impl ProtocolPacket for GamePacket { fn id(&self) -> u32 { match self { - GamePacket::ClientboundLoginPacket(_packet) => 0x00, + GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18, + GamePacket::ClientboundLoginPacket(_packet) => 0x26, + GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a, } } @@ -33,8 +43,11 @@ impl ProtocolPacket for GamePacket { { Ok(match flow { PacketFlow::ServerToClient => match id { + 0x18 => clientbound_custom_payload_packet::ClientboundCustomPayloadPacket::read(buf).await?, 0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?, - + 0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket + ::read(buf) + .await?, _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), }, PacketFlow::ClientToServer => match id { diff --git a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs index 2bc1fc1e..048fa53f 100644 --- a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs @@ -26,7 +26,7 @@ impl ClientboundCustomQueryPacket { ) -> Result { let transaction_id = buf.read_varint().await? as u32; let identifier = ResourceLocation::new(&buf.read_utf().await?)?; - let data = buf.read_bytes(1048576).await?; + let data = buf.read_bytes_with_len(1048576).await?; Ok(ClientboundCustomQueryPacket { transaction_id, identifier,