1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

finish update recipes packet implementation

This commit is contained in:
mat 2022-04-26 19:38:07 -05:00
parent dd24110019
commit 9c69d7d5f2
7 changed files with 125 additions and 29 deletions

View file

@ -1,6 +1,4 @@
//! Handle Minecraft authentication.
pub mod game_profile;
pub mod encryption;
pub mod game_profile;

View file

@ -122,6 +122,7 @@ pub async fn join_server(address: &ServerAddress) -> Result<(), String> {
panic!("Error: {:?}", e);
}
}
println!();
}
Ok(())

View file

@ -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 {

View file

@ -23,7 +23,6 @@ impl ClientboundDeclareCommandsPacket {
buf: &mut T,
) -> Result<GamePacket, String> {
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?)

View file

@ -25,14 +25,54 @@ pub struct ShapelessRecipe {
ingredients: Vec<Ingredient>,
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<Ingredient>,
result: Slot,
}
impl McBufWritable for ShapedRecipe {
fn write_into(&self, buf: &mut Vec<u8>) -> 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<R>(buf: &mut R) -> Result<Self, String>
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<R>(buf: &mut R) -> Result<Self, String>
@ -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::<Ingredient>::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);
};

View file

@ -24,20 +24,16 @@ impl McBufReadable for HashMap<ResourceLocation, Vec<Tags>> {
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 })
}
}

View file

@ -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();