From 9c69d7d5f2f704b1de37e1a102bf4390cdd879a5 Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 26 Apr 2022 19:38:07 -0500 Subject: [PATCH] finish update recipes packet implementation --- azalea-auth/src/lib.rs | 4 +- azalea-client/src/connect.rs | 1 + azalea-core/src/resource_location.rs | 7 +- .../clientbound_declare_commands_packet.rs | 5 - .../game/clientbound_update_recipes_packet.rs | 128 ++++++++++++++++-- .../game/clientbound_update_tags_packet.rs | 7 - bot/src/main.rs | 2 +- 7 files changed, 125 insertions(+), 29 deletions(-) diff --git a/azalea-auth/src/lib.rs b/azalea-auth/src/lib.rs index afe9eb33..ba35055f 100755 --- a/azalea-auth/src/lib.rs +++ b/azalea-auth/src/lib.rs @@ -1,6 +1,4 @@ //! Handle Minecraft authentication. -pub mod game_profile; pub mod encryption; - - +pub mod game_profile; diff --git a/azalea-client/src/connect.rs b/azalea-client/src/connect.rs index f441f2f3..b29ead3c 100755 --- a/azalea-client/src/connect.rs +++ b/azalea-client/src/connect.rs @@ -122,6 +122,7 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> { panic!("Error: {:?}", e); } } + println!(); } Ok(()) diff --git a/azalea-core/src/resource_location.rs b/azalea-core/src/resource_location.rs index 7e28a2a2..cdf8f381 100755 --- a/azalea-core/src/resource_location.rs +++ b/azalea-core/src/resource_location.rs @@ -1,6 +1,6 @@ //! A resource, like minecraft:stone -#[derive(Hash, Clone, Debug, PartialEq, Eq)] +#[derive(Hash, Clone, PartialEq, Eq)] pub struct ResourceLocation { pub namespace: String, pub path: String, @@ -36,6 +36,11 @@ impl std::fmt::Display for ResourceLocation { write!(f, "{}:{}", self.namespace, self.path) } } +impl std::fmt::Debug for ResourceLocation { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}:{}", self.namespace, self.path) + } +} #[cfg(test)] mod tests { 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 dc914f73..2df78ce9 100755 --- a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs @@ -23,7 +23,6 @@ impl ClientboundDeclareCommandsPacket { 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?; @@ -55,20 +54,16 @@ impl McBufReadable for BrigadierNodeStub { 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?) diff --git a/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs index d9e6b262..5ae06a6f 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs @@ -25,14 +25,54 @@ pub struct ShapelessRecipe { ingredients: Vec, result: Slot, } -#[derive(Clone, Debug, McBufReadable, McBufWritable)] +#[derive(Clone, Debug)] pub struct ShapedRecipe { - width: u32, - height: u32, + // TODO: make own McBufReadable and McBufWritable for this + width: usize, + height: usize, group: String, ingredients: Vec, result: Slot, } + +impl McBufWritable for ShapedRecipe { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_varint(self.width.try_into().unwrap())?; + buf.write_varint(self.height.try_into().unwrap())?; + buf.write_utf(&self.group)?; + for ingredient in &self.ingredients { + ingredient.write_into(buf)?; + } + self.result.write_into(buf)?; + + Ok(()) + } +} +#[async_trait] +impl McBufReadable for ShapedRecipe { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + let width = buf.read_varint().await?.try_into().unwrap(); + let height = buf.read_varint().await?.try_into().unwrap(); + let group = buf.read_utf().await?; + let mut ingredients = Vec::with_capacity(width * height); + for _ in 0..width * height { + ingredients.push(Ingredient::read_into(buf).await?); + } + let result = Slot::read_into(buf).await?; + + Ok(ShapedRecipe { + width, + height, + group, + ingredients, + result, + }) + } +} + #[derive(Clone, Debug, McBufReadable, McBufWritable)] pub struct CookingRecipe { group: String, @@ -78,6 +118,7 @@ pub enum RecipeData { Smoking(CookingRecipe), CampfireCooking(CookingRecipe), Stonecutting(StoneCuttingRecipe), + Smithing(SmithingRecipe), } #[derive(Clone, Debug, McBufReadable, McBufWritable)] @@ -90,6 +131,7 @@ impl McBufWritable for Recipe { todo!() } } + #[async_trait] impl McBufReadable for Recipe { async fn read_into(buf: &mut R) -> Result @@ -103,15 +145,77 @@ impl McBufReadable for Recipe { // if-else chain :( let data = if recipe_type == ResourceLocation::new("minecraft:crafting_shapeless").unwrap() { - let group = buf.read_utf().await?; - let ingredients = Vec::::read_into(buf).await?; - let result = Slot::read_into(buf).await?; - - RecipeData::CraftingShapeless(ShapelessRecipe { - group, - ingredients, - result, - }) + RecipeData::CraftingShapeless(ShapelessRecipe::read_into(buf).await?) + } else if recipe_type == ResourceLocation::new("minecraft:crafting_shaped").unwrap() { + RecipeData::CraftingShaped(ShapedRecipe::read_into(buf).await?) + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_armordye").unwrap() + { + RecipeData::CraftingSpecialArmorDye + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_bookcloning").unwrap() + { + RecipeData::CraftingSpecialBookCloning + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_mapcloning").unwrap() + { + RecipeData::CraftingSpecialMapCloning + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_mapextending").unwrap() + { + RecipeData::CraftingSpecialMapExtending + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_firework_rocket").unwrap() + { + RecipeData::CraftingSpecialFireworkRocket + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_firework_star").unwrap() + { + RecipeData::CraftingSpecialFireworkStar + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_firework_star_fade").unwrap() + { + RecipeData::CraftingSpecialFireworkStarFade + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_repairitem").unwrap() + { + RecipeData::CraftingSpecialRepairItem + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_tippedarrow").unwrap() + { + RecipeData::CraftingSpecialTippedArrow + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_bannerduplicate").unwrap() + { + RecipeData::CraftingSpecialBannerDuplicate + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_banneraddpattern").unwrap() + { + RecipeData::CraftingSpecialBannerAddPattern + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_shielddecoration").unwrap() + { + RecipeData::CraftingSpecialShieldDecoration + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_shulkerboxcoloring").unwrap() + { + RecipeData::CraftingSpecialShulkerBoxColoring + } else if recipe_type + == ResourceLocation::new("minecraft:crafting_special_suspiciousstew").unwrap() + { + RecipeData::CraftingSpecialSuspiciousStew + } else if recipe_type == ResourceLocation::new("minecraft:smelting").unwrap() { + RecipeData::Smelting(CookingRecipe::read_into(buf).await?) + } else if recipe_type == ResourceLocation::new("minecraft:blasting").unwrap() { + RecipeData::Blasting(CookingRecipe::read_into(buf).await?) + } else if recipe_type == ResourceLocation::new("minecraft:smoking").unwrap() { + RecipeData::Smoking(CookingRecipe::read_into(buf).await?) + } else if recipe_type == ResourceLocation::new("minecraft:campfire_cooking").unwrap() { + RecipeData::CampfireCooking(CookingRecipe::read_into(buf).await?) + } else if recipe_type == ResourceLocation::new("minecraft:stonecutting").unwrap() { + RecipeData::Stonecutting(StoneCuttingRecipe::read_into(buf).await?) + } else if recipe_type == ResourceLocation::new("minecraft:smithing").unwrap() { + RecipeData::Smithing(SmithingRecipe::read_into(buf).await?) } else { panic!("Unknown recipe type sent by server: {}", recipe_type); }; diff --git a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs index 66eee4b6..b6046948 100755 --- a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs @@ -24,20 +24,16 @@ impl McBufReadable for HashMap> { 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) @@ -60,11 +56,8 @@ impl McBufReadable for Tags { 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 }) } } diff --git a/bot/src/main.rs b/bot/src/main.rs index 71e325c6..b13a608b 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -3,7 +3,7 @@ async fn main() { println!("Hello, world!"); // let address = "95.111.249.143:10000"; - let address = "localhost:52400"; + let address = "localhost:57308"; // let response = azalea_client::ping::ping_server(&address.try_into().unwrap()) // .await // .unwrap();