From e81b85dd5bdd6d42ee84f24ed4a142f6141f170e Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 1 Jan 2022 19:44:51 -0600 Subject: add a couple more packets --- .../game/clientbound_custom_payload_packet.rs | 30 ++++++++++++++++++++++ .../src/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 +- 5 files changed, 74 insertions(+), 20 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 (limited to 'azalea-protocol/src/packets') 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, -- cgit v1.2.3 From a1afbb6031527c1db5831fc8e916bc0ecce633b4 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 1 Jan 2022 23:55:19 -0600 Subject: start adding packet macros --- Cargo.lock | 26 +++- azalea-protocol/Cargo.toml | 3 + azalea-protocol/packet-macros/Cargo.toml | 14 ++ azalea-protocol/packet-macros/src/lib.rs | 164 +++++++++++++++++++++ azalea-protocol/src/mc_buf.rs | 14 +- .../game/clientbound_custom_payload_packet.rs | 28 +--- .../clientbound_update_view_distance_packet.rs | 23 +-- azalea-protocol/src/packets/game/mod.rs | 7 +- .../packets/handshake/client_intention_packet.rs | 60 +++++--- azalea-protocol/src/packets/handshake/mod.rs | 2 +- .../login/clientbound_custom_query_packet.rs | 3 +- .../login/clientbound_game_profile_packet.rs | 3 +- .../src/packets/login/clientbound_hello_packet.rs | 2 +- .../login/clientbound_login_compression_packet.rs | 3 +- azalea-protocol/src/packets/login/mod.rs | 2 +- .../src/packets/login/serverbound_hello_packet.rs | 3 +- azalea-protocol/src/packets/mod.rs | 8 +- .../status/clientbound_status_response_packet.rs | 4 +- azalea-protocol/src/packets/status/mod.rs | 2 +- .../status/serverbound_status_request_packet.rs | 2 +- 20 files changed, 285 insertions(+), 88 deletions(-) create mode 100644 azalea-protocol/packet-macros/Cargo.toml create mode 100644 azalea-protocol/packet-macros/src/lib.rs (limited to 'azalea-protocol/src/packets') diff --git a/Cargo.lock b/Cargo.lock index c037630d..eca4d4fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -118,6 +118,9 @@ dependencies = [ "azalea-nbt", "byteorder", "bytes", + "num-derive", + "num-traits", + "packet-macros", "serde", "serde_json", "thiserror", @@ -172,6 +175,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" +[[package]] +name = "casey" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fabe85130dda9cf267715582ce6cf1ab581c8dfe3cb33f7065fee0f14e3fea14" +dependencies = [ + "syn", +] + [[package]] name = "cast" version = "0.2.7" @@ -676,6 +688,16 @@ version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" +[[package]] +name = "packet-macros" +version = "0.1.0" +dependencies = [ + "casey", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "parking_lot" version = "0.11.2" @@ -755,9 +777,9 @@ checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba" [[package]] name = "proc-macro2" -version = "1.0.32" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" dependencies = [ "unicode-xid", ] diff --git a/azalea-protocol/Cargo.toml b/azalea-protocol/Cargo.toml index c9883195..ff3bd9d4 100644 --- a/azalea-protocol/Cargo.toml +++ b/azalea-protocol/Cargo.toml @@ -15,6 +15,9 @@ azalea-core = {path = "../azalea-core"} azalea-nbt = {path = "../azalea-nbt"} byteorder = "^1.4.3" bytes = "^1.1.0" +num-derive = "^0.3.3" +num-traits = "^0.2.14" +packet-macros = {path = "./packet-macros"} serde = {version = "1.0.130", features = ["serde_derive"]} serde_json = "^1.0.72" thiserror = "^1.0.30" diff --git a/azalea-protocol/packet-macros/Cargo.toml b/azalea-protocol/packet-macros/Cargo.toml new file mode 100644 index 00000000..5a301756 --- /dev/null +++ b/azalea-protocol/packet-macros/Cargo.toml @@ -0,0 +1,14 @@ +[package] +edition = "2021" +name = "packet-macros" +version = "0.1.0" + +[lib] +proc-macro = true +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +casey = "^0.3.3" +proc-macro2 = "^1.0.36" +quote = "^1.0.10" +syn = "^1.0.82" diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs new file mode 100644 index 00000000..470ac4c1 --- /dev/null +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -0,0 +1,164 @@ +use quote::{quote, quote_spanned, ToTokens}; +use syn::{self, parse_macro_input, spanned::Spanned, DeriveInput, FieldsNamed}; + +fn as_packet_derive( + input: proc_macro::TokenStream, + state: proc_macro2::TokenStream, +) -> proc_macro::TokenStream { + let DeriveInput { ident, data, .. } = parse_macro_input!(input); + + let fields = match data { + syn::Data::Struct(syn::DataStruct { fields, .. }) => fields, + _ => panic!("#[derive(*Packet)] can only be used on structs"), + }; + let FieldsNamed { named, .. } = match fields { + syn::Fields::Named(f) => f, + _ => panic!("#[derive(*Packet)] can only be used on structs with named fields"), + }; + + let write_fields = named + .iter() + .map(|f| { + let field_name = &f.ident; + let field_type = &f.ty; + // do a different buf.write_* for each field depending on the type + // if it's a string, use buf.write_string + match field_type { + syn::Type::Path(syn::TypePath { path, .. }) => { + if path.is_ident("String") { + quote! { buf.write_utf(&self.#field_name)?; } + } else if path.is_ident("ResourceLocation") { + quote! { buf.write_resource_location(&self.#field_name)?; } + // i don't know how to do this in a way that isn't terrible + } else if path.to_token_stream().to_string() == "Vec < u8 >" { + quote! { buf.write_bytes(&self.#field_name)?; } + } else if path.is_ident("i32") { + // only treat it as a varint if it has the varint attribute + if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { + quote! { buf.write_varint(self.#field_name)?; } + } else { + quote! { buf.write_i32(self.#field_name)?; } + } + } else if path.is_ident("u32") { + if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { + quote! { buf.write_varint(self.#field_name as i32)?; } + } else { + quote! { buf.write_u32(self.#field_name)?; } + } + } else if path.is_ident("u16") { + quote! { buf.write_short(self.#field_name as i16)?; } + } else if path.is_ident("ConnectionProtocol") { + quote! { buf.write_varint(self.#field_name.clone() as i32)?; } + } else { + panic!( + "#[derive(*Packet)] doesn't know how to write {}", + path.to_token_stream() + ); + } + } + _ => panic!( + "Error writing field {}: {}", + field_name.clone().unwrap(), + field_type.to_token_stream() + ), + } + }) + .collect::>(); + + let read_fields = named + .iter() + .map(|f| { + let field_name = &f.ident; + let field_type = &f.ty; + // do a different buf.write_* for each field depending on the type + // if it's a string, use buf.write_string + match field_type { + syn::Type::Path(syn::TypePath { path, .. }) => { + if path.is_ident("String") { + quote! { let #field_name = buf.read_utf().await?; } + } else if path.is_ident("ResourceLocation") { + quote! { let #field_name = buf.read_resource_location().await?; } + // i don't know how to do this in a way that isn't terrible + } else if path.to_token_stream().to_string() == "Vec < u8 >" { + quote! { let #field_name = buf.read_bytes().await?; } + } else if path.is_ident("i32") { + // only treat it as a varint if it has the varint attribute + if f.attrs.iter().any(|a| a.path.is_ident("varint")) { + quote! { let #field_name = buf.read_varint().await?; } + } else { + quote! { let #field_name = buf.read_i32().await?; } + } + } else if path.is_ident("u32") { + if f.attrs.iter().any(|a| a.path.is_ident("varint")) { + quote! { let #field_name = buf.read_varint().await? as u32; } + } else { + quote! { let #field_name = buf.read_u32().await?; } + } + } else if path.is_ident("u16") { + quote! { let #field_name = buf.read_short().await? as u16; } + } else if path.is_ident("ConnectionProtocol") { + quote! { + let #field_name = ConnectionProtocol::from_i32(buf.read_varint().await?) + .ok_or_else(|| "Invalid intention".to_string())?; + } + } else { + panic!( + "#[derive(*Packet)] doesn't know how to read {}", + path.to_token_stream() + ); + } + } + _ => panic!( + "Error reading field {}: {}", + field_name.clone().unwrap(), + field_type.to_token_stream() + ), + } + }) + .collect::>(); + let read_field_names = named.iter().map(|f| &f.ident).collect::>(); + + let gen = quote! { + impl #ident { + pub fn get(self) -> #state { + #state::#ident(self) + } + + pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + #(#write_fields)* + Ok(()) + } + + pub async fn read( + buf: &mut T, + ) -> Result<#state, String> { + #(#read_fields)* + Ok(#ident { + #(#read_field_names: #read_field_names),* + }.get()) + } + } + }; + + gen.into() +} + +#[proc_macro_derive(GamePacket, attributes(varint))] +pub fn derive_game_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + as_packet_derive(input, quote! {crate::packets::game::GamePacket}) +} + +#[proc_macro_derive(HandshakePacket, attributes(varint))] +pub fn derive_handshake_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + as_packet_derive(input, quote! {crate::packets::handshake::HandshakePacket}) +} + +#[proc_macro_derive(LoginPacket, attributes(varint))] +pub fn derive_login_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + as_packet_derive(input, quote! {crate::packets::login::LoginPacket}) +} + +#[proc_macro_derive(StatusPacket, attributes(varint))] +pub fn derive_status_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { + as_packet_derive(input, quote! {crate::packets::status::StatusPacket}) +} diff --git a/azalea-protocol/src/mc_buf.rs b/azalea-protocol/src/mc_buf.rs index 860f61f2..72583d5a 100644 --- a/azalea-protocol/src/mc_buf.rs +++ b/azalea-protocol/src/mc_buf.rs @@ -35,7 +35,7 @@ pub trait Writable { fn write_varint(&mut self, value: i32) -> Result<(), std::io::Error>; fn write_utf_with_len(&mut self, string: &str, len: usize) -> Result<(), std::io::Error>; fn write_utf(&mut self, string: &str) -> Result<(), std::io::Error>; - fn write_short(&mut self, n: u16) -> Result<(), std::io::Error>; + fn write_short(&mut self, n: i16) -> Result<(), std::io::Error>; fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error>; fn write_int(&mut self, n: i32) -> Result<(), std::io::Error>; fn write_boolean(&mut self, b: bool) -> Result<(), std::io::Error>; @@ -125,8 +125,8 @@ impl Writable for Vec { self.write_utf_with_len(string, MAX_STRING_LENGTH.into()) } - fn write_short(&mut self, n: u16) -> Result<(), std::io::Error> { - WriteBytesExt::write_u16::(self, n) + fn write_short(&mut self, n: i16) -> Result<(), std::io::Error> { + WriteBytesExt::write_i16::(self, n) } fn write_byte_array(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> { @@ -176,6 +176,7 @@ pub trait Readable { async fn read_nbt(&mut self) -> Result; async fn read_long(&mut self) -> Result; async fn read_resource_location(&mut self) -> Result; + async fn read_short(&mut self) -> Result; } #[async_trait] @@ -334,6 +335,13 @@ where let location = ResourceLocation::new(&location_string)?; Ok(location) } + + async fn read_short(&mut self) -> Result { + match AsyncReadExt::read_i16(self).await { + Ok(r) => Ok(r), + Err(_) => Err("Error reading short".to_string()), + } + } } #[cfg(test)] diff --git a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs index 63047801..24220a83 100644 --- a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs @@ -1,30 +1,10 @@ -use super::GamePacket; use crate::mc_buf::{Readable, Writable}; -use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; +use crate::packets::game::GamePacket; +use azalea_core::resource_location::ResourceLocation; +use packet_macros::GamePacket; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, GamePacket)] 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_update_view_distance_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs index 562f8fc2..f6028e6c 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs @@ -2,27 +2,10 @@ use super::GamePacket; use crate::mc_buf::{Readable, Writable}; +use packet_macros::GamePacket; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, GamePacket)] pub struct ClientboundUpdateViewDistancePacket { + #[varint] 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 ab5ca7e8..43b3ca3d 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -30,7 +30,9 @@ impl ProtocolPacket for GamePacket { } } - fn write(&self, _buf: &mut Vec) {} + fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + Ok(()) + } /// Read a packet by its id, ConnectionProtocol, and flow async fn read( @@ -48,7 +50,8 @@ impl ProtocolPacket for GamePacket { 0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket ::read(buf) .await?, - _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), + // _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), + _ => panic!("Unknown ServerToClient game packet id: {}", id), }, PacketFlow::ClientToServer => match id { // 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?, diff --git a/azalea-protocol/src/packets/handshake/client_intention_packet.rs b/azalea-protocol/src/packets/handshake/client_intention_packet.rs index 939a695e..b3eb8301 100644 --- a/azalea-protocol/src/packets/handshake/client_intention_packet.rs +++ b/azalea-protocol/src/packets/handshake/client_intention_packet.rs @@ -1,34 +1,48 @@ +use crate::{ + mc_buf::{Readable, Writable}, + packets::ConnectionProtocol, +}; +use num_traits::FromPrimitive; +use packet_macros::HandshakePacket; use std::hash::Hash; -use crate::{mc_buf::Writable, packets::ConnectionProtocol}; - -use super::HandshakePacket; - -#[derive(Hash, Clone, Debug)] +#[derive(Hash, Clone, Debug, HandshakePacket)] pub struct ClientIntentionPacket { + #[varint] pub protocol_version: u32, pub hostname: String, pub port: u16, - /// 1 for status, 2 for login pub intention: ConnectionProtocol, } -impl ClientIntentionPacket { - pub fn get(self) -> HandshakePacket { - HandshakePacket::ClientIntentionPacket(self) - } +// impl ClientIntentionPacket { +// pub fn get(self) -> HandshakePacket { +// HandshakePacket::ClientIntentionPacket(self) +// } - pub fn write(&self, buf: &mut Vec) { - buf.write_varint(self.protocol_version as i32).unwrap(); - buf.write_utf(&self.hostname).unwrap(); - buf.write_short(self.port).unwrap(); - buf.write_varint(self.intention.clone() as i32).unwrap(); - } +// pub fn write(&self, buf: &mut Vec) { +// buf.write_varint(self.protocol_version as i32).unwrap(); +// buf.write_utf(&self.hostname).unwrap(); +// buf.write_short(self.port).unwrap(); +// buf.write_varint(self.intention.clone() as i32).unwrap(); +// } - pub async fn read( - _buf: &mut T, - ) -> Result { - Err("ClientIntentionPacket::parse not implemented".to_string()) - // Ok(ClientIntentionPacket {}.get()) - } -} +// pub async fn read( +// buf: &mut T, +// ) -> Result { +// let protocol_version = buf.read_varint().await? as u32; +// let hostname = buf.read_utf().await?; +// let port = buf.read_short().await? as u16; +// let intention = buf.read_varint().await?; + +// Ok(HandshakePacket::ClientIntentionPacket( +// ClientIntentionPacket { +// protocol_version, +// hostname, +// port, +// intention: ConnectionProtocol::from_i32(intention) +// .ok_or_else(|| "Invalid intention".to_string())?, +// }, +// )) +// } +// } diff --git a/azalea-protocol/src/packets/handshake/mod.rs b/azalea-protocol/src/packets/handshake/mod.rs index 1d753645..17465fca 100644 --- a/azalea-protocol/src/packets/handshake/mod.rs +++ b/azalea-protocol/src/packets/handshake/mod.rs @@ -22,7 +22,7 @@ impl ProtocolPacket for HandshakePacket { } } - fn write(&self, buf: &mut Vec) { + fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { match self { HandshakePacket::ClientIntentionPacket(packet) => packet.write(buf), } 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 048fa53f..22e58b0d 100644 --- a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs @@ -15,10 +15,11 @@ impl ClientboundCustomQueryPacket { LoginPacket::ClientboundCustomQueryPacket(self) } - pub fn write(&self, buf: &mut Vec) { + pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { buf.write_varint(self.transaction_id as i32).unwrap(); buf.write_utf(self.identifier.to_string().as_str()).unwrap(); buf.write_bytes(&self.data).unwrap(); + Ok(()) } pub async fn read( diff --git a/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs b/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs index 1a752c1a..c54aa819 100644 --- a/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs @@ -14,11 +14,12 @@ impl ClientboundGameProfilePacket { LoginPacket::ClientboundGameProfilePacket(self) } - pub fn write(&self, buf: &mut Vec) { + pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { for n in self.game_profile.uuid.to_int_array() { buf.write_int(n as i32).unwrap(); } buf.write_utf(self.game_profile.name.as_str()).unwrap(); + Ok(()) } pub async fn read( diff --git a/azalea-protocol/src/packets/login/clientbound_hello_packet.rs b/azalea-protocol/src/packets/login/clientbound_hello_packet.rs index e0b865be..9d0cec39 100644 --- a/azalea-protocol/src/packets/login/clientbound_hello_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_hello_packet.rs @@ -16,7 +16,7 @@ impl ClientboundHelloPacket { LoginPacket::ClientboundHelloPacket(self) } - pub fn write(&self, _buf: &mut Vec) { + pub fn write(&self, _buf: &mut Vec) -> Result<(), std::io::Error> { panic!("ClientboundHelloPacket::write not implemented") } diff --git a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs index af355192..a88c6cbf 100644 --- a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs @@ -14,8 +14,9 @@ impl ClientboundLoginCompressionPacket { LoginPacket::ClientboundLoginCompressionPacket(self) } - pub fn write(&self, buf: &mut Vec) { + pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { buf.write_varint(self.compression_threshold).unwrap(); + Ok(()) } pub async fn read( diff --git a/azalea-protocol/src/packets/login/mod.rs b/azalea-protocol/src/packets/login/mod.rs index 4d490d08..b1f61746 100644 --- a/azalea-protocol/src/packets/login/mod.rs +++ b/azalea-protocol/src/packets/login/mod.rs @@ -34,7 +34,7 @@ impl ProtocolPacket for LoginPacket { } } - fn write(&self, buf: &mut Vec) { + fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { match self { LoginPacket::ClientboundCustomQueryPacket(packet) => packet.write(buf), LoginPacket::ClientboundGameProfilePacket(packet) => packet.write(buf), diff --git a/azalea-protocol/src/packets/login/serverbound_hello_packet.rs b/azalea-protocol/src/packets/login/serverbound_hello_packet.rs index 0039cbce..a72480f2 100644 --- a/azalea-protocol/src/packets/login/serverbound_hello_packet.rs +++ b/azalea-protocol/src/packets/login/serverbound_hello_packet.rs @@ -14,8 +14,9 @@ impl ServerboundHelloPacket { LoginPacket::ServerboundHelloPacket(self) } - pub fn write(&self, buf: &mut Vec) { + pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { buf.write_utf(&self.username).unwrap(); + Ok(()) } pub async fn read( diff --git a/azalea-protocol/src/packets/mod.rs b/azalea-protocol/src/packets/mod.rs index e065b65c..0f1cd2f0 100644 --- a/azalea-protocol/src/packets/mod.rs +++ b/azalea-protocol/src/packets/mod.rs @@ -3,13 +3,13 @@ pub mod handshake; pub mod login; pub mod status; -use async_trait::async_trait; - use crate::connect::PacketFlow; +use async_trait::async_trait; +use num_derive::FromPrimitive; pub const PROTOCOL_VERSION: u32 = 757; -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, FromPrimitive)] pub enum ConnectionProtocol { Handshake = -1, Game = 0, @@ -42,5 +42,5 @@ where where Self: Sized; - fn write(&self, buf: &mut Vec); + fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error>; } diff --git a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs index 38270ad1..58f5b701 100644 --- a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs +++ b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs @@ -39,7 +39,9 @@ impl ClientboundStatusResponsePacket { StatusPacket::ClientboundStatusResponsePacket(Box::new(self)) } - pub fn write(&self, _buf: &mut Vec) {} + pub fn write(&self, _buf: &mut Vec) -> Result<(), std::io::Error> { + Ok(()) + } pub async fn read( buf: &mut T, diff --git a/azalea-protocol/src/packets/status/mod.rs b/azalea-protocol/src/packets/status/mod.rs index 6383bae8..31fedfb9 100644 --- a/azalea-protocol/src/packets/status/mod.rs +++ b/azalea-protocol/src/packets/status/mod.rs @@ -29,7 +29,7 @@ impl ProtocolPacket for StatusPacket { } } - fn write(&self, buf: &mut Vec) { + fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { match self { StatusPacket::ServerboundStatusRequestPacket(packet) => packet.write(buf), StatusPacket::ClientboundStatusResponsePacket(packet) => packet.write(buf), diff --git a/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs b/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs index 3a25ac42..af98f7cb 100644 --- a/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs +++ b/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs @@ -10,7 +10,7 @@ impl ServerboundStatusRequestPacket { StatusPacket::ServerboundStatusRequestPacket(self) } - pub fn write(&self, _buf: &mut Vec) { + pub fn write(&self, _buf: &mut Vec) -> Result<(), std::io::Error> { panic!("ServerboundStatusRequestPacket::write not implemented") } -- cgit v1.2.3 From 3ec7352da2c8a9870718e6a81913f6aa1b576b95 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 2 Jan 2022 00:03:04 -0600 Subject: add macro for a couple more packets --- azalea-protocol/packet-macros/src/lib.rs | 4 +-- .../game/clientbound_custom_payload_packet.rs | 1 - .../clientbound_update_view_distance_packet.rs | 1 - .../packets/handshake/client_intention_packet.rs | 32 ---------------------- .../login/clientbound_custom_query_packet.rs | 32 ++-------------------- 5 files changed, 5 insertions(+), 65 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 470ac4c1..11878abf 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -1,5 +1,5 @@ -use quote::{quote, quote_spanned, ToTokens}; -use syn::{self, parse_macro_input, spanned::Spanned, DeriveInput, FieldsNamed}; +use quote::{quote, ToTokens}; +use syn::{self, parse_macro_input, DeriveInput, FieldsNamed}; fn as_packet_derive( input: proc_macro::TokenStream, diff --git a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs index 24220a83..4ee0ddf0 100644 --- a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs @@ -1,5 +1,4 @@ use crate::mc_buf::{Readable, Writable}; -use crate::packets::game::GamePacket; use azalea_core::resource_location::ResourceLocation; use packet_macros::GamePacket; 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 index f6028e6c..cd48b304 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs @@ -1,6 +1,5 @@ // 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}; use packet_macros::GamePacket; diff --git a/azalea-protocol/src/packets/handshake/client_intention_packet.rs b/azalea-protocol/src/packets/handshake/client_intention_packet.rs index b3eb8301..00d124a4 100644 --- a/azalea-protocol/src/packets/handshake/client_intention_packet.rs +++ b/azalea-protocol/src/packets/handshake/client_intention_packet.rs @@ -14,35 +14,3 @@ pub struct ClientIntentionPacket { pub port: u16, pub intention: ConnectionProtocol, } - -// impl ClientIntentionPacket { -// pub fn get(self) -> HandshakePacket { -// HandshakePacket::ClientIntentionPacket(self) -// } - -// pub fn write(&self, buf: &mut Vec) { -// buf.write_varint(self.protocol_version as i32).unwrap(); -// buf.write_utf(&self.hostname).unwrap(); -// buf.write_short(self.port).unwrap(); -// buf.write_varint(self.intention.clone() as i32).unwrap(); -// } - -// pub async fn read( -// buf: &mut T, -// ) -> Result { -// let protocol_version = buf.read_varint().await? as u32; -// let hostname = buf.read_utf().await?; -// let port = buf.read_short().await? as u16; -// let intention = buf.read_varint().await?; - -// Ok(HandshakePacket::ClientIntentionPacket( -// ClientIntentionPacket { -// protocol_version, -// hostname, -// port, -// intention: ConnectionProtocol::from_i32(intention) -// .ok_or_else(|| "Invalid intention".to_string())?, -// }, -// )) -// } -// } 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 22e58b0d..77262c43 100644 --- a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs @@ -1,38 +1,12 @@ -use super::LoginPacket; use crate::mc_buf::{Readable, Writable}; use azalea_core::resource_location::ResourceLocation; +use packet_macros::LoginPacket; use std::hash::Hash; -#[derive(Hash, Clone, Debug)] +#[derive(Hash, Clone, Debug, LoginPacket)] pub struct ClientboundCustomQueryPacket { + #[varint] pub transaction_id: u32, pub identifier: ResourceLocation, pub data: Vec, } - -impl ClientboundCustomQueryPacket { - pub fn get(self) -> LoginPacket { - LoginPacket::ClientboundCustomQueryPacket(self) - } - - pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_varint(self.transaction_id as i32).unwrap(); - buf.write_utf(self.identifier.to_string().as_str()).unwrap(); - buf.write_bytes(&self.data).unwrap(); - Ok(()) - } - - pub async fn read( - buf: &mut T, - ) -> Result { - let transaction_id = buf.read_varint().await? as u32; - let identifier = ResourceLocation::new(&buf.read_utf().await?)?; - let data = buf.read_bytes_with_len(1048576).await?; - Ok(ClientboundCustomQueryPacket { - transaction_id, - identifier, - data, - } - .get()) - } -} -- cgit v1.2.3 From bb566aa54131a23b6d9e605c81a8ff4d1d1c21d7 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 2 Jan 2022 17:07:01 -0600 Subject: implement for Vec --- azalea-protocol/packet-macros/src/lib.rs | 7 +------ azalea-protocol/src/mc_buf/read.rs | 10 ++++++++++ azalea-protocol/src/mc_buf/write.rs | 6 ++++++ .../packets/game/clientbound_update_view_distance_packet.rs | 3 +-- 4 files changed, 18 insertions(+), 8 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 22c39ea9..5b2088a1 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -29,9 +29,6 @@ fn as_packet_derive( quote! { buf.write_utf(&self.#field_name)?; } } else if path.is_ident("ResourceLocation") { quote! { buf.write_resource_location(&self.#field_name)?; } - // i don't know how to do this in a way that isn't terrible - } else if path.to_token_stream().to_string() == "Vec < u8 >" { - quote! { buf.write_bytes(&self.#field_name)?; } } else if path.is_ident("u32") { if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { quote! { buf.write_varint(self.#field_name as i32)?; } @@ -49,7 +46,7 @@ fn as_packet_derive( } } else { quote! { - crate::mc_buf::McBufVarintWritable::write_into(&self.#field_name, buf)?; + crate::mc_buf::McBufWritable::write_into(&self.#field_name, buf)?; } } @@ -77,8 +74,6 @@ fn as_packet_derive( quote! { let #field_name = buf.read_utf().await?; } } else if path.is_ident("ResourceLocation") { quote! { let #field_name = buf.read_resource_location().await?; } - } else if path.to_token_stream().to_string() == "Vec < u8 >" { - quote! { let #field_name = buf.read_bytes().await?; } } else if path.is_ident("u32") { if f.attrs.iter().any(|a| a.path.is_ident("varint")) { quote! { let #field_name = buf.read_varint().await? as u32; } diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index c429eb7f..683c7d9a 100644 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -228,3 +228,13 @@ impl McBufVarintReadable for i32 { buf.read_varint().await } } + +#[async_trait] +impl McBufReadable for Vec { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_bytes().await + } +} diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index 3c3375b9..7b1ac861 100644 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -178,3 +178,9 @@ impl McBufVarintWritable for i32 { buf.write_varint(*self) } } + +impl McBufWritable for Vec { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_bytes(self) + } +} 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 index cd48b304..8301c089 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs @@ -1,6 +1,5 @@ -// i don't know the actual name of this packet, i couldn't find it in the source code! +// i don't know the actual name of this packet, i couldn't find it in the source code -use crate::mc_buf::{Readable, Writable}; use packet_macros::GamePacket; #[derive(Clone, Debug, GamePacket)] -- cgit v1.2.3 From 094c210dada7c0ee83c19965739312d2d00e4099 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 2 Jan 2022 17:19:04 -0600 Subject: switch all current macro types to the new system --- azalea-protocol/packet-macros/src/lib.rs | 46 +++------------------- azalea-protocol/src/mc_buf/read.rs | 66 ++++++++++++++++++++++++++++++++ azalea-protocol/src/mc_buf/write.rs | 42 ++++++++++++++++++++ azalea-protocol/src/packets/mod.rs | 24 +++++++++++- 4 files changed, 136 insertions(+), 42 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 5b2088a1..ba963111 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -25,31 +25,14 @@ fn as_packet_derive( // if it's a string, use buf.write_string match field_type { syn::Type::Path(syn::TypePath { path, .. }) => { - if path.is_ident("String") { - quote! { buf.write_utf(&self.#field_name)?; } - } else if path.is_ident("ResourceLocation") { - quote! { buf.write_resource_location(&self.#field_name)?; } - } else if path.is_ident("u32") { - if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { - quote! { buf.write_varint(self.#field_name as i32)?; } - } else { - quote! { buf.write_u32(self.#field_name)?; } + if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { + quote! { + crate::mc_buf::McBufVarintWritable::varint_write_into(&self.#field_name, buf)?; } - } else if path.is_ident("u16") { - quote! { buf.write_short(self.#field_name as i16)?; } - } else if path.is_ident("ConnectionProtocol") { - quote! { buf.write_varint(self.#field_name.clone() as i32)?; } } else { - if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { - quote! { - crate::mc_buf::McBufVarintWritable::varint_write_into(&self.#field_name, buf)?; - } - } else { - quote! { - crate::mc_buf::McBufWritable::write_into(&self.#field_name, buf)?; - } + quote! { + crate::mc_buf::McBufWritable::write_into(&self.#field_name, buf)?; } - } } _ => panic!( @@ -70,24 +53,6 @@ fn as_packet_derive( // if it's a string, use buf.write_string match field_type { syn::Type::Path(syn::TypePath { path, .. }) => { - if path.is_ident("String") { - quote! { let #field_name = buf.read_utf().await?; } - } else if path.is_ident("ResourceLocation") { - quote! { let #field_name = buf.read_resource_location().await?; } - } else if path.is_ident("u32") { - if f.attrs.iter().any(|a| a.path.is_ident("varint")) { - quote! { let #field_name = buf.read_varint().await? as u32; } - } else { - quote! { let #field_name = buf.read_u32().await?; } - } - } else if path.is_ident("u16") { - quote! { let #field_name = buf.read_short().await? as u16; } - } else if path.is_ident("ConnectionProtocol") { - quote! { - let #field_name = ConnectionProtocol::from_i32(buf.read_varint().await?) - .ok_or_else(|| "Invalid intention".to_string())?; - } - } else { if f.attrs.iter().any(|a| a.path.is_ident("varint")) { quote! { let #field_name = crate::mc_buf::McBufVarintReadable::varint_read_into(buf).await?; @@ -96,7 +61,6 @@ fn as_packet_derive( quote! { let #field_name = crate::mc_buf::McBufReadable::read_into(buf).await?; } - } } } _ => panic!( diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index 683c7d9a..374e5443 100644 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -238,3 +238,69 @@ impl McBufReadable for Vec { buf.read_bytes().await } } + +// string +#[async_trait] +impl McBufReadable for String { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_utf().await + } +} + +// ResourceLocation +#[async_trait] +impl McBufReadable for ResourceLocation { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_resource_location().await + } +} + +// u32 +#[async_trait] +impl McBufReadable for u32 { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_int().await.map(|i| i as u32) + } +} + +// u32 varint +#[async_trait] +impl McBufVarintReadable for u32 { + async fn varint_read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_varint().await.map(|i| i as u32) + } +} + +// u16 +#[async_trait] +impl McBufReadable for u16 { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_short().await.map(|i| i as u16) + } +} + +// u16 varint +#[async_trait] +impl McBufVarintReadable for u16 { + async fn varint_read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_varint().await.map(|i| i as u16) + } +} diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index 7b1ac861..f22b218a 100644 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -184,3 +184,45 @@ impl McBufWritable for Vec { buf.write_bytes(self) } } + +// string +impl McBufWritable for String { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_utf(self) + } +} + +// ResourceLocation +impl McBufWritable for ResourceLocation { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_resource_location(self) + } +} + +// u32 +impl McBufWritable for u32 { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_varint(*self as i32) + } +} + +// u32 varint +impl McBufVarintWritable for u32 { + fn varint_write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_varint(*self as i32) + } +} + +// u16 +impl McBufWritable for u16 { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_varint(*self as i32) + } +} + +// u16 varint +impl McBufVarintWritable for u16 { + fn varint_write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_varint(*self as i32) + } +} diff --git a/azalea-protocol/src/packets/mod.rs b/azalea-protocol/src/packets/mod.rs index 0f1cd2f0..f35451c6 100644 --- a/azalea-protocol/src/packets/mod.rs +++ b/azalea-protocol/src/packets/mod.rs @@ -3,9 +3,14 @@ pub mod handshake; pub mod login; pub mod status; -use crate::connect::PacketFlow; +use crate::{ + connect::PacketFlow, + mc_buf::{McBufReadable, McBufWritable, Readable, Writable}, +}; use async_trait::async_trait; use num_derive::FromPrimitive; +use num_traits::FromPrimitive; +use tokio::io::AsyncRead; pub const PROTOCOL_VERSION: u32 = 757; @@ -44,3 +49,20 @@ where fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error>; } + +#[async_trait] +impl McBufReadable for ConnectionProtocol { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + ConnectionProtocol::from_i32(buf.read_varint().await?) + .ok_or_else(|| "Invalid intention".to_string()) + } +} + +impl McBufWritable for ConnectionProtocol { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_varint(self.clone() as i32) + } +} -- cgit v1.2.3 From 45871fc01e212a50ac5e6268e4677f97f8fe5bb3 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 2 Jan 2022 17:40:18 -0600 Subject: better parsing for entire login packet --- azalea-protocol/src/mc_buf/read.rs | 84 +++++++++++++++++++- azalea-protocol/src/mc_buf/write.rs | 68 ++++++++++++++-- .../src/packets/game/clientbound_login_packet.rs | 90 ++-------------------- 3 files changed, 151 insertions(+), 91 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index 374e5443..5127860e 100644 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use azalea_core::resource_location::ResourceLocation; +use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; use tokio::io::{AsyncRead, AsyncReadExt}; use super::MAX_STRING_LENGTH; @@ -304,3 +304,85 @@ impl McBufVarintReadable for u16 { buf.read_varint().await.map(|i| i as u16) } } + +// i64 +#[async_trait] +impl McBufReadable for i64 { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_long().await + } +} + +// u64 +#[async_trait] +impl McBufReadable for u64 { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + i64::read_into(buf).await.map(|i| i as u64) + } +} + +// bool +#[async_trait] +impl McBufReadable for bool { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_boolean().await + } +} + +// GameType +#[async_trait] +impl McBufReadable for GameType { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + GameType::from_id(buf.read_byte().await?) + } +} + +// Option +#[async_trait] +impl McBufReadable for Option { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + GameType::from_optional_id(buf.read_byte().await? as i8) + } +} + +// Vec +#[async_trait] +impl McBufReadable for Vec { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + let mut vec = Vec::new(); + let length = buf.read_varint().await?; + for _ in 0..length { + vec.push(buf.read_resource_location().await?); + } + Ok(vec) + } +} + +// azalea_nbt::Tag +#[async_trait] +impl McBufReadable for azalea_nbt::Tag { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_nbt().await + } +} diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index f22b218a..14dac9d1 100644 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use azalea_core::resource_location::ResourceLocation; +use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; use byteorder::{BigEndian, WriteBytesExt}; use std::io::Write; @@ -202,27 +202,85 @@ impl McBufWritable for ResourceLocation { // u32 impl McBufWritable for u32 { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_varint(*self as i32) + i32::varint_write_into(&(*self as i32), buf) } } // u32 varint impl McBufVarintWritable for u32 { fn varint_write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_varint(*self as i32) + i32::varint_write_into(&(*self as i32), buf) } } // u16 impl McBufWritable for u16 { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_varint(*self as i32) + i32::varint_write_into(&(*self as i32), buf) } } // u16 varint impl McBufVarintWritable for u16 { fn varint_write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_varint(*self as i32) + i32::varint_write_into(&(*self as i32), buf) + } +} + +// u8 +impl McBufWritable for u8 { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_byte(*self) + } +} + +// i64 +impl McBufWritable for i64 { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + Writable::write_long(buf, *self) + } +} + +// u64 +impl McBufWritable for u64 { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + i64::write_into(&(*self as i64), buf) + } +} + +// bool +impl McBufWritable for bool { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_boolean(*self) + } +} + +// GameType +impl McBufWritable for GameType { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + u8::write_into(&self.to_id(), buf) + } +} + +// Option +impl McBufWritable for Option { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_byte(GameType::to_optional_id(&self) as u8) + } +} + +// Vec +impl McBufWritable for Vec { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_list(&self, |buf, resource_location| { + buf.write_resource_location(resource_location) + }) + } +} + +// azalea_nbt::Tag +impl McBufWritable for azalea_nbt::Tag { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_nbt(self) } } diff --git a/azalea-protocol/src/packets/game/clientbound_login_packet.rs b/azalea-protocol/src/packets/game/clientbound_login_packet.rs index 0286fce4..57869202 100644 --- a/azalea-protocol/src/packets/game/clientbound_login_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_login_packet.rs @@ -1,8 +1,7 @@ -use super::GamePacket; -use crate::mc_buf::{Readable, Writable}; use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; +use packet_macros::GamePacket; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, GamePacket)] pub struct ClientboundLoginPacket { pub player_id: i32, pub hardcore: bool, @@ -13,93 +12,14 @@ pub struct ClientboundLoginPacket { pub dimension_type: azalea_nbt::Tag, pub dimension: ResourceLocation, pub seed: i64, + #[varint] pub max_players: i32, + #[varint] pub chunk_radius: i32, + #[varint] pub simulation_distance: i32, pub reduced_debug_info: bool, pub show_death_screen: bool, pub is_debug: bool, pub is_flat: bool, } - -impl ClientboundLoginPacket { - pub fn get(self) -> GamePacket { - GamePacket::ClientboundLoginPacket(self) - } - - pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_int(self.player_id)?; - buf.write_boolean(self.hardcore)?; - 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_resource_location(resource_location) - })?; - 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( - buf: &mut T, - ) -> Result { - 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()) - } -} -- cgit v1.2.3 From 394f996df27bedc68be6c1f9e9764e8f78ba6282 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 2 Jan 2022 17:42:41 -0600 Subject: fix random warnings --- azalea-client/src/connect.rs | 7 ------- azalea-core/src/game_type.rs | 2 -- azalea-protocol/packet-macros/src/lib.rs | 4 ++-- azalea-protocol/src/mc_buf/write.rs | 4 ++-- .../src/packets/game/clientbound_custom_payload_packet.rs | 1 - azalea-protocol/src/packets/handshake/client_intention_packet.rs | 6 +----- .../src/packets/login/clientbound_custom_query_packet.rs | 1 - azalea-protocol/src/write.rs | 2 +- 8 files changed, 6 insertions(+), 21 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs index c04126dc..6ed25bff 100644 --- a/azalea-client/src/connect.rs +++ b/azalea-client/src/connect.rs @@ -70,13 +70,6 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> { GamePacket::ClientboundCustomPayloadPacket(p) => { println!("Got custom payload packet {:?}", p); } - // GamePacket::ClientboundKeepAlivePacket(p) => { - // println!("Got keep alive packet {:?}", p.keep_alive_id); - // } - // GamePacket::ClientboundChatMessagePacket(p) => { - // println!("Got chat message packet {:?}", p.message); - // } - _ => panic!("unhandled packet"), }, Err(e) => { println!("Error: {:?}", e); diff --git a/azalea-core/src/game_type.rs b/azalea-core/src/game_type.rs index b6ff479d..f5b9fb38 100644 --- a/azalea-core/src/game_type.rs +++ b/azalea-core/src/game_type.rs @@ -1,5 +1,3 @@ -use azalea_chat; - #[derive(Hash, Clone, Debug)] pub enum GameType { SURVIVAL, diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index ba963111..957515c4 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -24,7 +24,7 @@ fn as_packet_derive( // do a different buf.write_* for each field depending on the type // if it's a string, use buf.write_string match field_type { - syn::Type::Path(syn::TypePath { path, .. }) => { + syn::Type::Path(_) => { if f.attrs.iter().any(|attr| attr.path.is_ident("varint")) { quote! { crate::mc_buf::McBufVarintWritable::varint_write_into(&self.#field_name, buf)?; @@ -52,7 +52,7 @@ fn as_packet_derive( // do a different buf.write_* for each field depending on the type // if it's a string, use buf.write_string match field_type { - syn::Type::Path(syn::TypePath { path, .. }) => { + syn::Type::Path(_) => { if f.attrs.iter().any(|a| a.path.is_ident("varint")) { quote! { let #field_name = crate::mc_buf::McBufVarintReadable::varint_read_into(buf).await?; diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index 14dac9d1..6fbe6eab 100644 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -265,14 +265,14 @@ impl McBufWritable for GameType { // Option impl McBufWritable for Option { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_byte(GameType::to_optional_id(&self) as u8) + buf.write_byte(GameType::to_optional_id(self) as u8) } } // Vec impl McBufWritable for Vec { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_list(&self, |buf, resource_location| { + buf.write_list(self, |buf, resource_location| { buf.write_resource_location(resource_location) }) } diff --git a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs index 4ee0ddf0..134a3109 100644 --- a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs @@ -1,4 +1,3 @@ -use crate::mc_buf::{Readable, Writable}; use azalea_core::resource_location::ResourceLocation; use packet_macros::GamePacket; diff --git a/azalea-protocol/src/packets/handshake/client_intention_packet.rs b/azalea-protocol/src/packets/handshake/client_intention_packet.rs index 00d124a4..6216ddc4 100644 --- a/azalea-protocol/src/packets/handshake/client_intention_packet.rs +++ b/azalea-protocol/src/packets/handshake/client_intention_packet.rs @@ -1,8 +1,4 @@ -use crate::{ - mc_buf::{Readable, Writable}, - packets::ConnectionProtocol, -}; -use num_traits::FromPrimitive; +use crate::packets::ConnectionProtocol; use packet_macros::HandshakePacket; use std::hash::Hash; 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 77262c43..3138106e 100644 --- a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs @@ -1,4 +1,3 @@ -use crate::mc_buf::{Readable, Writable}; use azalea_core::resource_location::ResourceLocation; use packet_macros::LoginPacket; use std::hash::Hash; diff --git a/azalea-protocol/src/write.rs b/azalea-protocol/src/write.rs index 3898e74a..821ed6e8 100644 --- a/azalea-protocol/src/write.rs +++ b/azalea-protocol/src/write.rs @@ -17,7 +17,7 @@ fn packet_encoder(packet: &P) -> Result MAXIMUM_UNCOMPRESSED_LENGTH as usize { return Err(format!( "Packet too big (is {} bytes, should be less than {}): {:?}", -- cgit v1.2.3 From 96eba2b39a596dd19c29a93aaa3b5bb9b700ba62 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 3 Jan 2022 00:14:41 -0600 Subject: difficulty packet --- azalea-client/src/connect.rs | 3 + azalea-core/src/difficulty.rs | 96 ++++++++++++++++++++++ azalea-core/src/lib.rs | 10 +-- azalea-protocol/src/mc_buf/read.rs | 38 ++++++++- azalea-protocol/src/mc_buf/write.rs | 19 ++++- .../game/clientbound_change_difficulty_packet.rs | 8 ++ azalea-protocol/src/packets/game/mod.rs | 15 +++- 7 files changed, 177 insertions(+), 12 deletions(-) create mode 100644 azalea-core/src/difficulty.rs create mode 100644 azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs (limited to 'azalea-protocol/src/packets') diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs index 6ed25bff..7b1da525 100644 --- a/azalea-client/src/connect.rs +++ b/azalea-client/src/connect.rs @@ -70,6 +70,9 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> { GamePacket::ClientboundCustomPayloadPacket(p) => { println!("Got custom payload packet {:?}", p); } + GamePacket::ClientboundChangeDifficultyPacket(p) => { + println!("Got difficulty packet {:?}", p); + } }, Err(e) => { println!("Error: {:?}", e); diff --git a/azalea-core/src/difficulty.rs b/azalea-core/src/difficulty.rs new file mode 100644 index 00000000..21e980ba --- /dev/null +++ b/azalea-core/src/difficulty.rs @@ -0,0 +1,96 @@ +use std::fmt::{Debug, Error, Formatter}; + +#[derive(Hash, Clone, Debug, PartialEq)] +pub enum Difficulty { + PEACEFUL, + EASY, + NORMAL, + HARD, +} + +pub enum Err { + InvalidDifficulty(String), +} + +impl Debug for Err { + fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { + match self { + Err::InvalidDifficulty(s) => write!(f, "Invalid difficulty: {}", s), + } + } +} + +impl Difficulty { + pub fn name(&self) -> &'static str { + match self { + Difficulty::PEACEFUL => "peaceful", + Difficulty::EASY => "easy", + Difficulty::NORMAL => "normal", + Difficulty::HARD => "hard", + } + } + + pub fn from_name(name: &str) -> Result { + match name { + "peaceful" => Ok(Difficulty::PEACEFUL), + "easy" => Ok(Difficulty::EASY), + "normal" => Ok(Difficulty::NORMAL), + "hard" => Ok(Difficulty::HARD), + _ => Err(Err::InvalidDifficulty(name.to_string())), + } + } + + pub fn by_id(id: u8) -> Difficulty { + match id % 4 { + 0 => Difficulty::PEACEFUL, + 1 => Difficulty::EASY, + 2 => Difficulty::NORMAL, + 3 => Difficulty::HARD, + // this shouldn't be possible because of the modulo, so panicking is fine + _ => panic!("Unknown difficulty id: {}", id), + } + } + + pub fn id(&self) -> u8 { + match self { + Difficulty::PEACEFUL => 0, + Difficulty::EASY => 1, + Difficulty::NORMAL => 2, + Difficulty::HARD => 3, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_difficulty_from_name() { + assert_eq!( + Difficulty::PEACEFUL, + Difficulty::from_name("peaceful").unwrap() + ); + assert_eq!(Difficulty::EASY, Difficulty::from_name("easy").unwrap()); + assert_eq!(Difficulty::NORMAL, Difficulty::from_name("normal").unwrap()); + assert_eq!(Difficulty::HARD, Difficulty::from_name("hard").unwrap()); + assert!(Difficulty::from_name("invalid").is_err()); + } + + #[test] + fn test_difficulty_id() { + assert_eq!(0, Difficulty::PEACEFUL.id()); + assert_eq!(1, Difficulty::EASY.id()); + assert_eq!(2, Difficulty::NORMAL.id()); + assert_eq!(3, Difficulty::HARD.id()); + assert_eq!(4, Difficulty::PEACEFUL.id()); + } + + #[test] + fn test_difficulty_name() { + assert_eq!("peaceful", Difficulty::PEACEFUL.name()); + assert_eq!("easy", Difficulty::EASY.name()); + assert_eq!("normal", Difficulty::NORMAL.name()); + assert_eq!("hard", Difficulty::HARD.name()); + } +} diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs index 887d1686..cdf07c43 100644 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -1,14 +1,6 @@ //! Random miscellaneous things like UUIDs that don't deserve their own crate. +pub mod difficulty; pub mod game_type; pub mod resource_location; pub mod serializable_uuid; - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); - } -} diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index 5127860e..0fa1d099 100644 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -1,5 +1,8 @@ use async_trait::async_trait; -use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; +use azalea_core::{ + difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, +}; +use num_traits::FromPrimitive; use tokio::io::{AsyncRead, AsyncReadExt}; use super::MAX_STRING_LENGTH; @@ -338,6 +341,28 @@ impl McBufReadable for bool { } } +// u8 +#[async_trait] +impl McBufReadable for u8 { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_byte().await + } +} + +// i8 +#[async_trait] +impl McBufReadable for i8 { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_byte().await.map(|i| i as i8) + } +} + // GameType #[async_trait] impl McBufReadable for GameType { @@ -386,3 +411,14 @@ impl McBufReadable for azalea_nbt::Tag { buf.read_nbt().await } } + +// Difficulty +#[async_trait] +impl McBufReadable for Difficulty { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + Ok(Difficulty::by_id(u8::read_into(buf).await?)) + } +} diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index 6fbe6eab..fd9faeb4 100644 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -1,6 +1,9 @@ use async_trait::async_trait; -use azalea_core::{game_type::GameType, resource_location::ResourceLocation}; +use azalea_core::{ + difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, +}; use byteorder::{BigEndian, WriteBytesExt}; +use num_traits::FromPrimitive; use std::io::Write; use super::MAX_STRING_LENGTH; @@ -255,6 +258,13 @@ impl McBufWritable for bool { } } +// i8 +impl McBufWritable for i8 { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_byte(*self as u8) + } +} + // GameType impl McBufWritable for GameType { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { @@ -284,3 +294,10 @@ impl McBufWritable for azalea_nbt::Tag { buf.write_nbt(self) } } + +// Difficulty +impl McBufWritable for Difficulty { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + u8::write_into(&self.id(), buf) + } +} diff --git a/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs b/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs new file mode 100644 index 00000000..e12cfff3 --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs @@ -0,0 +1,8 @@ +use azalea_core::difficulty::Difficulty; +use packet_macros::GamePacket; + +#[derive(Clone, Debug, GamePacket)] +pub struct ClientboundChangeDifficultyPacket { + pub difficulty: Difficulty, + pub locked: bool, +} diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 43b3ca3d..4efe72fb 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -1,3 +1,4 @@ +pub mod clientbound_change_difficulty_packet; pub mod clientbound_custom_payload_packet; pub mod clientbound_login_packet; pub mod clientbound_update_view_distance_packet; @@ -18,12 +19,16 @@ where ClientboundCustomPayloadPacket( clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, ), + ClientboundChangeDifficultyPacket( + clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, + ), } #[async_trait] impl ProtocolPacket for GamePacket { fn id(&self) -> u32 { match self { + GamePacket::ClientboundChangeDifficultyPacket(_packet) => 0x0e, GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18, GamePacket::ClientboundLoginPacket(_packet) => 0x26, GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a, @@ -31,7 +36,12 @@ impl ProtocolPacket for GamePacket { } fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - Ok(()) + match self { + GamePacket::ClientboundChangeDifficultyPacket(packet) => packet.write(buf), + GamePacket::ClientboundCustomPayloadPacket(packet) => packet.write(buf), + GamePacket::ClientboundLoginPacket(packet) => packet.write(buf), + GamePacket::ClientboundUpdateViewDistancePacket(packet) => packet.write(buf), + } } /// Read a packet by its id, ConnectionProtocol, and flow @@ -45,6 +55,9 @@ impl ProtocolPacket for GamePacket { { Ok(match flow { PacketFlow::ServerToClient => match id { + 0x0e => clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket + ::read(buf) + .await?, 0x18 => clientbound_custom_payload_packet::ClientboundCustomPayloadPacket::read(buf).await?, 0x26 => clientbound_login_packet::ClientboundLoginPacket::read(buf).await?, 0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket -- cgit v1.2.3 From bd87cbb4434ba8bdf16ad93c5353ccefc0497d13 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 3 Jan 2022 13:36:02 -0600 Subject: create all empty brigadier modules --- azalea-brigadier/Cargo.toml | 8 +++++ azalea-brigadier/src/ambiguity_consumer.rs | 0 azalea-brigadier/src/arguments/argument_type.rs | 0 .../src/arguments/bool_argument_type.rs | 0 .../src/arguments/double_argument_type.rs | 0 .../src/arguments/float_argument_type.rs | 0 .../src/arguments/integer_argument_type.rs | 0 .../src/arguments/long_argument_type.rs | 0 .../src/arguments/string_argument_type.rs | 0 azalea-brigadier/src/builder/argument_builder.rs | 0 .../src/builder/literal_argument_builder.rs | 0 .../src/builder/required_argument_builder.rs | 0 azalea-brigadier/src/command.rs | 0 azalea-brigadier/src/command_dispatcher.rs | 0 azalea-brigadier/src/context/command_context.rs | 0 .../src/context/command_context_builder.rs | 0 azalea-brigadier/src/context/parsed_argument.rs | 0 .../src/context/parsed_command_node.rs | 0 azalea-brigadier/src/context/string_range.rs | 0 azalea-brigadier/src/context/suggestion_context.rs | 0 .../src/exceptions/builtin_exception_provider.rs | 0 .../src/exceptions/builtin_exceptions.rs | 0 .../src/exceptions/command_exception_type.rs | 0 .../src/exceptions/command_syntax_exception.rs | 0 .../exceptions/dynamic2_command_exception_type.rs | 0 .../exceptions/dynamic3_command_exception_type.rs | 0 .../exceptions/dynamic4_command_exception_type.rs | 0 .../exceptions/dynamicN_command_exception_type.rs | 0 .../exceptions/dynamic_command_exception_type.rs | 0 .../exceptions/simple_command_exception_type.rs | 0 azalea-brigadier/src/immutable_string_reader.rs | 0 azalea-brigadier/src/lib.rs | 8 +++++ azalea-brigadier/src/literal_message.rs | 0 azalea-brigadier/src/message.rs | 0 azalea-brigadier/src/parse_results.rs | 0 azalea-brigadier/src/redirect_modifier.rs | 0 azalea-brigadier/src/result_consumer.rs | 0 azalea-brigadier/src/single_redirect_modifier.rs | 0 azalea-brigadier/src/string_reader.rs | 0 .../src/suggestion/integer_suggestion.rs | 0 azalea-brigadier/src/suggestion/suggestion.rs | 0 .../src/suggestion/suggestion_provider.rs | 0 azalea-brigadier/src/suggestion/suggestions.rs | 0 .../src/suggestion/suggestions_builder.rs | 0 azalea-brigadier/src/tree/argument_command_node.rs | 0 azalea-brigadier/src/tree/command_node.rs | 0 azalea-brigadier/src/tree/literal_command_node.rs | 0 azalea-brigadier/src/tree/root_command_node.rs | 0 .../tests/arguments/bool_argument_type_test.rs | 0 .../tests/arguments/double_argument_type_test.rs | 0 .../tests/arguments/float_argument_type_test.rs | 0 .../tests/arguments/integer_argument_type_test.rs | 0 .../tests/arguments/long_argument_type_test.rs | 0 .../tests/arguments/string_argument_type_test.rs | 0 .../tests/builder/argument_builder_test.rs | 0 .../tests/builder/literal_argument_builder_test.rs | 0 .../builder/required_argument_builder_test.rs | 0 azalea-brigadier/tests/command_dispatcher_test.rs | 0 .../tests/command_dispatcher_usages_test.rs | 0 azalea-brigadier/tests/command_suggestions_test.rs | 0 .../tests/context/command_context_test.rs | 0 .../tests/context/parsed_argument_test.rs | 0 .../dynamic_command_syntax_exception_type_test.rs | 0 .../simple_command_syntax_exception_type_test.rs | 0 azalea-brigadier/tests/string_reader_test.rs | 0 .../tests/suggestion/suggestion_test.rs | 0 .../tests/suggestion/suggestions_builder_test.rs | 0 .../tests/suggestion/suggestions_test.rs | 0 .../tests/tree/abstract_command_node_test.rs | 0 .../tests/tree/argument_command_node_test.rs | 0 .../tests/tree/literal_command_node_test.rs | 0 .../tests/tree/root_command_node_test.rs | 0 .../game/clientbound_declare_commands_packet.rs | 37 ++++++++++++++++++++++ 73 files changed, 53 insertions(+) create mode 100644 azalea-brigadier/Cargo.toml create mode 100644 azalea-brigadier/src/ambiguity_consumer.rs create mode 100644 azalea-brigadier/src/arguments/argument_type.rs create mode 100644 azalea-brigadier/src/arguments/bool_argument_type.rs create mode 100644 azalea-brigadier/src/arguments/double_argument_type.rs create mode 100644 azalea-brigadier/src/arguments/float_argument_type.rs create mode 100644 azalea-brigadier/src/arguments/integer_argument_type.rs create mode 100644 azalea-brigadier/src/arguments/long_argument_type.rs create mode 100644 azalea-brigadier/src/arguments/string_argument_type.rs create mode 100644 azalea-brigadier/src/builder/argument_builder.rs create mode 100644 azalea-brigadier/src/builder/literal_argument_builder.rs create mode 100644 azalea-brigadier/src/builder/required_argument_builder.rs create mode 100644 azalea-brigadier/src/command.rs create mode 100644 azalea-brigadier/src/command_dispatcher.rs create mode 100644 azalea-brigadier/src/context/command_context.rs create mode 100644 azalea-brigadier/src/context/command_context_builder.rs create mode 100644 azalea-brigadier/src/context/parsed_argument.rs create mode 100644 azalea-brigadier/src/context/parsed_command_node.rs create mode 100644 azalea-brigadier/src/context/string_range.rs create mode 100644 azalea-brigadier/src/context/suggestion_context.rs create mode 100644 azalea-brigadier/src/exceptions/builtin_exception_provider.rs create mode 100644 azalea-brigadier/src/exceptions/builtin_exceptions.rs create mode 100644 azalea-brigadier/src/exceptions/command_exception_type.rs create mode 100644 azalea-brigadier/src/exceptions/command_syntax_exception.rs create mode 100644 azalea-brigadier/src/exceptions/dynamic2_command_exception_type.rs create mode 100644 azalea-brigadier/src/exceptions/dynamic3_command_exception_type.rs create mode 100644 azalea-brigadier/src/exceptions/dynamic4_command_exception_type.rs create mode 100644 azalea-brigadier/src/exceptions/dynamicN_command_exception_type.rs create mode 100644 azalea-brigadier/src/exceptions/dynamic_command_exception_type.rs create mode 100644 azalea-brigadier/src/exceptions/simple_command_exception_type.rs create mode 100644 azalea-brigadier/src/immutable_string_reader.rs create mode 100644 azalea-brigadier/src/lib.rs create mode 100644 azalea-brigadier/src/literal_message.rs create mode 100644 azalea-brigadier/src/message.rs create mode 100644 azalea-brigadier/src/parse_results.rs create mode 100644 azalea-brigadier/src/redirect_modifier.rs create mode 100644 azalea-brigadier/src/result_consumer.rs create mode 100644 azalea-brigadier/src/single_redirect_modifier.rs create mode 100644 azalea-brigadier/src/string_reader.rs create mode 100644 azalea-brigadier/src/suggestion/integer_suggestion.rs create mode 100644 azalea-brigadier/src/suggestion/suggestion.rs create mode 100644 azalea-brigadier/src/suggestion/suggestion_provider.rs create mode 100644 azalea-brigadier/src/suggestion/suggestions.rs create mode 100644 azalea-brigadier/src/suggestion/suggestions_builder.rs create mode 100644 azalea-brigadier/src/tree/argument_command_node.rs create mode 100644 azalea-brigadier/src/tree/command_node.rs create mode 100644 azalea-brigadier/src/tree/literal_command_node.rs create mode 100644 azalea-brigadier/src/tree/root_command_node.rs create mode 100644 azalea-brigadier/tests/arguments/bool_argument_type_test.rs create mode 100644 azalea-brigadier/tests/arguments/double_argument_type_test.rs create mode 100644 azalea-brigadier/tests/arguments/float_argument_type_test.rs create mode 100644 azalea-brigadier/tests/arguments/integer_argument_type_test.rs create mode 100644 azalea-brigadier/tests/arguments/long_argument_type_test.rs create mode 100644 azalea-brigadier/tests/arguments/string_argument_type_test.rs create mode 100644 azalea-brigadier/tests/builder/argument_builder_test.rs create mode 100644 azalea-brigadier/tests/builder/literal_argument_builder_test.rs create mode 100644 azalea-brigadier/tests/builder/required_argument_builder_test.rs create mode 100644 azalea-brigadier/tests/command_dispatcher_test.rs create mode 100644 azalea-brigadier/tests/command_dispatcher_usages_test.rs create mode 100644 azalea-brigadier/tests/command_suggestions_test.rs create mode 100644 azalea-brigadier/tests/context/command_context_test.rs create mode 100644 azalea-brigadier/tests/context/parsed_argument_test.rs create mode 100644 azalea-brigadier/tests/exceptions/dynamic_command_syntax_exception_type_test.rs create mode 100644 azalea-brigadier/tests/exceptions/simple_command_syntax_exception_type_test.rs create mode 100644 azalea-brigadier/tests/string_reader_test.rs create mode 100644 azalea-brigadier/tests/suggestion/suggestion_test.rs create mode 100644 azalea-brigadier/tests/suggestion/suggestions_builder_test.rs create mode 100644 azalea-brigadier/tests/suggestion/suggestions_test.rs create mode 100644 azalea-brigadier/tests/tree/abstract_command_node_test.rs create mode 100644 azalea-brigadier/tests/tree/argument_command_node_test.rs create mode 100644 azalea-brigadier/tests/tree/literal_command_node_test.rs create mode 100644 azalea-brigadier/tests/tree/root_command_node_test.rs create mode 100644 azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs (limited to 'azalea-protocol/src/packets') diff --git a/azalea-brigadier/Cargo.toml b/azalea-brigadier/Cargo.toml new file mode 100644 index 00000000..c617ffb1 --- /dev/null +++ b/azalea-brigadier/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "azalea-brigadier" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/azalea-brigadier/src/ambiguity_consumer.rs b/azalea-brigadier/src/ambiguity_consumer.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/arguments/argument_type.rs b/azalea-brigadier/src/arguments/argument_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/arguments/bool_argument_type.rs b/azalea-brigadier/src/arguments/bool_argument_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/arguments/double_argument_type.rs b/azalea-brigadier/src/arguments/double_argument_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/arguments/float_argument_type.rs b/azalea-brigadier/src/arguments/float_argument_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/arguments/integer_argument_type.rs b/azalea-brigadier/src/arguments/integer_argument_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/arguments/long_argument_type.rs b/azalea-brigadier/src/arguments/long_argument_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/arguments/string_argument_type.rs b/azalea-brigadier/src/arguments/string_argument_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/builder/literal_argument_builder.rs b/azalea-brigadier/src/builder/literal_argument_builder.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/builder/required_argument_builder.rs b/azalea-brigadier/src/builder/required_argument_builder.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/command.rs b/azalea-brigadier/src/command.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/context/command_context.rs b/azalea-brigadier/src/context/command_context.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/context/command_context_builder.rs b/azalea-brigadier/src/context/command_context_builder.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/context/parsed_argument.rs b/azalea-brigadier/src/context/parsed_argument.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/context/parsed_command_node.rs b/azalea-brigadier/src/context/parsed_command_node.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/context/string_range.rs b/azalea-brigadier/src/context/string_range.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/context/suggestion_context.rs b/azalea-brigadier/src/context/suggestion_context.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/exceptions/builtin_exception_provider.rs b/azalea-brigadier/src/exceptions/builtin_exception_provider.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/exceptions/builtin_exceptions.rs b/azalea-brigadier/src/exceptions/builtin_exceptions.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/exceptions/command_exception_type.rs b/azalea-brigadier/src/exceptions/command_exception_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/exceptions/command_syntax_exception.rs b/azalea-brigadier/src/exceptions/command_syntax_exception.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/exceptions/dynamic2_command_exception_type.rs b/azalea-brigadier/src/exceptions/dynamic2_command_exception_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/exceptions/dynamic3_command_exception_type.rs b/azalea-brigadier/src/exceptions/dynamic3_command_exception_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/exceptions/dynamic4_command_exception_type.rs b/azalea-brigadier/src/exceptions/dynamic4_command_exception_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/exceptions/dynamicN_command_exception_type.rs b/azalea-brigadier/src/exceptions/dynamicN_command_exception_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/exceptions/dynamic_command_exception_type.rs b/azalea-brigadier/src/exceptions/dynamic_command_exception_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/exceptions/simple_command_exception_type.rs b/azalea-brigadier/src/exceptions/simple_command_exception_type.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/immutable_string_reader.rs b/azalea-brigadier/src/immutable_string_reader.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/lib.rs b/azalea-brigadier/src/lib.rs new file mode 100644 index 00000000..1b4a90c9 --- /dev/null +++ b/azalea-brigadier/src/lib.rs @@ -0,0 +1,8 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); + } +} diff --git a/azalea-brigadier/src/literal_message.rs b/azalea-brigadier/src/literal_message.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/message.rs b/azalea-brigadier/src/message.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/parse_results.rs b/azalea-brigadier/src/parse_results.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/redirect_modifier.rs b/azalea-brigadier/src/redirect_modifier.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/result_consumer.rs b/azalea-brigadier/src/result_consumer.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/single_redirect_modifier.rs b/azalea-brigadier/src/single_redirect_modifier.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/string_reader.rs b/azalea-brigadier/src/string_reader.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/suggestion/integer_suggestion.rs b/azalea-brigadier/src/suggestion/integer_suggestion.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/suggestion/suggestion.rs b/azalea-brigadier/src/suggestion/suggestion.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/suggestion/suggestion_provider.rs b/azalea-brigadier/src/suggestion/suggestion_provider.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/suggestion/suggestions.rs b/azalea-brigadier/src/suggestion/suggestions.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/suggestion/suggestions_builder.rs b/azalea-brigadier/src/suggestion/suggestions_builder.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/tree/argument_command_node.rs b/azalea-brigadier/src/tree/argument_command_node.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/tree/command_node.rs b/azalea-brigadier/src/tree/command_node.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/tree/literal_command_node.rs b/azalea-brigadier/src/tree/literal_command_node.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/src/tree/root_command_node.rs b/azalea-brigadier/src/tree/root_command_node.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/arguments/bool_argument_type_test.rs b/azalea-brigadier/tests/arguments/bool_argument_type_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/arguments/double_argument_type_test.rs b/azalea-brigadier/tests/arguments/double_argument_type_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/arguments/float_argument_type_test.rs b/azalea-brigadier/tests/arguments/float_argument_type_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/arguments/integer_argument_type_test.rs b/azalea-brigadier/tests/arguments/integer_argument_type_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/arguments/long_argument_type_test.rs b/azalea-brigadier/tests/arguments/long_argument_type_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/arguments/string_argument_type_test.rs b/azalea-brigadier/tests/arguments/string_argument_type_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/builder/argument_builder_test.rs b/azalea-brigadier/tests/builder/argument_builder_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/builder/literal_argument_builder_test.rs b/azalea-brigadier/tests/builder/literal_argument_builder_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/builder/required_argument_builder_test.rs b/azalea-brigadier/tests/builder/required_argument_builder_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/command_dispatcher_test.rs b/azalea-brigadier/tests/command_dispatcher_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/command_dispatcher_usages_test.rs b/azalea-brigadier/tests/command_dispatcher_usages_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/command_suggestions_test.rs b/azalea-brigadier/tests/command_suggestions_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/context/command_context_test.rs b/azalea-brigadier/tests/context/command_context_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/context/parsed_argument_test.rs b/azalea-brigadier/tests/context/parsed_argument_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/exceptions/dynamic_command_syntax_exception_type_test.rs b/azalea-brigadier/tests/exceptions/dynamic_command_syntax_exception_type_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/exceptions/simple_command_syntax_exception_type_test.rs b/azalea-brigadier/tests/exceptions/simple_command_syntax_exception_type_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/string_reader_test.rs b/azalea-brigadier/tests/string_reader_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/suggestion/suggestion_test.rs b/azalea-brigadier/tests/suggestion/suggestion_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/suggestion/suggestions_builder_test.rs b/azalea-brigadier/tests/suggestion/suggestions_builder_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/suggestion/suggestions_test.rs b/azalea-brigadier/tests/suggestion/suggestions_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/tree/abstract_command_node_test.rs b/azalea-brigadier/tests/tree/abstract_command_node_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/tree/argument_command_node_test.rs b/azalea-brigadier/tests/tree/argument_command_node_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/tree/literal_command_node_test.rs b/azalea-brigadier/tests/tree/literal_command_node_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-brigadier/tests/tree/root_command_node_test.rs b/azalea-brigadier/tests/tree/root_command_node_test.rs new file mode 100644 index 00000000..e69de29b diff --git a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs new file mode 100644 index 00000000..1bcf0dd4 --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs @@ -0,0 +1,37 @@ +// use std::hash::Hash; + +// use crate::mc_buf::Readable; + +// use super::LoginPacket; + +// #[derive(Hash, Clone, Debug)] +// pub struct ClientboundDeclareCommandsPacket { +// pub root: RootCommandNode, +// pub public_key: Vec, +// pub nonce: Vec, +// } + +// impl ClientboundHelloPacket { +// pub fn get(self) -> LoginPacket { +// LoginPacket::ClientboundHelloPacket(self) +// } + +// pub fn write(&self, _buf: &mut Vec) -> Result<(), std::io::Error> { +// panic!("ClientboundHelloPacket::write not implemented") +// } + +// pub async fn read( +// buf: &mut T, +// ) -> Result { +// let server_id = buf.read_utf_with_len(20).await?; +// let public_key = buf.read_byte_array().await?; +// let nonce = buf.read_byte_array().await?; + +// Ok(ClientboundHelloPacket { +// server_id, +// public_key, +// nonce, +// } +// .get()) +// } +// } -- cgit v1.2.3 From 751098b636c9aee54b9ca7a465fdaa769f10be4d Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 18 Apr 2022 22:38:53 -0500 Subject: start working on declare commands packet --- Cargo.lock | 1 + README.md | 6 +- azalea-client/src/connect.rs | 3 + azalea-protocol/Cargo.toml | 1 + azalea-protocol/src/mc_buf/read.rs | 1 - .../game/clientbound_declare_commands_packet.rs | 133 +++++++++++++++------ azalea-protocol/src/packets/game/mod.rs | 9 ++ 7 files changed, 113 insertions(+), 41 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/Cargo.lock b/Cargo.lock index 67259fef..8d94c26c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -117,6 +117,7 @@ dependencies = [ "async-recursion", "async-trait", "azalea-auth", + "azalea-brigadier", "azalea-chat", "azalea-core", "azalea-nbt", diff --git a/README.md b/README.md index 3ae4b307..ed6ed46f 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ I named this Azalea because it sounds like a cool word and this is a cool librar ## Goals -- Bypass most anticheats -- Only support the latest Minecraft version - Do everything a vanilla client can do -- Be fast - Be easy to use +- Bypass most/all anticheats +- Support the latest Minecraft version +- Be fast diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs index 7b1da525..bc45a121 100644 --- a/azalea-client/src/connect.rs +++ b/azalea-client/src/connect.rs @@ -73,6 +73,9 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> { GamePacket::ClientboundChangeDifficultyPacket(p) => { println!("Got difficulty packet {:?}", p); } + GamePacket::ClientboundDeclareCommandsPacket(p) => { + println!("Got declare commands packet {:?}", p); + } }, Err(e) => { println!("Error: {:?}", e); diff --git a/azalea-protocol/Cargo.toml b/azalea-protocol/Cargo.toml index ff3bd9d4..37df8697 100644 --- a/azalea-protocol/Cargo.toml +++ b/azalea-protocol/Cargo.toml @@ -10,6 +10,7 @@ async-compression = {version = "^0.3.8", features = ["tokio", "zlib"]} async-recursion = "^0.3.2" async-trait = "0.1.51" azalea-auth = {path = "../azalea-auth"} +azalea-brigadier = {path = "../azalea-brigadier"} azalea-chat = {path = "../azalea-chat"} azalea-core = {path = "../azalea-core"} azalea-nbt = {path = "../azalea-nbt"} diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index 0fa1d099..20b69238 100644 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -2,7 +2,6 @@ use async_trait::async_trait; use azalea_core::{ difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, }; -use num_traits::FromPrimitive; use tokio::io::{AsyncRead, AsyncReadExt}; use super::MAX_STRING_LENGTH; diff --git a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs index 1bcf0dd4..1403630d 100644 --- a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs @@ -1,37 +1,96 @@ -// use std::hash::Hash; - -// use crate::mc_buf::Readable; - -// use super::LoginPacket; - -// #[derive(Hash, Clone, Debug)] -// pub struct ClientboundDeclareCommandsPacket { -// pub root: RootCommandNode, -// pub public_key: Vec, -// pub nonce: Vec, -// } - -// impl ClientboundHelloPacket { -// pub fn get(self) -> LoginPacket { -// LoginPacket::ClientboundHelloPacket(self) -// } - -// pub fn write(&self, _buf: &mut Vec) -> Result<(), std::io::Error> { -// panic!("ClientboundHelloPacket::write not implemented") -// } - -// pub async fn read( -// buf: &mut T, -// ) -> Result { -// let server_id = buf.read_utf_with_len(20).await?; -// let public_key = buf.read_byte_array().await?; -// let nonce = buf.read_byte_array().await?; - -// Ok(ClientboundHelloPacket { -// server_id, -// public_key, -// nonce, -// } -// .get()) -// } -// } +use std::hash::Hash; + +use async_trait::async_trait; +use tokio::io::AsyncRead; + +use crate::mc_buf::{McBufReadable, Readable}; + +use super::GamePacket; + +#[derive(Hash, Clone, Debug)] +pub struct ClientboundDeclareCommandsPacket { + pub entries: Vec, + pub root_index: i32, +} + +impl ClientboundDeclareCommandsPacket { + pub fn get(self) -> GamePacket { + GamePacket::ClientboundDeclareCommandsPacket(self) + } + + pub fn write(&self, _buf: &mut Vec) -> Result<(), std::io::Error> { + panic!("ClientboundDeclareCommandsPacket::write not implemented") + } + + pub async fn read( + buf: &mut T, + ) -> Result { + let node_count = buf.read_varint().await?; + println!("node_count: {}", node_count); + let mut nodes = Vec::with_capacity(node_count as usize); + for _ in 0..node_count { + let node = BrigadierNodeStub::read_into(buf).await?; + nodes.push(node); + } + let root_index = buf.read_varint().await?; + Ok(GamePacket::ClientboundDeclareCommandsPacket( + ClientboundDeclareCommandsPacket { + entries: nodes, + root_index, + }, + )) + } +} + +#[derive(Hash, Debug, Clone)] +pub struct BrigadierNodeStub {} + +// azalea_brigadier::tree::CommandNode +#[async_trait] +impl McBufReadable for BrigadierNodeStub { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + let flags = u8::read_into(buf).await?; + + let node_type = flags & 0x03; + let is_executable = flags & 0x04 != 0; + let has_redirect = flags & 0x08 != 0; + let has_suggestions_type = flags & 0x10 != 0; + println!("flags: {}, node_type: {}, is_executable: {}, has_redirect: {}, has_suggestions_type: {}", flags, node_type, is_executable, has_redirect, has_suggestions_type); + + let children = buf.read_int_id_list().await?; + println!("children: {:?}", children); + let redirect_node = if has_redirect { + buf.read_varint().await? + } else { + 0 + }; + println!("redirect_node: {}", redirect_node); + + if node_type == 2 { + let name = buf.read_utf().await?; + println!("name: {}", name); + + let resource_location = if has_suggestions_type { + Some(buf.read_resource_location().await?) + } else { + None + }; + println!( + "node_type=2, flags={}, name={}, resource_location={:?}", + flags, name, resource_location + ); + return Ok(BrigadierNodeStub {}); + } + if node_type == 1 { + let name = buf.read_utf().await?; + println!("node_type=1, flags={}, name={}", flags, name); + return Ok(BrigadierNodeStub {}); + } + println!("node_type={}, flags={}", node_type, flags); + Ok(BrigadierNodeStub {}) + // return Err("Unknown node type".to_string()); + } +} diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 4efe72fb..1fd47132 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -1,5 +1,6 @@ pub mod clientbound_change_difficulty_packet; pub mod clientbound_custom_payload_packet; +pub mod clientbound_declare_commands_packet; pub mod clientbound_login_packet; pub mod clientbound_update_view_distance_packet; @@ -22,6 +23,9 @@ where ClientboundChangeDifficultyPacket( clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, ), + ClientboundDeclareCommandsPacket( + clientbound_declare_commands_packet::ClientboundDeclareCommandsPacket, + ), } #[async_trait] @@ -32,6 +36,7 @@ impl ProtocolPacket for GamePacket { GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18, GamePacket::ClientboundLoginPacket(_packet) => 0x26, GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a, + GamePacket::ClientboundDeclareCommandsPacket(_packet) => 0x12, } } @@ -41,6 +46,7 @@ impl ProtocolPacket for GamePacket { GamePacket::ClientboundCustomPayloadPacket(packet) => packet.write(buf), GamePacket::ClientboundLoginPacket(packet) => packet.write(buf), GamePacket::ClientboundUpdateViewDistancePacket(packet) => packet.write(buf), + GamePacket::ClientboundDeclareCommandsPacket(packet) => packet.write(buf), } } @@ -63,6 +69,9 @@ impl ProtocolPacket for GamePacket { 0x4a => clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket ::read(buf) .await?, + 0x12 => clientbound_declare_commands_packet::ClientboundDeclareCommandsPacket + ::read(buf) + .await?, // _ => return Err(format!("Unknown ServerToClient game packet id: {}", id)), _ => panic!("Unknown ServerToClient game packet id: {}", id), }, -- cgit v1.2.3 From f60426cd5a8a276adcb88988cef38d27f48de912 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 19 Apr 2022 00:48:13 -0500 Subject: start adding declare_state_packets --- azalea-protocol/packet-macros/src/lib.rs | 133 +++++++++++++++++++++++++++-- azalea-protocol/src/packets/game/mod.rs | 139 +++++++++++++++++-------------- 2 files changed, 200 insertions(+), 72 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 957515c4..0e6ad0e1 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -1,10 +1,14 @@ +use std::collections::{BTreeMap, HashMap}; + +use proc_macro::TokenStream; use quote::{quote, ToTokens}; -use syn::{self, parse_macro_input, DeriveInput, FieldsNamed}; +use syn::{ + self, braced, + parse::{Parse, ParseStream, Result}, + parse_macro_input, DeriveInput, Expr, FieldsNamed, Ident, LitInt, Token, Type, Visibility, +}; -fn as_packet_derive( - input: proc_macro::TokenStream, - state: proc_macro2::TokenStream, -) -> proc_macro::TokenStream { +fn as_packet_derive(input: TokenStream, state: proc_macro2::TokenStream) -> TokenStream { let DeriveInput { ident, data, .. } = parse_macro_input!(input); let fields = match data { @@ -99,21 +103,132 @@ fn as_packet_derive( } #[proc_macro_derive(GamePacket, attributes(varint))] -pub fn derive_game_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { +pub fn derive_game_packet(input: TokenStream) -> TokenStream { as_packet_derive(input, quote! {crate::packets::game::GamePacket}) } #[proc_macro_derive(HandshakePacket, attributes(varint))] -pub fn derive_handshake_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { +pub fn derive_handshake_packet(input: TokenStream) -> TokenStream { as_packet_derive(input, quote! {crate::packets::handshake::HandshakePacket}) } #[proc_macro_derive(LoginPacket, attributes(varint))] -pub fn derive_login_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { +pub fn derive_login_packet(input: TokenStream) -> TokenStream { as_packet_derive(input, quote! {crate::packets::login::LoginPacket}) } #[proc_macro_derive(StatusPacket, attributes(varint))] -pub fn derive_status_packet(input: proc_macro::TokenStream) -> proc_macro::TokenStream { +pub fn derive_status_packet(input: TokenStream) -> TokenStream { as_packet_derive(input, quote! {crate::packets::status::StatusPacket}) } + +#[derive(Debug)] +struct PacketIdPair { + id: u32, + module: Ident, + name: Ident, +} +#[derive(Debug)] +struct PacketIdMap { + packets: Vec, +} + +impl Parse for PacketIdMap { + fn parse(input: ParseStream) -> Result { + let mut packets = vec![]; + loop { + // 0x0e: clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, + // 0x0e + let packet_id: LitInt = match input.parse() { + Ok(i) => i, + Err(_) => break, + }; + let packet_id = packet_id.base10_parse::()?; + // : + input.parse::()?; + // clientbound_change_difficulty_packet + let module: Ident = input.parse()?; + // :: + input.parse::()?; + // ClientboundChangeDifficultyPacket + let name: Ident = input.parse()?; + input.parse::()?; + + packets.push(PacketIdPair { + id: packet_id, + module, + name, + }); + } + + Ok(PacketIdMap { packets }) + } +} + +#[derive(Debug)] +struct DeclareStatePackets { + name: Ident, + serverbound: PacketIdMap, + clientbound: PacketIdMap, +} + +impl Parse for DeclareStatePackets { + fn parse(input: ParseStream) -> Result { + let name = input.parse()?; + input.parse::()?; + + let serverbound_token: Ident = input.parse()?; + if serverbound_token != "Serverbound" { + return Err(syn::Error::new( + serverbound_token.span(), + "Expected `Serverbound`", + )); + } + input.parse::]>()?; + let content; + braced!(content in input); + let serverbound = content.parse()?; + + input.parse::()?; + + let clientbound_token: Ident = input.parse()?; + if clientbound_token != "Clientbound" { + return Err(syn::Error::new( + clientbound_token.span(), + "Expected `Clientbound`", + )); + } + input.parse::]>()?; + let content; + braced!(content in input); + let clientbound = content.parse()?; + + Ok(DeclareStatePackets { + name, + serverbound, + clientbound, + }) + } +} +#[proc_macro] +pub fn declare_state_packets(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeclareStatePackets); + + let name = input.name; + + let mut enum_contents = quote!(); + for PacketIdPair { id, module, name } in input.serverbound.packets { + enum_contents.extend(quote! {#name(#module::#name)}); + } + + quote! { + #[derive(Clone, Debug)] + pub enum #name + where + Self: Sized, + { + #enum_contents + } + } + .into() +} diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 4efe72fb..cb07cf97 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -6,70 +6,83 @@ pub mod clientbound_update_view_distance_packet; use super::ProtocolPacket; use crate::connect::PacketFlow; use async_trait::async_trait; +use packet_macros::declare_state_packets; -#[derive(Clone, Debug)] -pub enum GamePacket -where - Self: Sized, -{ - ClientboundLoginPacket(clientbound_login_packet::ClientboundLoginPacket), - ClientboundUpdateViewDistancePacket( - clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, - ), - ClientboundCustomPayloadPacket( - clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, - ), - ClientboundChangeDifficultyPacket( - clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, - ), -} - -#[async_trait] -impl ProtocolPacket for GamePacket { - fn id(&self) -> u32 { - match self { - GamePacket::ClientboundChangeDifficultyPacket(_packet) => 0x0e, - GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18, - GamePacket::ClientboundLoginPacket(_packet) => 0x26, - GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a, - } +declare_state_packets!( + GamePacket, + // no serverbound packets implemented yet + Serverbound => {}, + Clientbound => { + 0x0e: clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, + 0x18: clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, + 0x26: clientbound_login_packet::ClientboundLoginPacket, + 0x4a: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, } +); - fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - match self { - GamePacket::ClientboundChangeDifficultyPacket(packet) => packet.write(buf), - GamePacket::ClientboundCustomPayloadPacket(packet) => packet.write(buf), - GamePacket::ClientboundLoginPacket(packet) => packet.write(buf), - GamePacket::ClientboundUpdateViewDistancePacket(packet) => packet.write(buf), - } - } +// #[derive(Clone, Debug)] +// pub enum GamePacket +// where +// Self: Sized, +// { +// ClientboundLoginPacket(clientbound_login_packet::ClientboundLoginPacket), +// ClientboundUpdateViewDistancePacket( +// clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, +// ), +// ClientboundCustomPayloadPacket( +// clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, +// ), +// ClientboundChangeDifficultyPacket( +// clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, +// ), +// } - /// Read a packet by its id, ConnectionProtocol, and flow - async fn read( - id: u32, - flow: &PacketFlow, - buf: &mut T, - ) -> Result - where - Self: Sized, - { - Ok(match flow { - PacketFlow::ServerToClient => match id { - 0x0e => clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket - ::read(buf) - .await?, - 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)), - _ => panic!("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)), - }, - }) - } -} +// #[async_trait] +// impl ProtocolPacket for GamePacket { +// fn id(&self) -> u32 { +// match self { +// GamePacket::ClientboundChangeDifficultyPacket(_packet) => 0x0e, +// GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18, +// GamePacket::ClientboundLoginPacket(_packet) => 0x26, +// GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a, +// } +// } + +// fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { +// match self { +// GamePacket::ClientboundChangeDifficultyPacket(packet) => packet.write(buf), +// GamePacket::ClientboundCustomPayloadPacket(packet) => packet.write(buf), +// GamePacket::ClientboundLoginPacket(packet) => packet.write(buf), +// GamePacket::ClientboundUpdateViewDistancePacket(packet) => packet.write(buf), +// } +// } + +// /// Read a packet by its id, ConnectionProtocol, and flow +// async fn read( +// id: u32, +// flow: &PacketFlow, +// buf: &mut T, +// ) -> Result +// where +// Self: Sized, +// { +// Ok(match flow { +// PacketFlow::ServerToClient => match id { +// 0x0e => clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket +// ::read(buf) +// .await?, +// 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)), +// _ => panic!("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)), +// }, +// }) +// } +// } -- cgit v1.2.3 From 9633874d23f3baa8e5d5c33f3fd51ae6aa880a88 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 19 Apr 2022 18:53:13 -0500 Subject: finish declare_state_packets implementation --- azalea-protocol/packet-macros/src/lib.rs | 35 ++++++++--------- azalea-protocol/src/packets/game/mod.rs | 67 -------------------------------- 2 files changed, 17 insertions(+), 85 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 8442b921..ab062550 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -1,11 +1,9 @@ -use std::collections::{BTreeMap, HashMap}; - use proc_macro::TokenStream; use quote::{quote, ToTokens}; use syn::{ self, braced, parse::{Parse, ParseStream, Result}, - parse_macro_input, DeriveInput, Expr, FieldsNamed, Ident, LitInt, Token, Type, Visibility, + parse_macro_input, DeriveInput, FieldsNamed, Ident, LitInt, Token, }; fn as_packet_derive(input: TokenStream, state: proc_macro2::TokenStream) -> TokenStream { @@ -215,10 +213,13 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeclareStatePackets); let state_name = input.name; + let state_name_litstr = syn::LitStr::new(&state_name.to_string(), state_name.span()); let mut enum_contents = quote!(); let mut id_match_contents = quote!(); let mut write_match_contents = quote!(); + let mut serverbound_read_match_contents = quote!(); + let mut clientbound_read_match_contents = quote!(); for PacketIdPair { id, module, name } in input.serverbound.packets { enum_contents.extend(quote! { #name(#module::#name) @@ -229,16 +230,22 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { write_match_contents.extend(quote! { #state_name::#name(packet) => packet.write(buf) }); + serverbound_read_match_contents.extend(quote! { + #id => #module::#name::read(buf).await?, + }); } for PacketIdPair { id, module, name } in input.clientbound.packets { enum_contents.extend(quote! { - #name(#module::#name) + #name(#module::#name), }); id_match_contents.extend(quote! { - #state_name::#name(_packet) => #id + #state_name::#name(_packet) => #id, }); write_match_contents.extend(quote! { - #state_name::#name(packet) => packet.write(buf) + #state_name::#name(packet) => packet.write(buf), + }); + clientbound_read_match_contents.extend(quote! { + #id => #module::#name::read(buf).await?, }); } @@ -276,20 +283,12 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { { Ok(match flow { PacketFlow::ServerToClient => match id { - 0x0e => clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket - ::read(buf) - .await?, - 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)), - _ => panic!("Unknown ServerToClient game packet id: {}", id), + #serverbound_read_match_contents + _ => panic!("Unknown ServerToClient {} packet id: {}", #state_name_litstr, id), }, PacketFlow::ClientToServer => match id { - // 0x00 => serverbound_hello_packet::ServerboundHelloPacket::read(buf).await?, - _ => return Err(format!("Unknown ClientToServer game packet id: {}", id)), + #clientbound_read_match_contents + _ => return Err(format!("Unknown ClientToServer {} packet id: {}", #state_name_litstr, id)), }, }) } diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index cb07cf97..2affd49e 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -19,70 +19,3 @@ declare_state_packets!( 0x4a: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, } ); - -// #[derive(Clone, Debug)] -// pub enum GamePacket -// where -// Self: Sized, -// { -// ClientboundLoginPacket(clientbound_login_packet::ClientboundLoginPacket), -// ClientboundUpdateViewDistancePacket( -// clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, -// ), -// ClientboundCustomPayloadPacket( -// clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, -// ), -// ClientboundChangeDifficultyPacket( -// clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, -// ), -// } - -// #[async_trait] -// impl ProtocolPacket for GamePacket { -// fn id(&self) -> u32 { -// match self { -// GamePacket::ClientboundChangeDifficultyPacket(_packet) => 0x0e, -// GamePacket::ClientboundCustomPayloadPacket(_packet) => 0x18, -// GamePacket::ClientboundLoginPacket(_packet) => 0x26, -// GamePacket::ClientboundUpdateViewDistancePacket(_packet) => 0x4a, -// } -// } - -// fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { -// match self { -// GamePacket::ClientboundChangeDifficultyPacket(packet) => packet.write(buf), -// GamePacket::ClientboundCustomPayloadPacket(packet) => packet.write(buf), -// GamePacket::ClientboundLoginPacket(packet) => packet.write(buf), -// GamePacket::ClientboundUpdateViewDistancePacket(packet) => packet.write(buf), -// } -// } - -// /// Read a packet by its id, ConnectionProtocol, and flow -// async fn read( -// id: u32, -// flow: &PacketFlow, -// buf: &mut T, -// ) -> Result -// where -// Self: Sized, -// { -// Ok(match flow { -// PacketFlow::ServerToClient => match id { -// 0x0e => clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket -// ::read(buf) -// .await?, -// 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)), -// _ => panic!("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)), -// }, -// }) -// } -// } -- cgit v1.2.3 From cbdab27cb555e38b39fe0f600ff945090ec10dcb Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 19 Apr 2022 19:17:36 -0500 Subject: add declare_state_packets to the other states --- azalea-protocol/packet-macros/src/lib.rs | 18 ++--- azalea-protocol/src/packets/game/mod.rs | 4 -- azalea-protocol/src/packets/handshake/mod.rs | 51 ++----------- azalea-protocol/src/packets/login/mod.rs | 83 ++++------------------ .../status/clientbound_status_response_packet.rs | 2 +- azalea-protocol/src/packets/status/mod.rs | 71 +++--------------- 6 files changed, 39 insertions(+), 190 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index ab062550..3cc3677a 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -222,13 +222,13 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { let mut clientbound_read_match_contents = quote!(); for PacketIdPair { id, module, name } in input.serverbound.packets { enum_contents.extend(quote! { - #name(#module::#name) + #name(#module::#name), }); id_match_contents.extend(quote! { - #state_name::#name(_packet) => #id + #state_name::#name(_packet) => #id, }); write_match_contents.extend(quote! { - #state_name::#name(packet) => packet.write(buf) + #state_name::#name(packet) => packet.write(buf), }); serverbound_read_match_contents.extend(quote! { #id => #module::#name::read(buf).await?, @@ -258,8 +258,8 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { #enum_contents } - #[async_trait] - impl ProtocolPacket for GamePacket { + #[async_trait::async_trait] + impl crate::packets::ProtocolPacket for #state_name { fn id(&self) -> u32 { match self { #id_match_contents @@ -275,18 +275,18 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream { /// Read a packet by its id, ConnectionProtocol, and flow async fn read( id: u32, - flow: &PacketFlow, + flow: &crate::connect::PacketFlow, buf: &mut T, - ) -> Result + ) -> Result<#state_name, String> where Self: Sized, { Ok(match flow { - PacketFlow::ServerToClient => match id { + crate::connect::PacketFlow::ServerToClient => match id { #serverbound_read_match_contents _ => panic!("Unknown ServerToClient {} packet id: {}", #state_name_litstr, id), }, - PacketFlow::ClientToServer => match id { + crate::connect::PacketFlow::ClientToServer => match id { #clientbound_read_match_contents _ => return Err(format!("Unknown ClientToServer {} packet id: {}", #state_name_litstr, id)), }, diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 2affd49e..a4304d9f 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -3,14 +3,10 @@ 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; -use async_trait::async_trait; use packet_macros::declare_state_packets; declare_state_packets!( GamePacket, - // no serverbound packets implemented yet Serverbound => {}, Clientbound => { 0x0e: clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, diff --git a/azalea-protocol/src/packets/handshake/mod.rs b/azalea-protocol/src/packets/handshake/mod.rs index 17465fca..88d9939b 100644 --- a/azalea-protocol/src/packets/handshake/mod.rs +++ b/azalea-protocol/src/packets/handshake/mod.rs @@ -1,48 +1,11 @@ pub mod client_intention_packet; -use async_trait::async_trait; +use packet_macros::declare_state_packets; -use crate::connect::PacketFlow; - -use super::ProtocolPacket; - -#[derive(Clone, Debug)] -pub enum HandshakePacket -where - Self: Sized, -{ - ClientIntentionPacket(client_intention_packet::ClientIntentionPacket), -} - -#[async_trait] -impl ProtocolPacket for HandshakePacket { - fn id(&self) -> u32 { - match self { - HandshakePacket::ClientIntentionPacket(_packet) => 0x00, - } - } - - fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - match self { - HandshakePacket::ClientIntentionPacket(packet) => packet.write(buf), - } - } - - /// Read a packet by its id, ConnectionProtocol, and flow - async fn read( - id: u32, - flow: &PacketFlow, - buf: &mut T, - ) -> Result - where - Self: Sized, - { - match flow { - PacketFlow::ServerToClient => Err("HandshakePacket::read not implemented".to_string()), - PacketFlow::ClientToServer => match id { - 0x00 => Ok(client_intention_packet::ClientIntentionPacket::read(buf).await?), - _ => Err(format!("Unknown ClientToServer status packet id: {}", id)), - }, - } +declare_state_packets!( + HandshakePacket, + Serverbound => {}, + Clientbound => { + 0x00: client_intention_packet::ClientIntentionPacket, } -} +); diff --git a/azalea-protocol/src/packets/login/mod.rs b/azalea-protocol/src/packets/login/mod.rs index b1f61746..ef5f15c1 100644 --- a/azalea-protocol/src/packets/login/mod.rs +++ b/azalea-protocol/src/packets/login/mod.rs @@ -4,76 +4,17 @@ pub mod clientbound_hello_packet; pub mod clientbound_login_compression_packet; pub mod serverbound_hello_packet; -use super::ProtocolPacket; -use crate::connect::PacketFlow; -use async_trait::async_trait; +use packet_macros::declare_state_packets; -#[derive(Clone, Debug)] -pub enum LoginPacket -where - Self: Sized, -{ - ClientboundCustomQueryPacket(clientbound_custom_query_packet::ClientboundCustomQueryPacket), - ClientboundGameProfilePacket(clientbound_game_profile_packet::ClientboundGameProfilePacket), - ClientboundHelloPacket(clientbound_hello_packet::ClientboundHelloPacket), - ClientboundLoginCompressionPacket( - clientbound_login_compression_packet::ClientboundLoginCompressionPacket, - ), - ServerboundHelloPacket(serverbound_hello_packet::ServerboundHelloPacket), -} - -#[async_trait] -impl ProtocolPacket for LoginPacket { - fn id(&self) -> u32 { - match self { - LoginPacket::ClientboundCustomQueryPacket(_packet) => 0x04, - LoginPacket::ClientboundGameProfilePacket(_packet) => 0x02, - LoginPacket::ClientboundHelloPacket(_packet) => 0x01, - LoginPacket::ClientboundLoginCompressionPacket(_packet) => 0x03, - LoginPacket::ServerboundHelloPacket(_packet) => 0x00, - } - } - - fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - match self { - LoginPacket::ClientboundCustomQueryPacket(packet) => packet.write(buf), - LoginPacket::ClientboundGameProfilePacket(packet) => packet.write(buf), - LoginPacket::ClientboundHelloPacket(packet) => packet.write(buf), - LoginPacket::ClientboundLoginCompressionPacket(packet) => packet.write(buf), - LoginPacket::ServerboundHelloPacket(packet) => packet.write(buf), - } - } - - /// Read a packet by its id, ConnectionProtocol, and flow - async fn read( - id: u32, - flow: &PacketFlow, - buf: &mut T, - ) -> Result - where - Self: Sized, - { - Ok(match flow { - PacketFlow::ServerToClient => match id { - 0x01 => clientbound_hello_packet::ClientboundHelloPacket::read(buf).await?, - 0x02 => { - clientbound_game_profile_packet::ClientboundGameProfilePacket::read(buf).await? - } - 0x04 => { - clientbound_custom_query_packet::ClientboundCustomQueryPacket::read(buf).await? - } - 0x03 => { - clientbound_login_compression_packet::ClientboundLoginCompressionPacket::read( - buf, - ) - .await? - } - _ => 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 login packet id: {}", id)), - }, - }) +declare_state_packets!( + LoginPacket, + Serverbound => { + 0x00: serverbound_hello_packet::ServerboundHelloPacket, + }, + Clientbound => { + 0x00: clientbound_hello_packet::ClientboundHelloPacket, + 0x02: clientbound_game_profile_packet::ClientboundGameProfilePacket, + 0x03: clientbound_login_compression_packet::ClientboundLoginCompressionPacket, + 0x04: clientbound_custom_query_packet::ClientboundCustomQueryPacket, } -} +); diff --git a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs index 58f5b701..884cf408 100644 --- a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs +++ b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs @@ -36,7 +36,7 @@ pub struct ClientboundStatusResponsePacket { impl ClientboundStatusResponsePacket { pub fn get(self) -> StatusPacket { - StatusPacket::ClientboundStatusResponsePacket(Box::new(self)) + StatusPacket::ClientboundStatusResponsePacket(self) } pub fn write(&self, _buf: &mut Vec) -> Result<(), std::io::Error> { diff --git a/azalea-protocol/src/packets/status/mod.rs b/azalea-protocol/src/packets/status/mod.rs index 31fedfb9..56aa577e 100644 --- a/azalea-protocol/src/packets/status/mod.rs +++ b/azalea-protocol/src/packets/status/mod.rs @@ -1,65 +1,14 @@ pub mod clientbound_status_response_packet; pub mod serverbound_status_request_packet; -use async_trait::async_trait; - -use crate::connect::PacketFlow; - -use super::ProtocolPacket; - -#[derive(Clone, Debug)] -pub enum StatusPacket -where - Self: Sized, -{ - ServerboundStatusRequestPacket( - serverbound_status_request_packet::ServerboundStatusRequestPacket, - ), - ClientboundStatusResponsePacket( - Box, - ), -} - -#[async_trait] -impl ProtocolPacket for StatusPacket { - fn id(&self) -> u32 { - match self { - StatusPacket::ServerboundStatusRequestPacket(_packet) => 0x00, - StatusPacket::ClientboundStatusResponsePacket(_packet) => 0x00, - } - } - - fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - match self { - StatusPacket::ServerboundStatusRequestPacket(packet) => packet.write(buf), - StatusPacket::ClientboundStatusResponsePacket(packet) => packet.write(buf), - } - } - - /// Read a packet by its id, ConnectionProtocol, and flow - async fn read( - id: u32, - flow: &PacketFlow, - buf: &mut T, - ) -> Result - where - Self: Sized, - { - match flow { - PacketFlow::ServerToClient => match id { - 0x00 => Ok( - clientbound_status_response_packet::ClientboundStatusResponsePacket::read(buf) - .await?, - ), - _ => Err(format!("Unknown ServerToClient status packet id: {}", id)), - }, - PacketFlow::ClientToServer => match id { - 0x00 => Ok( - serverbound_status_request_packet::ServerboundStatusRequestPacket::read(buf) - .await?, - ), - _ => Err(format!("Unknown ClientToServer status packet id: {}", id)), - }, - } +use packet_macros::declare_state_packets; + +declare_state_packets!( + StatusPacket, + Serverbound => { + 0x00: serverbound_status_request_packet::ServerboundStatusRequestPacket, + }, + Clientbound => { + 0x00: clientbound_status_response_packet::ClientboundStatusResponsePacket, } -} +); -- cgit v1.2.3 From a3fad4765b7ef2077fde0b5c87a5cf657f9f6584 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 19 Apr 2022 21:03:10 -0500 Subject: reorder some packets --- azalea-protocol/src/packets/game/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 904b38c8..2dbf7f24 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -11,9 +11,9 @@ declare_state_packets!( Serverbound => {}, Clientbound => { 0x0e: clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, + 0x12: clientbound_declare_commands_packet::ClientboundDeclareCommandsPacket, 0x18: clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, 0x26: clientbound_login_packet::ClientboundLoginPacket, - 0x4a: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, - 0x12: clientbound_declare_commands_packet::ClientboundDeclareCommandsPacket + 0x4a: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket } ); -- cgit v1.2.3 From 8bd97c6c96f2c3c1ef4edfc1ff9d59f9af664972 Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 20 Apr 2022 16:28:31 +0000 Subject: add player abilities packet --- azalea-client/src/connect.rs | 4 ++++ azalea-protocol/src/mc_buf/read.rs | 19 +++++++++++++++++++ azalea-protocol/src/mc_buf/write.rs | 12 ++++++++++++ .../game/clientbound_player_abilities_packet.rs | 11 +++++++++++ azalea-protocol/src/packets/game/mod.rs | 2 ++ 5 files changed, 48 insertions(+) create mode 100644 azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs (limited to 'azalea-protocol/src/packets') diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs index bc45a121..f19c67c2 100644 --- a/azalea-client/src/connect.rs +++ b/azalea-client/src/connect.rs @@ -76,6 +76,10 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> { GamePacket::ClientboundDeclareCommandsPacket(p) => { println!("Got declare commands packet {:?}", p); } + + GamePacket::ClientboundPlayerAbilitiesPacket(p) => { + println!("Got player abilities packet {:?}", p); + } }, Err(e) => { println!("Error: {:?}", e); diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index 20b69238..e036643e 100644 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -24,6 +24,7 @@ pub trait Readable { async fn read_long(&mut self) -> Result; async fn read_resource_location(&mut self) -> Result; async fn read_short(&mut self) -> Result; + async fn read_float(&mut self) -> Result; } #[async_trait] @@ -189,6 +190,13 @@ where Err(_) => Err("Error reading short".to_string()), } } + + async fn read_float(&mut self) -> Result { + match AsyncReadExt::read_f32(self).await { + Ok(r) => Ok(r), + Err(_) => Err("Error reading float".to_string()), + } + } } #[async_trait] @@ -362,6 +370,17 @@ impl McBufReadable for i8 { } } +// f32 +#[async_trait] +impl McBufReadable for f32 { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_float().await + } +} + // GameType #[async_trait] impl McBufReadable for GameType { diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index 26c0ef35..07605b16 100644 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -41,6 +41,7 @@ pub trait Writable { &mut self, location: &ResourceLocation, ) -> Result<(), std::io::Error>; + fn write_float(&mut self, n: f32) -> Result<(), std::io::Error>; } #[async_trait] @@ -147,6 +148,10 @@ impl Writable for Vec { WriteBytesExt::write_i64::(self, n) } + fn write_float(&mut self, n: f32) -> Result<(), std::io::Error> { + WriteBytesExt::write_f32::(self, n) + } + fn write_resource_location( &mut self, location: &ResourceLocation, @@ -264,6 +269,13 @@ impl McBufWritable for i8 { } } +// i8 +impl McBufWritable for f32 { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_float(*self) + } +} + // GameType impl McBufWritable for GameType { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { diff --git a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs new file mode 100644 index 00000000..6136fff5 --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs @@ -0,0 +1,11 @@ +// i don't know the actual name of this packet, i couldn't find it in the source code + +use packet_macros::GamePacket; + +#[derive(Clone, Debug, GamePacket)] +pub struct ClientboundPlayerAbilitiesPacket { + pub flags: u8, + pub flying_speed: f32, + /// Used for the fov + pub walking_speed: f32, +} diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 2dbf7f24..38630a0e 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -2,6 +2,7 @@ pub mod clientbound_change_difficulty_packet; pub mod clientbound_custom_payload_packet; pub mod clientbound_declare_commands_packet; pub mod clientbound_login_packet; +pub mod clientbound_player_abilities_packet; pub mod clientbound_update_view_distance_packet; use packet_macros::declare_state_packets; @@ -14,6 +15,7 @@ declare_state_packets!( 0x12: clientbound_declare_commands_packet::ClientboundDeclareCommandsPacket, 0x18: clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, 0x26: clientbound_login_packet::ClientboundLoginPacket, + 0x32: clientbound_player_abilities_packet::ClientboundPlayerAbilitiesPacket, 0x4a: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket } ); -- cgit v1.2.3 From 298d30ad08d5efe8c94f1865909bafc220a8cfac Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 20 Apr 2022 18:03:42 +0000 Subject: make PlayerAbilitiesFlags its own thing --- .../game/clientbound_player_abilities_packet.rs | 50 +++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs index 6136fff5..402923c9 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs @@ -1,11 +1,59 @@ // i don't know the actual name of this packet, i couldn't find it in the source code +use crate::mc_buf::{McBufReadable, McBufWritable, Readable}; +use async_trait::async_trait; use packet_macros::GamePacket; +use tokio::io::AsyncRead; #[derive(Clone, Debug, GamePacket)] pub struct ClientboundPlayerAbilitiesPacket { - pub flags: u8, + pub flags: PlayerAbilitiesFlags, pub flying_speed: f32, /// Used for the fov pub walking_speed: f32, } + +#[derive(Clone, Debug)] +pub struct PlayerAbilitiesFlags { + pub invulnerable: bool, + pub flying: bool, + pub can_fly: bool, + pub instant_break: bool, +} + +// Difficulty +#[async_trait] +impl McBufReadable for PlayerAbilitiesFlags { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + let byte = buf.read_byte().await?; + Ok(PlayerAbilitiesFlags { + invulnerable: byte & 1 != 0, + flying: byte & 2 != 0, + can_fly: byte & 4 != 0, + instant_break: byte & 8 != 0, + }) + } +} + +// Difficulty +impl McBufWritable for PlayerAbilitiesFlags { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + let mut byte = 0; + if self.invulnerable { + byte = byte | 1; + } + if self.flying { + byte = byte | 2; + } + if self.can_fly { + byte = byte | 4; + } + if self.instant_break { + byte = byte | 8; + } + u8::write_into(&byte, buf) + } +} -- cgit v1.2.3 From 2c848ebaa59781462aba6da428c8dfcc51dbacf8 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 21 Apr 2022 15:25:46 +0000 Subject: Set carried item and update tags packets --- azalea-client/src/connect.rs | 7 ++++++- azalea-protocol/packet-macros/src/lib.rs | 7 +++---- azalea-protocol/src/mc_buf/write.rs | 4 ++-- .../game/clientbound_player_abilities_packet.rs | 2 -- azalea-protocol/src/packets/game/mod.rs | 6 +++++- .../status/serverbound_status_request_packet.rs | 21 ++------------------- bot/src/main.rs | 6 ++++-- 7 files changed, 22 insertions(+), 31 deletions(-) (limited to 'azalea-protocol/src/packets') diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs index f19c67c2..3f0a8560 100644 --- a/azalea-client/src/connect.rs +++ b/azalea-client/src/connect.rs @@ -76,10 +76,15 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> { GamePacket::ClientboundDeclareCommandsPacket(p) => { println!("Got declare commands packet {:?}", p); } - GamePacket::ClientboundPlayerAbilitiesPacket(p) => { println!("Got player abilities packet {:?}", p); } + GamePacket::ClientboundSetCarriedItemPacket(p) => { + println!("Got set carried item packet {:?}", p); + } + GamePacket::ClientboundUpdateTagsPacket(p) => { + println!("Got update tags packet {:?}", p); + } }, Err(e) => { println!("Error: {:?}", e); diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index bdb83871..45df7e81 100644 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -75,7 +75,7 @@ fn as_packet_derive(input: TokenStream, state: proc_macro2::TokenStream) -> Toke .collect::>(); let read_field_names = named.iter().map(|f| &f.ident).collect::>(); - let gen = quote! { + quote! { impl #ident { pub fn get(self) -> #state { #state::#ident(self) @@ -95,9 +95,8 @@ fn as_packet_derive(input: TokenStream, state: proc_macro2::TokenStream) -> Toke }.get()) } } - }; - - gen.into() + } + .into() } #[proc_macro_derive(GamePacket, attributes(varint))] diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index 07605b16..9330dccb 100644 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -14,7 +14,7 @@ pub trait Writable { F: FnOnce(&mut Self, &T) -> Result<(), std::io::Error> + Copy, T: Sized, Self: Sized; - fn write_int_id_list(&mut self, list: Vec) -> Result<(), std::io::Error>; + fn write_int_id_list(&mut self, list: &Vec) -> Result<(), std::io::Error>; fn write_map( &mut self, map: Vec<(KT, VT)>, @@ -58,7 +58,7 @@ impl Writable for Vec { Ok(()) } - fn write_int_id_list(&mut self, list: Vec) -> Result<(), std::io::Error> { + fn write_int_id_list(&mut self, list: &Vec) -> Result<(), std::io::Error> { self.write_list(&list, |buf, n| buf.write_varint(*n)) } diff --git a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs index 402923c9..f4f528cf 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs @@ -21,7 +21,6 @@ pub struct PlayerAbilitiesFlags { pub instant_break: bool, } -// Difficulty #[async_trait] impl McBufReadable for PlayerAbilitiesFlags { async fn read_into(buf: &mut R) -> Result @@ -38,7 +37,6 @@ impl McBufReadable for PlayerAbilitiesFlags { } } -// Difficulty impl McBufWritable for PlayerAbilitiesFlags { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { let mut byte = 0; diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index 38630a0e..e150606c 100644 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -3,6 +3,8 @@ pub mod clientbound_custom_payload_packet; pub mod clientbound_declare_commands_packet; pub mod clientbound_login_packet; pub mod clientbound_player_abilities_packet; +pub mod clientbound_set_carried_item_packet; +pub mod clientbound_update_tags_packet; pub mod clientbound_update_view_distance_packet; use packet_macros::declare_state_packets; @@ -16,6 +18,8 @@ declare_state_packets!( 0x18: clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, 0x26: clientbound_login_packet::ClientboundLoginPacket, 0x32: clientbound_player_abilities_packet::ClientboundPlayerAbilitiesPacket, - 0x4a: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket + 0x48: clientbound_set_carried_item_packet::ClientboundSetCarriedItemPacket, + 0x4a: clientbound_update_view_distance_packet::ClientboundUpdateViewDistancePacket, + 0x67: clientbound_update_tags_packet::ClientboundUpdateTagsPacket } ); diff --git a/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs b/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs index af98f7cb..e77687ec 100644 --- a/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs +++ b/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs @@ -1,22 +1,5 @@ +use packet_macros::StatusPacket; use std::hash::Hash; -use super::StatusPacket; - -#[derive(Hash, Clone, Debug)] +#[derive(Clone, Debug, StatusPacket)] pub struct ServerboundStatusRequestPacket {} - -impl ServerboundStatusRequestPacket { - pub fn get(self) -> StatusPacket { - StatusPacket::ServerboundStatusRequestPacket(self) - } - - pub fn write(&self, _buf: &mut Vec) -> Result<(), std::io::Error> { - panic!("ServerboundStatusRequestPacket::write not implemented") - } - - pub async fn read( - _buf: &mut T, - ) -> Result { - Err("ServerboundStatusRequestPacket::read not implemented".to_string()) - } -} diff --git a/bot/src/main.rs b/bot/src/main.rs index 1041e765..957b3cbc 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -1,4 +1,5 @@ use azalea_client::connect::join_server; +use azalea_client::ping::ping_server; #[tokio::main] async fn main() { @@ -6,7 +7,8 @@ async fn main() { let address = "95.111.249.143:10000"; // let address = "localhost:63482"; - let _response = join_server(&address.try_into().unwrap()).await.unwrap(); - // println!("{}", response.description.to_ansi(None)); + let response = ping_server(&address.try_into().unwrap()).await.unwrap(); + // let _response = join_server(&address.try_into().unwrap()).await.unwrap(); + println!("{}", response.description.to_ansi(None)); println!("connected"); } -- cgit v1.2.3 From 7cdd4171453ead8645b082f0cf5879f7d1a39660 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 22 Apr 2022 00:13:45 +0000 Subject: add files --- .../game/clientbound_set_carried_item_packet.rs | 7 ++ .../packets/game/clientbound_update_tags_packet.rs | 105 +++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 azalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs create mode 100644 azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs (limited to 'azalea-protocol/src/packets') diff --git a/azalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs new file mode 100644 index 00000000..4f0bf575 --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs @@ -0,0 +1,7 @@ +use packet_macros::GamePacket; + +/// Sent to change the player's slot selection. +#[derive(Clone, Debug, GamePacket)] +pub struct ClientboundSetCarriedItemPacket { + pub slot: u8, +} diff --git a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs new file mode 100644 index 00000000..a2f17370 --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs @@ -0,0 +1,105 @@ +use std::collections::HashMap; + +use async_trait::async_trait; +use azalea_core::resource_location::ResourceLocation; +use packet_macros::GamePacket; +use tokio::io::AsyncRead; + +use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; + +#[derive(Clone, Debug, GamePacket)] +pub struct ClientboundUpdateTagsPacket { + pub tags: HashMap>, +} + +#[derive(Clone, Debug)] +pub struct Tags { + pub name: ResourceLocation, + pub elements: Vec, +} + +#[async_trait] +impl McBufReadable for HashMap> { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + println!("reading tags!"); + let length = buf.read_varint().await? as usize; + println!("length: {}", length); + let mut data = HashMap::with_capacity(length); + for _ in 0..length { + let tag_type = buf.read_resource_location().await?; + println!("read tag type {}", tag_type); + let tags_count = buf.read_varint().await? as usize; + let mut tags_vec = Vec::with_capacity(tags_count); + for _ in 0..tags_count { + let tags = Tags::read_into(buf).await?; + tags_vec.push(tags); + } + println!("tags: {} {:?}", tag_type, tags_vec); + data.insert(tag_type, tags_vec); + } + Ok(data) + } +} + +impl McBufWritable for HashMap> { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_varint(self.len() as i32)?; + for (k, v) in self { + k.write_into(buf)?; + v.write_into(buf)?; + } + Ok(()) + } +} + +#[async_trait] +impl McBufReadable for Vec { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + let tags_count = buf.read_varint().await? as usize; + let mut tags_vec = Vec::with_capacity(tags_count); + for _ in 0..tags_count { + let tags = Tags::read_into(buf).await?; + tags_vec.push(tags); + } + Ok(tags_vec) + } +} + +impl McBufWritable for Vec { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_varint(self.len() as i32)?; + for tag in self { + tag.write_into(buf)?; + } + Ok(()) + } +} + +#[async_trait] +impl McBufReadable for Tags { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + println!("reading tags."); + let name = buf.read_resource_location().await?; + println!("tags name: {}", name); + let elements = buf.read_int_id_list().await?; + println!("elements: {:?}", elements); + Ok(Tags { name, elements }) + } +} + +impl McBufWritable for Tags { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + self.name.write_into(buf)?; + buf.write_int_id_list(&self.elements)?; + Ok(()) + } +} -- cgit v1.2.3 From 248f752748a0033db7f8242ee0ecd73ea8ce8ec9 Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 22 Apr 2022 04:33:58 +0000 Subject: simplify error handling --- .gitignore | 0 .gitpod.yml | 0 .vscode/settings.json | 0 Cargo.lock | 0 Cargo.toml | 0 README.md | 0 azalea-auth/Cargo.toml | 0 azalea-auth/src/game_profile.rs | 0 azalea-auth/src/lib.rs | 0 azalea-brigadier/Cargo.toml | 0 azalea-brigadier/README.md | 0 azalea-brigadier/src/arguments/argument_type.rs | 0 .../src/arguments/integer_argument_type.rs | 0 azalea-brigadier/src/arguments/mod.rs | 0 azalea-brigadier/src/builder/argument_builder.rs | 0 .../src/builder/literal_argument_builder.rs | 0 azalea-brigadier/src/builder/mod.rs | 0 .../src/builder/required_argument_builder.rs | 0 azalea-brigadier/src/command_dispatcher.rs | 0 azalea-brigadier/src/context/command_context.rs | 0 .../src/context/command_context_builder.rs | 0 azalea-brigadier/src/context/mod.rs | 0 azalea-brigadier/src/context/parsed_argument.rs | 0 .../src/context/parsed_command_node.rs | 0 azalea-brigadier/src/context/string_range.rs | 0 .../src/exceptions/builtin_exceptions.rs | 0 .../src/exceptions/command_syntax_exception.rs | 0 azalea-brigadier/src/exceptions/mod.rs | 0 azalea-brigadier/src/lib.rs | 0 azalea-brigadier/src/message.rs | 0 azalea-brigadier/src/modifier.rs | 0 azalea-brigadier/src/parse_results.rs | 0 azalea-brigadier/src/string_reader.rs | 0 azalea-brigadier/src/tree/mod.rs | 0 .../tests/arguments/bool_argument_type_test.rs | 0 .../tests/arguments/double_argument_type_test.rs | 0 .../tests/arguments/float_argument_type_test.rs | 0 .../tests/arguments/integer_argument_type_test.rs | 0 .../tests/arguments/long_argument_type_test.rs | 0 .../tests/arguments/string_argument_type_test.rs | 0 .../tests/builder/argument_builder_test.rs | 0 .../tests/builder/literal_argument_builder_test.rs | 0 .../builder/required_argument_builder_test.rs | 0 azalea-brigadier/tests/command_dispatcher_test.rs | 0 .../tests/command_dispatcher_usages_test.rs | 0 azalea-brigadier/tests/command_suggestions_test.rs | 0 .../tests/context/command_context_test.rs | 0 .../tests/context/parsed_argument_test.rs | 0 .../dynamic_command_syntax_exception_type_test.rs | 0 .../simple_command_syntax_exception_type_test.rs | 0 azalea-brigadier/tests/string_reader_test.rs | 0 .../tests/suggestion/suggestion_test.rs | 0 .../tests/suggestion/suggestions_builder_test.rs | 0 .../tests/suggestion/suggestions_test.rs | 0 .../tests/tree/abstract_command_node_test.rs | 0 .../tests/tree/argument_command_node_test.rs | 0 .../tests/tree/literal_command_node_test.rs | 0 .../tests/tree/root_command_node_test.rs | 0 azalea-chat/Cargo.toml | 0 azalea-chat/README.md | 0 azalea-chat/src/base_component.rs | 0 azalea-chat/src/component.rs | 0 azalea-chat/src/events.rs | 0 azalea-chat/src/lib.rs | 0 azalea-chat/src/style.rs | 0 azalea-chat/src/text_component.rs | 0 azalea-chat/src/translatable_component.rs | 0 azalea-chat/tests/integration_test.rs | 0 azalea-client/Cargo.toml | 0 azalea-client/README.md | 0 azalea-client/src/connect.rs | 0 azalea-client/src/crypt.rs | 0 azalea-client/src/lib.rs | 0 azalea-client/src/ping.rs | 0 azalea-core/Cargo.toml | 0 azalea-core/src/difficulty.rs | 0 azalea-core/src/game_type.rs | 0 azalea-core/src/lib.rs | 0 azalea-core/src/resource_location.rs | 0 azalea-core/src/serializable_uuid.rs | 0 azalea-nbt/Cargo.toml | 0 azalea-nbt/README.md | 0 azalea-nbt/benches/my_benchmark.rs | 0 azalea-nbt/src/decode.rs | 34 ++++++++++----------- azalea-nbt/src/encode.rs | 0 azalea-nbt/src/error.rs | 11 +++++++ azalea-nbt/src/lib.rs | 0 azalea-nbt/src/tag.rs | 0 azalea-nbt/tests/bigtest.nbt | Bin azalea-nbt/tests/complex_player.dat | Bin azalea-nbt/tests/hello_world.nbt | Bin azalea-nbt/tests/inttest.nbt | Bin azalea-nbt/tests/level.dat | Bin azalea-nbt/tests/simple_player.dat | Bin azalea-nbt/tests/stringtest.nbt | Bin azalea-nbt/tests/tests.rs | 0 azalea-protocol/Cargo.toml | 0 azalea-protocol/README.md | 0 azalea-protocol/packet-macros/Cargo.toml | 0 azalea-protocol/packet-macros/src/lib.rs | 0 azalea-protocol/src/connect.rs | 0 azalea-protocol/src/lib.rs | 0 azalea-protocol/src/mc_buf/mod.rs | 0 azalea-protocol/src/mc_buf/read.rs | 0 azalea-protocol/src/mc_buf/write.rs | 0 .../game/clientbound_change_difficulty_packet.rs | 0 .../game/clientbound_custom_payload_packet.rs | 0 .../game/clientbound_declare_commands_packet.rs | 0 .../src/packets/game/clientbound_login_packet.rs | 0 .../game/clientbound_player_abilities_packet.rs | 0 .../game/clientbound_set_carried_item_packet.rs | 0 .../packets/game/clientbound_update_tags_packet.rs | 0 .../clientbound_update_view_distance_packet.rs | 0 azalea-protocol/src/packets/game/mod.rs | 0 .../packets/handshake/client_intention_packet.rs | 0 azalea-protocol/src/packets/handshake/mod.rs | 0 .../login/clientbound_custom_query_packet.rs | 0 .../login/clientbound_game_profile_packet.rs | 0 .../src/packets/login/clientbound_hello_packet.rs | 0 .../login/clientbound_login_compression_packet.rs | 0 azalea-protocol/src/packets/login/mod.rs | 0 .../src/packets/login/serverbound_hello_packet.rs | 0 azalea-protocol/src/packets/mod.rs | 0 .../status/clientbound_status_response_packet.rs | 0 azalea-protocol/src/packets/status/mod.rs | 0 .../status/serverbound_status_request_packet.rs | 0 azalea-protocol/src/read.rs | 0 azalea-protocol/src/resolver.rs | 0 azalea-protocol/src/write.rs | 0 bot/Cargo.toml | 0 bot/src/main.rs | 0 131 files changed, 28 insertions(+), 17 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 .gitpod.yml mode change 100644 => 100755 .vscode/settings.json mode change 100644 => 100755 Cargo.lock mode change 100644 => 100755 Cargo.toml mode change 100644 => 100755 README.md mode change 100644 => 100755 azalea-auth/Cargo.toml mode change 100644 => 100755 azalea-auth/src/game_profile.rs mode change 100644 => 100755 azalea-auth/src/lib.rs mode change 100644 => 100755 azalea-brigadier/Cargo.toml mode change 100644 => 100755 azalea-brigadier/README.md mode change 100644 => 100755 azalea-brigadier/src/arguments/argument_type.rs mode change 100644 => 100755 azalea-brigadier/src/arguments/integer_argument_type.rs mode change 100644 => 100755 azalea-brigadier/src/arguments/mod.rs mode change 100644 => 100755 azalea-brigadier/src/builder/argument_builder.rs mode change 100644 => 100755 azalea-brigadier/src/builder/literal_argument_builder.rs mode change 100644 => 100755 azalea-brigadier/src/builder/mod.rs mode change 100644 => 100755 azalea-brigadier/src/builder/required_argument_builder.rs mode change 100644 => 100755 azalea-brigadier/src/command_dispatcher.rs mode change 100644 => 100755 azalea-brigadier/src/context/command_context.rs mode change 100644 => 100755 azalea-brigadier/src/context/command_context_builder.rs mode change 100644 => 100755 azalea-brigadier/src/context/mod.rs mode change 100644 => 100755 azalea-brigadier/src/context/parsed_argument.rs mode change 100644 => 100755 azalea-brigadier/src/context/parsed_command_node.rs mode change 100644 => 100755 azalea-brigadier/src/context/string_range.rs mode change 100644 => 100755 azalea-brigadier/src/exceptions/builtin_exceptions.rs mode change 100644 => 100755 azalea-brigadier/src/exceptions/command_syntax_exception.rs mode change 100644 => 100755 azalea-brigadier/src/exceptions/mod.rs mode change 100644 => 100755 azalea-brigadier/src/lib.rs mode change 100644 => 100755 azalea-brigadier/src/message.rs mode change 100644 => 100755 azalea-brigadier/src/modifier.rs mode change 100644 => 100755 azalea-brigadier/src/parse_results.rs mode change 100644 => 100755 azalea-brigadier/src/string_reader.rs mode change 100644 => 100755 azalea-brigadier/src/tree/mod.rs mode change 100644 => 100755 azalea-brigadier/tests/arguments/bool_argument_type_test.rs mode change 100644 => 100755 azalea-brigadier/tests/arguments/double_argument_type_test.rs mode change 100644 => 100755 azalea-brigadier/tests/arguments/float_argument_type_test.rs mode change 100644 => 100755 azalea-brigadier/tests/arguments/integer_argument_type_test.rs mode change 100644 => 100755 azalea-brigadier/tests/arguments/long_argument_type_test.rs mode change 100644 => 100755 azalea-brigadier/tests/arguments/string_argument_type_test.rs mode change 100644 => 100755 azalea-brigadier/tests/builder/argument_builder_test.rs mode change 100644 => 100755 azalea-brigadier/tests/builder/literal_argument_builder_test.rs mode change 100644 => 100755 azalea-brigadier/tests/builder/required_argument_builder_test.rs mode change 100644 => 100755 azalea-brigadier/tests/command_dispatcher_test.rs mode change 100644 => 100755 azalea-brigadier/tests/command_dispatcher_usages_test.rs mode change 100644 => 100755 azalea-brigadier/tests/command_suggestions_test.rs mode change 100644 => 100755 azalea-brigadier/tests/context/command_context_test.rs mode change 100644 => 100755 azalea-brigadier/tests/context/parsed_argument_test.rs mode change 100644 => 100755 azalea-brigadier/tests/exceptions/dynamic_command_syntax_exception_type_test.rs mode change 100644 => 100755 azalea-brigadier/tests/exceptions/simple_command_syntax_exception_type_test.rs mode change 100644 => 100755 azalea-brigadier/tests/string_reader_test.rs mode change 100644 => 100755 azalea-brigadier/tests/suggestion/suggestion_test.rs mode change 100644 => 100755 azalea-brigadier/tests/suggestion/suggestions_builder_test.rs mode change 100644 => 100755 azalea-brigadier/tests/suggestion/suggestions_test.rs mode change 100644 => 100755 azalea-brigadier/tests/tree/abstract_command_node_test.rs mode change 100644 => 100755 azalea-brigadier/tests/tree/argument_command_node_test.rs mode change 100644 => 100755 azalea-brigadier/tests/tree/literal_command_node_test.rs mode change 100644 => 100755 azalea-brigadier/tests/tree/root_command_node_test.rs mode change 100644 => 100755 azalea-chat/Cargo.toml mode change 100644 => 100755 azalea-chat/README.md mode change 100644 => 100755 azalea-chat/src/base_component.rs mode change 100644 => 100755 azalea-chat/src/component.rs mode change 100644 => 100755 azalea-chat/src/events.rs mode change 100644 => 100755 azalea-chat/src/lib.rs mode change 100644 => 100755 azalea-chat/src/style.rs mode change 100644 => 100755 azalea-chat/src/text_component.rs mode change 100644 => 100755 azalea-chat/src/translatable_component.rs mode change 100644 => 100755 azalea-chat/tests/integration_test.rs mode change 100644 => 100755 azalea-client/Cargo.toml mode change 100644 => 100755 azalea-client/README.md mode change 100644 => 100755 azalea-client/src/connect.rs mode change 100644 => 100755 azalea-client/src/crypt.rs mode change 100644 => 100755 azalea-client/src/lib.rs mode change 100644 => 100755 azalea-client/src/ping.rs mode change 100644 => 100755 azalea-core/Cargo.toml mode change 100644 => 100755 azalea-core/src/difficulty.rs mode change 100644 => 100755 azalea-core/src/game_type.rs mode change 100644 => 100755 azalea-core/src/lib.rs mode change 100644 => 100755 azalea-core/src/resource_location.rs mode change 100644 => 100755 azalea-core/src/serializable_uuid.rs mode change 100644 => 100755 azalea-nbt/Cargo.toml mode change 100644 => 100755 azalea-nbt/README.md mode change 100644 => 100755 azalea-nbt/benches/my_benchmark.rs mode change 100644 => 100755 azalea-nbt/src/decode.rs mode change 100644 => 100755 azalea-nbt/src/encode.rs mode change 100644 => 100755 azalea-nbt/src/error.rs mode change 100644 => 100755 azalea-nbt/src/lib.rs mode change 100644 => 100755 azalea-nbt/src/tag.rs mode change 100644 => 100755 azalea-nbt/tests/bigtest.nbt mode change 100644 => 100755 azalea-nbt/tests/complex_player.dat mode change 100644 => 100755 azalea-nbt/tests/hello_world.nbt mode change 100644 => 100755 azalea-nbt/tests/inttest.nbt mode change 100644 => 100755 azalea-nbt/tests/level.dat mode change 100644 => 100755 azalea-nbt/tests/simple_player.dat mode change 100644 => 100755 azalea-nbt/tests/stringtest.nbt mode change 100644 => 100755 azalea-nbt/tests/tests.rs mode change 100644 => 100755 azalea-protocol/Cargo.toml mode change 100644 => 100755 azalea-protocol/README.md mode change 100644 => 100755 azalea-protocol/packet-macros/Cargo.toml mode change 100644 => 100755 azalea-protocol/packet-macros/src/lib.rs mode change 100644 => 100755 azalea-protocol/src/connect.rs mode change 100644 => 100755 azalea-protocol/src/lib.rs mode change 100644 => 100755 azalea-protocol/src/mc_buf/mod.rs mode change 100644 => 100755 azalea-protocol/src/mc_buf/read.rs mode change 100644 => 100755 azalea-protocol/src/mc_buf/write.rs mode change 100644 => 100755 azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/game/clientbound_login_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/game/clientbound_update_view_distance_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/game/mod.rs mode change 100644 => 100755 azalea-protocol/src/packets/handshake/client_intention_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/handshake/mod.rs mode change 100644 => 100755 azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/login/clientbound_hello_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/login/mod.rs mode change 100644 => 100755 azalea-protocol/src/packets/login/serverbound_hello_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/mod.rs mode change 100644 => 100755 azalea-protocol/src/packets/status/clientbound_status_response_packet.rs mode change 100644 => 100755 azalea-protocol/src/packets/status/mod.rs mode change 100644 => 100755 azalea-protocol/src/packets/status/serverbound_status_request_packet.rs mode change 100644 => 100755 azalea-protocol/src/read.rs mode change 100644 => 100755 azalea-protocol/src/resolver.rs mode change 100644 => 100755 azalea-protocol/src/write.rs mode change 100644 => 100755 bot/Cargo.toml mode change 100644 => 100755 bot/src/main.rs (limited to 'azalea-protocol/src/packets') diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.gitpod.yml b/.gitpod.yml old mode 100644 new mode 100755 diff --git a/.vscode/settings.json b/.vscode/settings.json old mode 100644 new mode 100755 diff --git a/Cargo.lock b/Cargo.lock old mode 100644 new mode 100755 diff --git a/Cargo.toml b/Cargo.toml old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/azalea-auth/Cargo.toml b/azalea-auth/Cargo.toml old mode 100644 new mode 100755 diff --git a/azalea-auth/src/game_profile.rs b/azalea-auth/src/game_profile.rs old mode 100644 new mode 100755 diff --git a/azalea-auth/src/lib.rs b/azalea-auth/src/lib.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/Cargo.toml b/azalea-brigadier/Cargo.toml old mode 100644 new mode 100755 diff --git a/azalea-brigadier/README.md b/azalea-brigadier/README.md old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/arguments/argument_type.rs b/azalea-brigadier/src/arguments/argument_type.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/arguments/integer_argument_type.rs b/azalea-brigadier/src/arguments/integer_argument_type.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/arguments/mod.rs b/azalea-brigadier/src/arguments/mod.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/builder/literal_argument_builder.rs b/azalea-brigadier/src/builder/literal_argument_builder.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/builder/mod.rs b/azalea-brigadier/src/builder/mod.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/builder/required_argument_builder.rs b/azalea-brigadier/src/builder/required_argument_builder.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/context/command_context.rs b/azalea-brigadier/src/context/command_context.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/context/command_context_builder.rs b/azalea-brigadier/src/context/command_context_builder.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/context/mod.rs b/azalea-brigadier/src/context/mod.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/context/parsed_argument.rs b/azalea-brigadier/src/context/parsed_argument.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/context/parsed_command_node.rs b/azalea-brigadier/src/context/parsed_command_node.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/context/string_range.rs b/azalea-brigadier/src/context/string_range.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/exceptions/builtin_exceptions.rs b/azalea-brigadier/src/exceptions/builtin_exceptions.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/exceptions/command_syntax_exception.rs b/azalea-brigadier/src/exceptions/command_syntax_exception.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/exceptions/mod.rs b/azalea-brigadier/src/exceptions/mod.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/lib.rs b/azalea-brigadier/src/lib.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/message.rs b/azalea-brigadier/src/message.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/modifier.rs b/azalea-brigadier/src/modifier.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/parse_results.rs b/azalea-brigadier/src/parse_results.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/string_reader.rs b/azalea-brigadier/src/string_reader.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/src/tree/mod.rs b/azalea-brigadier/src/tree/mod.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/arguments/bool_argument_type_test.rs b/azalea-brigadier/tests/arguments/bool_argument_type_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/arguments/double_argument_type_test.rs b/azalea-brigadier/tests/arguments/double_argument_type_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/arguments/float_argument_type_test.rs b/azalea-brigadier/tests/arguments/float_argument_type_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/arguments/integer_argument_type_test.rs b/azalea-brigadier/tests/arguments/integer_argument_type_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/arguments/long_argument_type_test.rs b/azalea-brigadier/tests/arguments/long_argument_type_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/arguments/string_argument_type_test.rs b/azalea-brigadier/tests/arguments/string_argument_type_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/builder/argument_builder_test.rs b/azalea-brigadier/tests/builder/argument_builder_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/builder/literal_argument_builder_test.rs b/azalea-brigadier/tests/builder/literal_argument_builder_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/builder/required_argument_builder_test.rs b/azalea-brigadier/tests/builder/required_argument_builder_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/command_dispatcher_test.rs b/azalea-brigadier/tests/command_dispatcher_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/command_dispatcher_usages_test.rs b/azalea-brigadier/tests/command_dispatcher_usages_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/command_suggestions_test.rs b/azalea-brigadier/tests/command_suggestions_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/context/command_context_test.rs b/azalea-brigadier/tests/context/command_context_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/context/parsed_argument_test.rs b/azalea-brigadier/tests/context/parsed_argument_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/exceptions/dynamic_command_syntax_exception_type_test.rs b/azalea-brigadier/tests/exceptions/dynamic_command_syntax_exception_type_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/exceptions/simple_command_syntax_exception_type_test.rs b/azalea-brigadier/tests/exceptions/simple_command_syntax_exception_type_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/string_reader_test.rs b/azalea-brigadier/tests/string_reader_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/suggestion/suggestion_test.rs b/azalea-brigadier/tests/suggestion/suggestion_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/suggestion/suggestions_builder_test.rs b/azalea-brigadier/tests/suggestion/suggestions_builder_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/suggestion/suggestions_test.rs b/azalea-brigadier/tests/suggestion/suggestions_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/tree/abstract_command_node_test.rs b/azalea-brigadier/tests/tree/abstract_command_node_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/tree/argument_command_node_test.rs b/azalea-brigadier/tests/tree/argument_command_node_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/tree/literal_command_node_test.rs b/azalea-brigadier/tests/tree/literal_command_node_test.rs old mode 100644 new mode 100755 diff --git a/azalea-brigadier/tests/tree/root_command_node_test.rs b/azalea-brigadier/tests/tree/root_command_node_test.rs old mode 100644 new mode 100755 diff --git a/azalea-chat/Cargo.toml b/azalea-chat/Cargo.toml old mode 100644 new mode 100755 diff --git a/azalea-chat/README.md b/azalea-chat/README.md old mode 100644 new mode 100755 diff --git a/azalea-chat/src/base_component.rs b/azalea-chat/src/base_component.rs old mode 100644 new mode 100755 diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs old mode 100644 new mode 100755 diff --git a/azalea-chat/src/events.rs b/azalea-chat/src/events.rs old mode 100644 new mode 100755 diff --git a/azalea-chat/src/lib.rs b/azalea-chat/src/lib.rs old mode 100644 new mode 100755 diff --git a/azalea-chat/src/style.rs b/azalea-chat/src/style.rs old mode 100644 new mode 100755 diff --git a/azalea-chat/src/text_component.rs b/azalea-chat/src/text_component.rs old mode 100644 new mode 100755 diff --git a/azalea-chat/src/translatable_component.rs b/azalea-chat/src/translatable_component.rs old mode 100644 new mode 100755 diff --git a/azalea-chat/tests/integration_test.rs b/azalea-chat/tests/integration_test.rs old mode 100644 new mode 100755 diff --git a/azalea-client/Cargo.toml b/azalea-client/Cargo.toml old mode 100644 new mode 100755 diff --git a/azalea-client/README.md b/azalea-client/README.md old mode 100644 new mode 100755 diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs old mode 100644 new mode 100755 diff --git a/azalea-client/src/crypt.rs b/azalea-client/src/crypt.rs old mode 100644 new mode 100755 diff --git a/azalea-client/src/lib.rs b/azalea-client/src/lib.rs old mode 100644 new mode 100755 diff --git a/azalea-client/src/ping.rs b/azalea-client/src/ping.rs old mode 100644 new mode 100755 diff --git a/azalea-core/Cargo.toml b/azalea-core/Cargo.toml old mode 100644 new mode 100755 diff --git a/azalea-core/src/difficulty.rs b/azalea-core/src/difficulty.rs old mode 100644 new mode 100755 diff --git a/azalea-core/src/game_type.rs b/azalea-core/src/game_type.rs old mode 100644 new mode 100755 diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs old mode 100644 new mode 100755 diff --git a/azalea-core/src/resource_location.rs b/azalea-core/src/resource_location.rs old mode 100644 new mode 100755 diff --git a/azalea-core/src/serializable_uuid.rs b/azalea-core/src/serializable_uuid.rs old mode 100644 new mode 100755 diff --git a/azalea-nbt/Cargo.toml b/azalea-nbt/Cargo.toml old mode 100644 new mode 100755 diff --git a/azalea-nbt/README.md b/azalea-nbt/README.md old mode 100644 new mode 100755 diff --git a/azalea-nbt/benches/my_benchmark.rs b/azalea-nbt/benches/my_benchmark.rs old mode 100644 new mode 100755 diff --git a/azalea-nbt/src/decode.rs b/azalea-nbt/src/decode.rs old mode 100644 new mode 100755 index 41689a46..e4968811 --- a/azalea-nbt/src/decode.rs +++ b/azalea-nbt/src/decode.rs @@ -11,13 +11,13 @@ async fn read_string(stream: &mut R) -> Result where R: AsyncRead + std::marker::Unpin, { - let length = stream.read_u16().await.map_err(|_| Error::InvalidTag)?; + let length = stream.read_u16().await?; let mut buf = Vec::with_capacity(length as usize); for _ in 0..length { - buf.push(stream.read_u8().await.map_err(|_| Error::InvalidTag)?); + buf.push(stream.read_u8().await?); } - String::from_utf8(buf).map_err(|_| Error::InvalidTag) + Ok(String::from_utf8(buf)?) } impl Tag { @@ -31,26 +31,26 @@ impl Tag { // a TAG_Compound, and is not named despite being in a TAG_Compound 0 => Tag::End, // A single signed byte - 1 => Tag::Byte(stream.read_i8().await.map_err(|_| Error::InvalidTag)?), + 1 => Tag::Byte(stream.read_i8().await?), // A single signed, big endian 16 bit integer - 2 => Tag::Short(stream.read_i16().await.map_err(|_| Error::InvalidTag)?), + 2 => Tag::Short(stream.read_i16().await?), // A single signed, big endian 32 bit integer - 3 => Tag::Int(stream.read_i32().await.map_err(|_| Error::InvalidTag)?), + 3 => Tag::Int(stream.read_i32().await?), // A single signed, big endian 64 bit integer - 4 => Tag::Long(stream.read_i64().await.map_err(|_| Error::InvalidTag)?), + 4 => Tag::Long(stream.read_i64().await?), // A single, big endian IEEE-754 single-precision floating point // number (NaN possible) - 5 => Tag::Float(stream.read_f32().await.map_err(|_| Error::InvalidTag)?), + 5 => Tag::Float(stream.read_f32().await?), // A single, big endian IEEE-754 double-precision floating point // number (NaN possible) - 6 => Tag::Double(stream.read_f64().await.map_err(|_| Error::InvalidTag)?), + 6 => Tag::Double(stream.read_f64().await?), // A length-prefixed array of signed bytes. The prefix is a signed // integer (thus 4 bytes) 7 => { - let length = stream.read_i32().await.map_err(|_| Error::InvalidTag)?; + let length = stream.read_i32().await?; let mut bytes = Vec::with_capacity(length as usize); for _ in 0..length { - bytes.push(stream.read_i8().await.map_err(|_| Error::InvalidTag)?); + bytes.push(stream.read_i8().await?); } Tag::ByteArray(bytes) } @@ -67,8 +67,8 @@ impl Tag { // another reference implementation by Mojang uses 1 instead; // parsers should accept any type if the length is <= 0). 9 => { - let type_id = stream.read_u8().await.map_err(|_| Error::InvalidTag)?; - let length = stream.read_i32().await.map_err(|_| Error::InvalidTag)?; + let type_id = stream.read_u8().await?; + let length = stream.read_i32().await?; let mut list = Vec::with_capacity(length as usize); for _ in 0..length { list.push(Tag::read_known(stream, type_id).await?); @@ -94,20 +94,20 @@ impl Tag { // signed integer (thus 4 bytes) and indicates the number of 4 byte // integers. 11 => { - let length = stream.read_i32().await.map_err(|_| Error::InvalidTag)?; + let length = stream.read_i32().await?; let mut ints = Vec::with_capacity(length as usize); for _ in 0..length { - ints.push(stream.read_i32().await.map_err(|_| Error::InvalidTag)?); + ints.push(stream.read_i32().await?); } Tag::IntArray(ints) } // A length-prefixed array of signed longs. The prefix is a signed // integer (thus 4 bytes) and indicates the number of 8 byte longs. 12 => { - let length = stream.read_i32().await.map_err(|_| Error::InvalidTag)?; + let length = stream.read_i32().await?; let mut longs = Vec::with_capacity(length as usize); for _ in 0..length { - longs.push(stream.read_i64().await.map_err(|_| Error::InvalidTag)?); + longs.push(stream.read_i64().await?); } Tag::LongArray(longs) } diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs old mode 100644 new mode 100755 diff --git a/azalea-nbt/src/error.rs b/azalea-nbt/src/error.rs old mode 100644 new mode 100755 index 05ff15e0..278d2770 --- a/azalea-nbt/src/error.rs +++ b/azalea-nbt/src/error.rs @@ -14,3 +14,14 @@ impl std::fmt::Display for Error { } } } + +impl From for Error { + fn from(err: std::io::Error) -> Self { + Error::WriteError + } +} +impl From for Error { + fn from(err: std::string::FromUtf8Error) -> Self { + Error::WriteError + } +} \ No newline at end of file diff --git a/azalea-nbt/src/lib.rs b/azalea-nbt/src/lib.rs old mode 100644 new mode 100755 diff --git a/azalea-nbt/src/tag.rs b/azalea-nbt/src/tag.rs old mode 100644 new mode 100755 diff --git a/azalea-nbt/tests/bigtest.nbt b/azalea-nbt/tests/bigtest.nbt old mode 100644 new mode 100755 diff --git a/azalea-nbt/tests/complex_player.dat b/azalea-nbt/tests/complex_player.dat old mode 100644 new mode 100755 diff --git a/azalea-nbt/tests/hello_world.nbt b/azalea-nbt/tests/hello_world.nbt old mode 100644 new mode 100755 diff --git a/azalea-nbt/tests/inttest.nbt b/azalea-nbt/tests/inttest.nbt old mode 100644 new mode 100755 diff --git a/azalea-nbt/tests/level.dat b/azalea-nbt/tests/level.dat old mode 100644 new mode 100755 diff --git a/azalea-nbt/tests/simple_player.dat b/azalea-nbt/tests/simple_player.dat old mode 100644 new mode 100755 diff --git a/azalea-nbt/tests/stringtest.nbt b/azalea-nbt/tests/stringtest.nbt old mode 100644 new mode 100755 diff --git a/azalea-nbt/tests/tests.rs b/azalea-nbt/tests/tests.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/Cargo.toml b/azalea-protocol/Cargo.toml old mode 100644 new mode 100755 diff --git a/azalea-protocol/README.md b/azalea-protocol/README.md old mode 100644 new mode 100755 diff --git a/azalea-protocol/packet-macros/Cargo.toml b/azalea-protocol/packet-macros/Cargo.toml old mode 100644 new mode 100755 diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/lib.rs b/azalea-protocol/src/lib.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/mc_buf/mod.rs b/azalea-protocol/src/mc_buf/mod.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs b/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/game/clientbound_login_packet.rs b/azalea-protocol/src/packets/game/clientbound_login_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs old mode 100644 new mode 100755 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 old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/handshake/client_intention_packet.rs b/azalea-protocol/src/packets/handshake/client_intention_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/handshake/mod.rs b/azalea-protocol/src/packets/handshake/mod.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs b/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/login/clientbound_hello_packet.rs b/azalea-protocol/src/packets/login/clientbound_hello_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/login/mod.rs b/azalea-protocol/src/packets/login/mod.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/login/serverbound_hello_packet.rs b/azalea-protocol/src/packets/login/serverbound_hello_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/mod.rs b/azalea-protocol/src/packets/mod.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/status/mod.rs b/azalea-protocol/src/packets/status/mod.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs b/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/read.rs b/azalea-protocol/src/read.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/resolver.rs b/azalea-protocol/src/resolver.rs old mode 100644 new mode 100755 diff --git a/azalea-protocol/src/write.rs b/azalea-protocol/src/write.rs old mode 100644 new mode 100755 diff --git a/bot/Cargo.toml b/bot/Cargo.toml old mode 100644 new mode 100755 diff --git a/bot/src/main.rs b/bot/src/main.rs old mode 100644 new mode 100755 -- cgit v1.2.3 From b7641ff308aab7840d2a2253ae50f8ee496b2a97 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 24 Apr 2022 16:18:51 -0500 Subject: 1.18.2 support --- azalea-client/src/connect.rs | 15 +++- azalea-protocol/src/mc_buf/mod.rs | 84 +++++++++++++++++++--- azalea-protocol/src/mc_buf/read.rs | 21 +++++- azalea-protocol/src/mc_buf/write.rs | 33 ++++++++- .../packets/game/clientbound_disconnect_packet.rs | 9 +++ azalea-protocol/src/packets/game/mod.rs | 2 + .../packets/handshake/client_intention_packet.rs | 24 ++++++- .../login/clientbound_login_disconnect_packet.rs | 7 ++ azalea-protocol/src/packets/login/mod.rs | 5 +- azalea-protocol/src/packets/mod.rs | 6 +- bot/src/main.rs | 4 +- 11 files changed, 188 insertions(+), 22 deletions(-) create mode 100644 azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs create mode 100644 azalea-protocol/src/packets/login/clientbound_login_disconnect_packet.rs (limited to 'azalea-protocol/src/packets') diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs index 7e6dba51..6e58bee6 100755 --- a/azalea-client/src/connect.rs +++ b/azalea-client/src/connect.rs @@ -48,10 +48,18 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> { println!("Got profile {:?}", p.game_profile); break conn.game(); } - _ => panic!("unhandled packet"), + LoginPacket::ServerboundHelloPacket(p) => { + println!("Got hello {:?}", p); + } + LoginPacket::ClientboundLoginDisconnectPacket(p) => { + println!("Got disconnect {:?}", p); + } + LoginPacket::ClientboundCustomQueryPacket(p) => { + println!("Got custom query {:?}", p); + } }, Err(e) => { - println!("Error: {:?}", e); + panic!("Error: {:?}", e); } } }; @@ -85,6 +93,9 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> { GamePacket::ClientboundUpdateTagsPacket(p) => { println!("Got update tags packet {:?}", p); } + GamePacket::ClientboundDisconnectPacket(p) => { + println!("Got login disconnect packet {:?}", p); + } }, Err(e) => { panic!("Error: {:?}", e); diff --git a/azalea-protocol/src/mc_buf/mod.rs b/azalea-protocol/src/mc_buf/mod.rs index a924431e..4ecb65d1 100755 --- a/azalea-protocol/src/mc_buf/mod.rs +++ b/azalea-protocol/src/mc_buf/mod.rs @@ -19,21 +19,53 @@ mod tests { #[test] fn test_write_varint() { - let mut buf = Vec::new(); - buf.write_varint(123456).unwrap(); - assert_eq!(buf, vec![192, 196, 7]); - let mut buf = Vec::new(); buf.write_varint(0).unwrap(); assert_eq!(buf, vec![0]); + + let mut buf = Vec::new(); + buf.write_varint(1).unwrap(); + assert_eq!(buf, vec![1]); + + let mut buf = Vec::new(); + buf.write_varint(2).unwrap(); + assert_eq!(buf, vec![2]); + + let mut buf = Vec::new(); + buf.write_varint(127).unwrap(); + assert_eq!(buf, vec![127]); + + let mut buf = Vec::new(); + buf.write_varint(128).unwrap(); + assert_eq!(buf, vec![128, 1]); + + let mut buf = Vec::new(); + buf.write_varint(255).unwrap(); + assert_eq!(buf, vec![255, 1]); + + let mut buf = Vec::new(); + buf.write_varint(25565).unwrap(); + assert_eq!(buf, vec![221, 199, 1]); + + let mut buf = Vec::new(); + buf.write_varint(2097151).unwrap(); + assert_eq!(buf, vec![255, 255, 127]); + + let mut buf = Vec::new(); + buf.write_varint(2147483647).unwrap(); + assert_eq!(buf, vec![255, 255, 255, 255, 7]); + + let mut buf = Vec::new(); + buf.write_varint(-1).unwrap(); + assert_eq!(buf, vec![255, 255, 255, 255, 15]); + + let mut buf = Vec::new(); + buf.write_varint(-2147483648).unwrap(); + assert_eq!(buf, vec![128, 128, 128, 128, 8]); } #[tokio::test] async fn test_read_varint() { - let mut buf = BufReader::new(Cursor::new(vec![192, 196, 7])); - assert_eq!(buf.read_varint().await.unwrap(), 123456); - assert_eq!(buf.get_varint_size(123456), 3); - let mut buf = BufReader::new(Cursor::new(vec![0])); assert_eq!(buf.read_varint().await.unwrap(), 0); assert_eq!(buf.get_varint_size(0), 1); @@ -41,6 +73,42 @@ mod tests { let mut buf = BufReader::new(Cursor::new(vec![1])); assert_eq!(buf.read_varint().await.unwrap(), 1); assert_eq!(buf.get_varint_size(1), 1); + + let mut buf = BufReader::new(Cursor::new(vec![2])); + assert_eq!(buf.read_varint().await.unwrap(), 2); + assert_eq!(buf.get_varint_size(2), 1); + + let mut buf = BufReader::new(Cursor::new(vec![127])); + assert_eq!(buf.read_varint().await.unwrap(), 127); + assert_eq!(buf.get_varint_size(127), 1); + + let mut buf = BufReader::new(Cursor::new(vec![128, 1])); + assert_eq!(buf.read_varint().await.unwrap(), 128); + assert_eq!(buf.get_varint_size(128), 2); + + let mut buf = BufReader::new(Cursor::new(vec![255, 1])); + assert_eq!(buf.read_varint().await.unwrap(), 255); + assert_eq!(buf.get_varint_size(255), 2); + + let mut buf = BufReader::new(Cursor::new(vec![221, 199, 1])); + assert_eq!(buf.read_varint().await.unwrap(), 25565); + assert_eq!(buf.get_varint_size(25565), 3); + + let mut buf = BufReader::new(Cursor::new(vec![255, 255, 127])); + assert_eq!(buf.read_varint().await.unwrap(), 2097151); + assert_eq!(buf.get_varint_size(2097151), 3); + + let mut buf = BufReader::new(Cursor::new(vec![255, 255, 255, 255, 7])); + assert_eq!(buf.read_varint().await.unwrap(), 2147483647); + assert_eq!(buf.get_varint_size(2147483647), 5); + + let mut buf = BufReader::new(Cursor::new(vec![255, 255, 255, 255, 15])); + assert_eq!(buf.read_varint().await.unwrap(), -1); + assert_eq!(buf.get_varint_size(-1), 5); + + let mut buf = BufReader::new(Cursor::new(vec![128, 128, 128, 128, 8])); + assert_eq!(buf.read_varint().await.unwrap(), -2147483648); + assert_eq!(buf.get_varint_size(-2147483648), 5); } #[tokio::test] diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index e036643e..1e031916 100755 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -1,7 +1,9 @@ use async_trait::async_trait; +use azalea_chat::component::Component; use azalea_core::{ difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, }; +use serde::Deserialize; use tokio::io::{AsyncRead, AsyncReadExt}; use super::MAX_STRING_LENGTH; @@ -41,12 +43,12 @@ where Ok(list) } - // fast varints stolen from https://github.com/luojia65/mc-varint/blob/master/src/lib.rs#L67 + // fast varints modified from https://github.com/luojia65/mc-varint/blob/master/src/lib.rs#L67 /// Read a single varint from the reader and return the value, along with the number of bytes read async fn read_varint(&mut self) -> Result { let mut buffer = [0]; let mut ans = 0; - for i in 0..4 { + for i in 0..5 { self.read_exact(&mut buffer) .await .map_err(|_| "Invalid VarInt".to_string())?; @@ -440,3 +442,18 @@ impl McBufReadable for Difficulty { Ok(Difficulty::by_id(u8::read_into(buf).await?)) } } + +// Component +#[async_trait] +impl McBufReadable for Component { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + let string = buf.read_utf().await?; + let json: serde_json::Value = serde_json::from_str(string.as_str()) + .map_err(|e| "Component isn't valid JSON".to_string())?; + let component = Component::deserialize(json).map_err(|e| e.to_string())?; + Ok(component) + } +} diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index 9330dccb..05f613d8 100755 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -1,4 +1,5 @@ use async_trait::async_trait; +use azalea_chat::component::Component; use azalea_core::{ difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation, }; @@ -209,7 +210,7 @@ impl McBufWritable for ResourceLocation { // u32 impl McBufWritable for u32 { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - i32::varint_write_into(&(*self as i32), buf) + i16::write_into(&(*self as i16), buf) } } @@ -223,7 +224,7 @@ impl McBufVarintWritable for u32 { // u16 impl McBufWritable for u16 { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - i32::varint_write_into(&(*self as i32), buf) + i16::write_into(&(*self as i16), buf) } } @@ -241,6 +242,13 @@ impl McBufWritable for u8 { } } +// i16 +impl McBufWritable for i16 { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + Writable::write_short(buf, *self) + } +} + // i64 impl McBufWritable for i64 { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { @@ -269,7 +277,7 @@ impl McBufWritable for i8 { } } -// i8 +// f32 impl McBufWritable for f32 { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { buf.write_float(*self) @@ -312,3 +320,22 @@ impl McBufWritable for Difficulty { u8::write_into(&self.id(), buf) } } + +// Component +#[async_trait] +impl McBufWritable for Component { + // async fn read_into(buf: &mut R) -> Result + // where + // R: AsyncRead + std::marker::Unpin + std::marker::Send, + // { + // let string = buf.read_utf().await?; + // let json: serde_json::Value = serde_json::from_str(string.as_str()) + // .map_err(|e| "Component isn't valid JSON".to_string())?; + // let component = Component::deserialize(json).map_err(|e| e.to_string())?; + // Ok(component) + // } + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + // component doesn't have serialize implemented yet + todo!() + } +} diff --git a/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs b/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs new file mode 100644 index 00000000..74f5f72e --- /dev/null +++ b/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs @@ -0,0 +1,9 @@ +use azalea_chat::component::Component; +use azalea_core::resource_location::ResourceLocation; +use packet_macros::GamePacket; +use serde::Deserialize; + +#[derive(Clone, Debug, GamePacket)] +pub struct ClientboundDisconnectPacket { + pub reason: Component, +} diff --git a/azalea-protocol/src/packets/game/mod.rs b/azalea-protocol/src/packets/game/mod.rs index e150606c..dde3f753 100755 --- a/azalea-protocol/src/packets/game/mod.rs +++ b/azalea-protocol/src/packets/game/mod.rs @@ -1,6 +1,7 @@ pub mod clientbound_change_difficulty_packet; pub mod clientbound_custom_payload_packet; pub mod clientbound_declare_commands_packet; +pub mod clientbound_disconnect_packet; pub mod clientbound_login_packet; pub mod clientbound_player_abilities_packet; pub mod clientbound_set_carried_item_packet; @@ -15,6 +16,7 @@ declare_state_packets!( Clientbound => { 0x0e: clientbound_change_difficulty_packet::ClientboundChangeDifficultyPacket, 0x12: clientbound_declare_commands_packet::ClientboundDeclareCommandsPacket, + 0x1a: clientbound_disconnect_packet::ClientboundDisconnectPacket, 0x18: clientbound_custom_payload_packet::ClientboundCustomPayloadPacket, 0x26: clientbound_login_packet::ClientboundLoginPacket, 0x32: clientbound_player_abilities_packet::ClientboundPlayerAbilitiesPacket, diff --git a/azalea-protocol/src/packets/handshake/client_intention_packet.rs b/azalea-protocol/src/packets/handshake/client_intention_packet.rs index 6216ddc4..a92d65f6 100755 --- a/azalea-protocol/src/packets/handshake/client_intention_packet.rs +++ b/azalea-protocol/src/packets/handshake/client_intention_packet.rs @@ -1,7 +1,9 @@ -use crate::packets::ConnectionProtocol; +use crate::{mc_buf::Writable, packets::ConnectionProtocol}; use packet_macros::HandshakePacket; use std::hash::Hash; +use super::HandshakePacket; + #[derive(Hash, Clone, Debug, HandshakePacket)] pub struct ClientIntentionPacket { #[varint] @@ -10,3 +12,23 @@ pub struct ClientIntentionPacket { pub port: u16, pub intention: ConnectionProtocol, } + +// impl ClientIntentionPacket { +// pub fn get(self) -> HandshakePacket { +// HandshakePacket::ClientIntentionPacket(self) +// } + +// pub fn write(&self, buf: &mut Vec) -> Result<(), std::io::Error> { +// buf.write_varint(self.protocol_version as i32)?; +// buf.write_utf(&self.hostname)?; +// buf.write_short(self.port as i16)?; +// buf.write_varint(self.intention as i32)?; +// Ok(()) +// } + +// pub async fn read( +// buf: &mut T, +// ) -> Result { +// todo!() +// } +// } diff --git a/azalea-protocol/src/packets/login/clientbound_login_disconnect_packet.rs b/azalea-protocol/src/packets/login/clientbound_login_disconnect_packet.rs new file mode 100644 index 00000000..28d91c79 --- /dev/null +++ b/azalea-protocol/src/packets/login/clientbound_login_disconnect_packet.rs @@ -0,0 +1,7 @@ +use azalea_chat::component::Component; +use packet_macros::LoginPacket; + +#[derive(Clone, Debug, LoginPacket)] +pub struct ClientboundLoginDisconnectPacket { + pub reason: Component, +} diff --git a/azalea-protocol/src/packets/login/mod.rs b/azalea-protocol/src/packets/login/mod.rs index ef5f15c1..ab68518c 100755 --- a/azalea-protocol/src/packets/login/mod.rs +++ b/azalea-protocol/src/packets/login/mod.rs @@ -2,6 +2,7 @@ pub mod clientbound_custom_query_packet; pub mod clientbound_game_profile_packet; pub mod clientbound_hello_packet; pub mod clientbound_login_compression_packet; +pub mod clientbound_login_disconnect_packet; pub mod serverbound_hello_packet; use packet_macros::declare_state_packets; @@ -12,7 +13,9 @@ declare_state_packets!( 0x00: serverbound_hello_packet::ServerboundHelloPacket, }, Clientbound => { - 0x00: clientbound_hello_packet::ClientboundHelloPacket, + // 0x00: clientbound_login_disconnect_packet::ClientboundLoginDisconnectPacket, + 26: clientbound_login_disconnect_packet::ClientboundLoginDisconnectPacket, + 0x01: clientbound_hello_packet::ClientboundHelloPacket, 0x02: clientbound_game_profile_packet::ClientboundGameProfilePacket, 0x03: clientbound_login_compression_packet::ClientboundLoginCompressionPacket, 0x04: clientbound_custom_query_packet::ClientboundCustomQueryPacket, diff --git a/azalea-protocol/src/packets/mod.rs b/azalea-protocol/src/packets/mod.rs index f35451c6..98741a75 100755 --- a/azalea-protocol/src/packets/mod.rs +++ b/azalea-protocol/src/packets/mod.rs @@ -12,9 +12,9 @@ use num_derive::FromPrimitive; use num_traits::FromPrimitive; use tokio::io::AsyncRead; -pub const PROTOCOL_VERSION: u32 = 757; +pub const PROTOCOL_VERSION: u32 = 758; -#[derive(Debug, Clone, PartialEq, Eq, Hash, FromPrimitive)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, FromPrimitive)] pub enum ConnectionProtocol { Handshake = -1, Game = 0, @@ -63,6 +63,6 @@ impl McBufReadable for ConnectionProtocol { impl McBufWritable for ConnectionProtocol { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { - buf.write_varint(self.clone() as i32) + buf.write_varint(*self as i32) } } diff --git a/bot/src/main.rs b/bot/src/main.rs index 011c7d0d..7d129478 100755 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -2,8 +2,8 @@ async fn main() { println!("Hello, world!"); - let address = "95.111.249.143:10000"; - // let address = "localhost:63482"; + // let address = "95.111.249.143:10000"; + let address = "localhost:52400"; // let response = azalea_client::ping::ping_server(&address.try_into().unwrap()) // .await // .unwrap(); -- cgit v1.2.3