diff options
| author | mat <github@matdoes.dev> | 2021-12-15 23:07:10 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-15 23:07:10 -0600 |
| commit | 72aefe871ca4983431b1a0b707b472e73ffea836 (patch) | |
| tree | d498fd6cd3272569f2b43ec61ef02d97b21ac3aa /minecraft-protocol/src | |
| parent | 4794b4f1a0a500fed258863d3d4e7216f67c8639 (diff) | |
| download | azalea-drasl-72aefe871ca4983431b1a0b707b472e73ffea836.tar.xz | |
make minecraft-core
Diffstat (limited to 'minecraft-protocol/src')
| -rw-r--r-- | minecraft-protocol/src/packets/login/clientbound_custom_query_packet.rs | 40 | ||||
| -rw-r--r-- | minecraft-protocol/src/packets/login/mod.rs | 19 |
2 files changed, 34 insertions, 25 deletions
diff --git a/minecraft-protocol/src/packets/login/clientbound_custom_query_packet.rs b/minecraft-protocol/src/packets/login/clientbound_custom_query_packet.rs index 54f3dd14..093176eb 100644 --- a/minecraft-protocol/src/packets/login/clientbound_custom_query_packet.rs +++ b/minecraft-protocol/src/packets/login/clientbound_custom_query_packet.rs @@ -1,39 +1,41 @@ use std::hash::Hash; use tokio::io::BufReader; -use crate::mc_buf; +use crate::mc_buf::{self, Readable, Writable}; use super::LoginPacket; #[derive(Hash, Clone, Debug)] pub struct ClientboundCustomQueryPacket { - pub transacton_id: u32, - // TODO: this should be a resource location - pub identifier: String, - pub data: Vec<u8>, + pub transaction_id: u32, + // TODO: this should be a resource location + pub identifier: String, + pub data: Vec<u8>, } -impl ClientboundHelloPacket { +impl ClientboundCustomQueryPacket { pub fn get(self) -> LoginPacket { - LoginPacket::ClientboundHelloPacket(self) + LoginPacket::ClientboundCustomQueryPacket(self) } - pub fn write(&self, _buf: &mut Vec<u8>) { - panic!("ClientboundHelloPacket::write not implemented") + pub fn write(&self, buf: &mut Vec<u8>) { + buf.write_varint(self.transaction_id as i32).unwrap(); + buf.write_utf(&self.identifier).unwrap(); + buf.write_bytes(&self.data).unwrap(); } pub async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>( buf: &mut BufReader<T>, ) -> Result<LoginPacket, String> { - // let server_id = mc_buf::read_utf_with_len(buf, 20).await?; - // let public_key = mc_buf::read_byte_array(buf).await?; - // let nonce = mc_buf::read_byte_array(buf).await?; - - // Ok(ClientboundHelloPacket { - // server_id, - // public_key, - // nonce, - // } - // .get()) + let transaction_id = buf.read_varint().await?.0 as u32; + // TODO: this should be a resource location + let identifier = buf.read_utf().await?; + let data = buf.read_bytes(1048576).await?; + Ok(ClientboundCustomQueryPacket { + transaction_id, + identifier, + data, + } + .get()) } } diff --git a/minecraft-protocol/src/packets/login/mod.rs b/minecraft-protocol/src/packets/login/mod.rs index 70799e22..f0ed6717 100644 --- a/minecraft-protocol/src/packets/login/mod.rs +++ b/minecraft-protocol/src/packets/login/mod.rs @@ -1,3 +1,4 @@ +pub mod clientbound_custom_query_packet; pub mod clientbound_hello_packet; pub mod serverbound_hello_packet; @@ -13,6 +14,7 @@ pub enum LoginPacket where Self: Sized, { + ClientboundCustomQueryPacket(clientbound_custom_query_packet::ClientboundCustomQueryPacket), ServerboundHelloPacket(serverbound_hello_packet::ServerboundHelloPacket), ClientboundHelloPacket(clientbound_hello_packet::ClientboundHelloPacket), } @@ -21,6 +23,7 @@ where impl ProtocolPacket for LoginPacket { fn id(&self) -> u32 { match self { + LoginPacket::ClientboundCustomQueryPacket(_packet) => 0x04, LoginPacket::ServerboundHelloPacket(_packet) => 0x00, LoginPacket::ClientboundHelloPacket(_packet) => 0x01, } @@ -28,6 +31,7 @@ impl ProtocolPacket for LoginPacket { fn write(&self, buf: &mut Vec<u8>) { match self { + LoginPacket::ClientboundCustomQueryPacket(packet) => packet.write(buf), LoginPacket::ServerboundHelloPacket(packet) => packet.write(buf), LoginPacket::ClientboundHelloPacket(packet) => packet.write(buf), } @@ -42,15 +46,18 @@ impl ProtocolPacket for LoginPacket { where Self: Sized, { - match flow { + Ok(match flow { PacketFlow::ServerToClient => match id { - 0x01 => Ok(clientbound_hello_packet::ClientboundHelloPacket::read(buf).await?), - _ => Err(format!("Unknown ServerToClient status packet id: {}", id)), + 0x01 => clientbound_hello_packet::ClientboundHelloPacket::read(buf).await?, + 0x04 => { + clientbound_custom_query_packet::ClientboundCustomQueryPacket::read(buf).await? + } + _ => return Err(format!("Unknown ServerToClient status packet id: {}", id)), }, PacketFlow::ClientToServer => match id { - 0x00 => Ok(serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?), - _ => Err(format!("Unknown ClientToServer status packet id: {}", id)), + 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?, + _ => return Err(format!("Unknown ClientToServer status packet id: {}", id)), }, - } + }) } } |
