Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.20.5 #127

Merged
merged 16 commits into from
Apr 23, 2024
Prev Previous commit
Next Next commit
fix failing tests
  • Loading branch information
mat-1 committed Apr 19, 2024
commit 81c14d42d472dc6d19063d7d2da98cf2f346fcbe
2 changes: 1 addition & 1 deletion azalea-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ impl Client {
conn.write(
ServerboundKeyPacket {
key_bytes: e.encrypted_public_key,
encrypted_challenge: e.encrypted_nonce,
encrypted_challenge: e.encrypted_challenge,
}
.get(),
)
Expand Down
10 changes: 5 additions & 5 deletions azalea-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ pub fn hex_digest(digest: &[u8]) -> String {
pub struct EncryptResult {
pub secret_key: [u8; 16],
pub encrypted_public_key: Vec<u8>,
pub encrypted_nonce: Vec<u8>,
pub encrypted_challenge: Vec<u8>,
}

pub fn encrypt(public_key: &[u8], nonce: &[u8]) -> Result<EncryptResult, String> {
pub fn encrypt(public_key: &[u8], challenge: &[u8]) -> Result<EncryptResult, String> {
// On receipt of a Encryption Request from the server, the client will
// generate a random 16-byte shared secret, to be used with the AES/CFB8
// stream cipher.
Expand All @@ -51,14 +51,14 @@ pub fn encrypt(public_key: &[u8], nonce: &[u8]) -> Result<EncryptResult, String>
// &secret_key));

// this.keybytes = Crypt.encryptUsingKey(publicKey, secretKey.getEncoded());
// this.nonce = Crypt.encryptUsingKey(publicKey, arrby);
// this.challenge = Crypt.encryptUsingKey(publicKey, arrby);
let encrypted_public_key: Vec<u8> = rsa_public_encrypt_pkcs1::encrypt(public_key, &secret_key)?;
let encrypted_nonce: Vec<u8> = rsa_public_encrypt_pkcs1::encrypt(public_key, nonce)?;
let encrypted_challenge: Vec<u8> = rsa_public_encrypt_pkcs1::encrypt(public_key, challenge)?;

Ok(EncryptResult {
secret_key,
encrypted_public_key,
encrypted_nonce,
encrypted_challenge,
})
}

Expand Down
10 changes: 5 additions & 5 deletions azalea-inventory/src/slot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufWritable};
use azalea_buf::{BufReadError, McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable};
use azalea_registry::DataComponentKind;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -122,12 +122,12 @@ impl ItemSlotData {
/// let mut a = ItemSlotData {
/// kind: Item::Stone,
/// count: 1,
/// nbt: Default::default(),
/// components: Default::default(),
/// };
/// let mut b = ItemSlotData {
/// kind: Item::Stone,
/// count: 2,
/// nbt: Default::default(),
/// components: Default::default(),
/// };
/// assert!(a.is_same_item_and_components(&b));
///
Expand Down Expand Up @@ -159,9 +159,9 @@ impl McBufReadable for ItemSlot {
impl McBufWritable for ItemSlot {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self {
ItemSlot::Empty => 0.write_into(buf)?,
ItemSlot::Empty => 0.var_write_into(buf)?,
ItemSlot::Present(i) => {
i.count.write_into(buf)?;
i.count.var_write_into(buf)?;
i.kind.write_into(buf)?;
i.components.write_into(buf)?;
}
Expand Down
9 changes: 5 additions & 4 deletions azalea-protocol/src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ pub struct WriteConnection<W: ProtocolPacket> {
/// let packet = conn.read().await?;
/// match packet {
/// ClientboundLoginPacket::Hello(p) => {
/// let e = azalea_crypto::encrypt(&p.public_key, &p.nonce).unwrap();
/// let e = azalea_crypto::encrypt(&p.public_key, &p.challenge).unwrap();
///
/// conn.write(
/// ServerboundKeyPacket {
/// key_bytes: e.encrypted_public_key,
/// encrypted_challenge: e.encrypted_nonce,
/// encrypted_challenge: e.encrypted_challenge,
/// }
/// .get(),
/// )
Expand All @@ -127,6 +127,7 @@ pub struct WriteConnection<W: ProtocolPacket> {
/// return Err("login disconnect".into());
/// }
/// ClientboundLoginPacket::CustomQuery(p) => {}
/// ClientboundLoginPacket::CookieRequest(_) => {}
/// }
/// };
///
Expand Down Expand Up @@ -368,7 +369,7 @@ impl Connection<ClientboundLoginPacket, ServerboundLoginPacket> {
/// match conn.read().await? {
/// ClientboundLoginPacket::Hello(p) => {
/// // tell Mojang we're joining the server & enable encryption
/// let e = azalea_crypto::encrypt(&p.public_key, &p.nonce).unwrap();
/// let e = azalea_crypto::encrypt(&p.public_key, &p.challenge).unwrap();
/// conn.authenticate(
/// &access_token,
/// &profile.id,
Expand All @@ -378,7 +379,7 @@ impl Connection<ClientboundLoginPacket, ServerboundLoginPacket> {
/// conn.write(
/// ServerboundKeyPacket {
/// key_bytes: e.encrypted_public_key,
/// encrypted_challenge: e.encrypted_nonce,
/// encrypted_challenge: e.encrypted_challenge,
/// }.get()
/// ).await?;
/// conn.set_encryption_key(e.secret_key);
Expand Down
141 changes: 1 addition & 140 deletions azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use azalea_buf::{
use azalea_core::resource_location::ResourceLocation;
use azalea_inventory::ItemSlot;
use azalea_protocol_macros::ClientboundGamePacket;
use azalea_registry::RecipeSerializer;

use std::io::{Cursor, Write};

Expand All @@ -13,7 +12,7 @@ pub struct ClientboundUpdateRecipesPacket {
pub recipes: Vec<RecipeHolder>,
}

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, McBuf)]
pub struct RecipeHolder {
pub id: ResourceLocation,
pub data: RecipeData,
Expand Down Expand Up @@ -155,144 +154,6 @@ pub struct Ingredient {
pub allowed: Vec<ItemSlot>,
}

impl McBufWritable for RecipeHolder {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let recipe_serializer = match &self.data {
RecipeData::CraftingShapeless(_) => RecipeSerializer::CraftingShapeless,
RecipeData::CraftingShaped(_) => RecipeSerializer::CraftingShaped,
RecipeData::CraftingSpecialArmorDye(_) => RecipeSerializer::CraftingSpecialArmordye,
RecipeData::CraftingSpecialBookCloning(_) => {
RecipeSerializer::CraftingSpecialBookcloning
}
RecipeData::CraftingSpecialMapCloning(_) => RecipeSerializer::CraftingSpecialMapcloning,
RecipeData::CraftingSpecialMapExtending(_) => {
RecipeSerializer::CraftingSpecialMapextending
}
RecipeData::CraftingSpecialFireworkRocket(_) => {
RecipeSerializer::CraftingSpecialFireworkRocket
}
RecipeData::CraftingSpecialFireworkStar(_) => {
RecipeSerializer::CraftingSpecialFireworkStar
}

RecipeData::CraftingSpecialFireworkStarFade(_) => {
RecipeSerializer::CraftingSpecialFireworkStarFade
}
RecipeData::CraftingSpecialRepairItem(_) => RecipeSerializer::CraftingSpecialRepairitem,
RecipeData::CraftingSpecialTippedArrow(_) => {
RecipeSerializer::CraftingSpecialTippedarrow
}
RecipeData::CraftingSpecialBannerDuplicate(_) => {
RecipeSerializer::CraftingSpecialBannerduplicate
}
RecipeData::CraftingSpecialShieldDecoration(_) => {
RecipeSerializer::CraftingSpecialShielddecoration
}
RecipeData::CraftingSpecialShulkerBoxColoring(_) => {
RecipeSerializer::CraftingSpecialShulkerboxcoloring
}
RecipeData::CraftingSpecialSuspiciousStew(_) => {
RecipeSerializer::CraftingSpecialSuspiciousstew
}
RecipeData::Smelting(_) => RecipeSerializer::Smelting,
RecipeData::Blasting(_) => RecipeSerializer::Blasting,
RecipeData::Smoking(_) => RecipeSerializer::Smoking,
RecipeData::CampfireCooking(_) => RecipeSerializer::CampfireCooking,
RecipeData::Stonecutting(_) => RecipeSerializer::Stonecutting,
RecipeData::SmithingTransform(_) => RecipeSerializer::SmithingTransform,
RecipeData::SmithingTrim(_) => RecipeSerializer::SmithingTrim,
RecipeData::CraftingDecoratedPot(_) => RecipeSerializer::CraftingDecoratedPot,
};
let resource_location = ResourceLocation::new(&recipe_serializer.to_string());
resource_location.write_into(buf)?;
self.id.write_into(buf)?;
self.data.write_without_id(buf)?;
Ok(())
}
}

impl McBufReadable for RecipeHolder {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let identifier = ResourceLocation::read_from(buf)?;
let recipe_serializer = RecipeSerializer::read_from(buf)?;

// rust doesn't let us match ResourceLocation so we have to do a big
// if-else chain :(
let data = match recipe_serializer {
RecipeSerializer::CraftingShaped => {
RecipeData::CraftingShaped(ShapedRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingShapeless => {
RecipeData::CraftingShapeless(ShapelessRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialArmordye => {
RecipeData::CraftingSpecialArmorDye(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialBookcloning => {
RecipeData::CraftingSpecialBookCloning(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialMapcloning => {
RecipeData::CraftingSpecialMapCloning(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialMapextending => {
RecipeData::CraftingSpecialMapExtending(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialFireworkRocket => {
RecipeData::CraftingSpecialFireworkRocket(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialFireworkStar => {
RecipeData::CraftingSpecialFireworkStar(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialFireworkStarFade => {
RecipeData::CraftingSpecialFireworkStarFade(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialRepairitem => {
RecipeData::CraftingSpecialRepairItem(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialTippedarrow => {
RecipeData::CraftingSpecialTippedArrow(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialBannerduplicate => {
RecipeData::CraftingSpecialBannerDuplicate(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialShielddecoration => {
RecipeData::CraftingSpecialShieldDecoration(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialShulkerboxcoloring => {
RecipeData::CraftingSpecialShulkerBoxColoring(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingSpecialSuspiciousstew => {
RecipeData::CraftingSpecialSuspiciousStew(SimpleRecipe::read_from(buf)?)
}
RecipeSerializer::Smelting => RecipeData::Smelting(CookingRecipe::read_from(buf)?),
RecipeSerializer::Blasting => RecipeData::Blasting(CookingRecipe::read_from(buf)?),
RecipeSerializer::Smoking => RecipeData::Smoking(CookingRecipe::read_from(buf)?),
RecipeSerializer::CampfireCooking => {
RecipeData::CampfireCooking(CookingRecipe::read_from(buf)?)
}
RecipeSerializer::Stonecutting => {
RecipeData::Stonecutting(StoneCutterRecipe::read_from(buf)?)
}
RecipeSerializer::SmithingTransform => {
RecipeData::SmithingTransform(SmithingTransformRecipe::read_from(buf)?)
}
RecipeSerializer::SmithingTrim => {
RecipeData::SmithingTrim(SmithingTrimRecipe::read_from(buf)?)
}
RecipeSerializer::CraftingDecoratedPot => {
RecipeData::CraftingDecoratedPot(SimpleRecipe::read_from(buf)?)
}
};

let recipe = RecipeHolder {
id: identifier,
data,
};

Ok(recipe)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading