mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
joining a server works
This commit is contained in:
parent
3937faf18d
commit
0fc82aa579
5 changed files with 104 additions and 70 deletions
|
@ -132,48 +132,48 @@ impl From<Vec<u8>> for BitSet {
|
|||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct FixedBitSet<const N: usize>
|
||||
where
|
||||
[(); N.div_ceil(64)]: Sized,
|
||||
[(); N.div_ceil(8)]: Sized,
|
||||
{
|
||||
data: [u64; N.div_ceil(64)],
|
||||
data: [u8; N.div_ceil(8)],
|
||||
}
|
||||
|
||||
impl<const N: usize> FixedBitSet<N>
|
||||
where
|
||||
[u64; N.div_ceil(64)]: Sized,
|
||||
[u8; N.div_ceil(8)]: Sized,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
FixedBitSet {
|
||||
data: [0; N.div_ceil(64)],
|
||||
data: [0; N.div_ceil(8)],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn index(&self, index: usize) -> bool {
|
||||
(self.data[index / 64] & (1u64 << (index % 64))) != 0
|
||||
(self.data[index / 8] & (1u8 << (index % 8))) != 0
|
||||
}
|
||||
|
||||
pub fn set(&mut self, bit_index: usize) {
|
||||
self.data[bit_index / 64] |= 1u64 << (bit_index % 64);
|
||||
self.data[bit_index / 8] |= 1u8 << (bit_index % 8);
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> McBufReadable for FixedBitSet<N>
|
||||
where
|
||||
[u64; N.div_ceil(64)]: Sized,
|
||||
[u8; N.div_ceil(8)]: Sized,
|
||||
{
|
||||
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
|
||||
let mut data = [0; N.div_ceil(64)];
|
||||
for i in 0..N.div_ceil(64) {
|
||||
data[i] = u64::read_from(buf)?;
|
||||
let mut data = [0; N.div_ceil(8)];
|
||||
for i in 0..N.div_ceil(8) {
|
||||
data[i] = u8::read_from(buf)?;
|
||||
}
|
||||
Ok(FixedBitSet { data })
|
||||
}
|
||||
}
|
||||
impl<const N: usize> McBufWritable for FixedBitSet<N>
|
||||
where
|
||||
[u64; N.div_ceil(64)]: Sized,
|
||||
[u8; N.div_ceil(8)]: Sized,
|
||||
{
|
||||
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
|
||||
for i in 0..N.div_ceil(64) {
|
||||
for i in 0..N.div_ceil(8) {
|
||||
self.data[i].write_into(buf)?;
|
||||
}
|
||||
Ok(())
|
||||
|
@ -181,7 +181,7 @@ where
|
|||
}
|
||||
impl<const N: usize> Default for FixedBitSet<N>
|
||||
where
|
||||
[u64; N.div_ceil(64)]: Sized,
|
||||
[u8; N.div_ceil(8)]: Sized,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
|
|
|
@ -221,11 +221,18 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream {
|
|||
});
|
||||
serverbound_read_match_contents.extend(quote! {
|
||||
#id => {
|
||||
let data = #module::#name::read(buf).map_err(|e| crate::read::ReadPacketError::Parse { source: e, packet_id: #id, packet_name: #name_litstr.to_string() })?;
|
||||
let mut leftover = Vec::new();
|
||||
let _ = std::io::Read::read_to_end(buf, &mut leftover);
|
||||
if !leftover.is_empty() {
|
||||
return Err(crate::read::ReadPacketError::LeftoverData { packet_name: #name_litstr.to_string(), data: leftover });
|
||||
let data = #module::#name::read(buf).map_err(|e| crate::read::ReadPacketError::Parse {
|
||||
source: e,
|
||||
packet_id: #id,
|
||||
packet_name: #name_litstr.to_string(),
|
||||
})?;
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
let mut leftover = Vec::new();
|
||||
let _ = std::io::Read::read_to_end(buf, &mut leftover);
|
||||
if !leftover.is_empty() {
|
||||
return Err(crate::read::ReadPacketError::LeftoverData { packet_name: #name_litstr.to_string(), data: leftover });
|
||||
}
|
||||
}
|
||||
data
|
||||
},
|
||||
|
@ -246,7 +253,11 @@ pub fn declare_state_packets(input: TokenStream) -> TokenStream {
|
|||
});
|
||||
clientbound_read_match_contents.extend(quote! {
|
||||
#id => {
|
||||
let data = #module::#name::read(buf).map_err(|e| crate::read::ReadPacketError::Parse { source: e, packet_id: #id, packet_name: #name_litstr.to_string() })?;
|
||||
let data = #module::#name::read(buf).map_err(|e| crate::read::ReadPacketError::Parse {
|
||||
source: e,
|
||||
packet_id: #id,
|
||||
packet_name: #name_litstr.to_string(),
|
||||
})?;
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
let mut leftover = Vec::new();
|
||||
|
|
|
@ -20,17 +20,27 @@ pub struct Recipe {
|
|||
pub struct ShapelessRecipe {
|
||||
/// Used to group similar recipes together in the recipe book.
|
||||
/// Tag is present in recipe JSON
|
||||
group: String,
|
||||
ingredients: Vec<Ingredient>,
|
||||
result: Slot,
|
||||
pub group: String,
|
||||
pub category: CraftingBookCategory,
|
||||
pub ingredients: Vec<Ingredient>,
|
||||
pub result: Slot,
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ShapedRecipe {
|
||||
width: usize,
|
||||
height: usize,
|
||||
group: String,
|
||||
ingredients: Vec<Ingredient>,
|
||||
result: Slot,
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub group: String,
|
||||
pub category: CraftingBookCategory,
|
||||
pub ingredients: Vec<Ingredient>,
|
||||
pub result: Slot,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Copy, McBuf)]
|
||||
pub enum CraftingBookCategory {
|
||||
Building = 0,
|
||||
Redstone,
|
||||
Equipment,
|
||||
Misc,
|
||||
}
|
||||
|
||||
impl McBufWritable for ShapedRecipe {
|
||||
|
@ -38,6 +48,7 @@ impl McBufWritable for ShapedRecipe {
|
|||
(self.width as u32).var_write_into(buf)?;
|
||||
(self.height as u32).var_write_into(buf)?;
|
||||
self.group.write_into(buf)?;
|
||||
self.category.write_into(buf)?;
|
||||
for ingredient in &self.ingredients {
|
||||
ingredient.write_into(buf)?;
|
||||
}
|
||||
|
@ -51,6 +62,7 @@ impl McBufReadable for ShapedRecipe {
|
|||
let width = u32::var_read_from(buf)?.try_into().unwrap();
|
||||
let height = u32::var_read_from(buf)?.try_into().unwrap();
|
||||
let group = String::read_from(buf)?;
|
||||
let category = CraftingBookCategory::read_from(buf)?;
|
||||
let mut ingredients = Vec::with_capacity(width * height);
|
||||
for _ in 0..width * height {
|
||||
ingredients.push(Ingredient::read_from(buf)?);
|
||||
|
@ -61,6 +73,7 @@ impl McBufReadable for ShapedRecipe {
|
|||
width,
|
||||
height,
|
||||
group,
|
||||
category,
|
||||
ingredients,
|
||||
result,
|
||||
})
|
||||
|
@ -69,49 +82,55 @@ impl McBufReadable for ShapedRecipe {
|
|||
|
||||
#[derive(Clone, Debug, McBuf)]
|
||||
pub struct CookingRecipe {
|
||||
group: String,
|
||||
ingredient: Ingredient,
|
||||
result: Slot,
|
||||
experience: f32,
|
||||
pub group: String,
|
||||
pub category: CraftingBookCategory,
|
||||
pub ingredient: Ingredient,
|
||||
pub result: Slot,
|
||||
pub experience: f32,
|
||||
#[var]
|
||||
cooking_time: u32,
|
||||
pub cooking_time: u32,
|
||||
}
|
||||
#[derive(Clone, Debug, McBuf)]
|
||||
pub struct StoneCuttingRecipe {
|
||||
group: String,
|
||||
ingredient: Ingredient,
|
||||
result: Slot,
|
||||
pub struct StoneCutterRecipe {
|
||||
pub group: String,
|
||||
pub ingredient: Ingredient,
|
||||
pub result: Slot,
|
||||
}
|
||||
#[derive(Clone, Debug, McBuf)]
|
||||
pub struct SmithingRecipe {
|
||||
base: Ingredient,
|
||||
addition: Ingredient,
|
||||
result: Slot,
|
||||
pub base: Ingredient,
|
||||
pub addition: Ingredient,
|
||||
pub result: Slot,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, McBuf)]
|
||||
pub struct SimpleRecipe {
|
||||
pub category: CraftingBookCategory,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum RecipeData {
|
||||
CraftingShapeless(ShapelessRecipe),
|
||||
CraftingShaped(ShapedRecipe),
|
||||
CraftingSpecialArmorDye,
|
||||
CraftingSpecialBookCloning,
|
||||
CraftingSpecialMapCloning,
|
||||
CraftingSpecialMapExtending,
|
||||
CraftingSpecialFireworkRocket,
|
||||
CraftingSpecialFireworkStar,
|
||||
CraftingSpecialFireworkStarFade,
|
||||
CraftingSpecialRepairItem,
|
||||
CraftingSpecialTippedArrow,
|
||||
CraftingSpecialBannerDuplicate,
|
||||
CraftingSpecialBannerAddPattern,
|
||||
CraftingSpecialShieldDecoration,
|
||||
CraftingSpecialShulkerBoxColoring,
|
||||
CraftingSpecialSuspiciousStew,
|
||||
CraftingSpecialArmorDye(SimpleRecipe),
|
||||
CraftingSpecialBookCloning(SimpleRecipe),
|
||||
CraftingSpecialMapCloning(SimpleRecipe),
|
||||
CraftingSpecialMapExtending(SimpleRecipe),
|
||||
CraftingSpecialFireworkRocket(SimpleRecipe),
|
||||
CraftingSpecialFireworkStar(SimpleRecipe),
|
||||
CraftingSpecialFireworkStarFade(SimpleRecipe),
|
||||
CraftingSpecialRepairItem(SimpleRecipe),
|
||||
CraftingSpecialTippedArrow(SimpleRecipe),
|
||||
CraftingSpecialBannerDuplicate(SimpleRecipe),
|
||||
CraftingSpecialBannerAddPattern(SimpleRecipe),
|
||||
CraftingSpecialShieldDecoration(SimpleRecipe),
|
||||
CraftingSpecialShulkerBoxColoring(SimpleRecipe),
|
||||
CraftingSpecialSuspiciousStew(SimpleRecipe),
|
||||
Smelting(CookingRecipe),
|
||||
Blasting(CookingRecipe),
|
||||
Smoking(CookingRecipe),
|
||||
CampfireCooking(CookingRecipe),
|
||||
Stonecutting(StoneCuttingRecipe),
|
||||
Stonecutting(StoneCutterRecipe),
|
||||
Smithing(SmithingRecipe),
|
||||
}
|
||||
|
||||
|
@ -141,59 +160,59 @@ impl McBufReadable for Recipe {
|
|||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_armordye").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialArmorDye
|
||||
RecipeData::CraftingSpecialArmorDye(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_bookcloning").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialBookCloning
|
||||
RecipeData::CraftingSpecialBookCloning(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_mapcloning").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialMapCloning
|
||||
RecipeData::CraftingSpecialMapCloning(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_mapextending").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialMapExtending
|
||||
RecipeData::CraftingSpecialMapExtending(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_firework_rocket").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialFireworkRocket
|
||||
RecipeData::CraftingSpecialFireworkRocket(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_firework_star").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialFireworkStar
|
||||
RecipeData::CraftingSpecialFireworkStar(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_firework_star_fade").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialFireworkStarFade
|
||||
RecipeData::CraftingSpecialFireworkStarFade(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_repairitem").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialRepairItem
|
||||
RecipeData::CraftingSpecialRepairItem(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_tippedarrow").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialTippedArrow
|
||||
RecipeData::CraftingSpecialTippedArrow(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_bannerduplicate").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialBannerDuplicate
|
||||
RecipeData::CraftingSpecialBannerDuplicate(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_banneraddpattern").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialBannerAddPattern
|
||||
RecipeData::CraftingSpecialBannerAddPattern(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_shielddecoration").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialShieldDecoration
|
||||
RecipeData::CraftingSpecialShieldDecoration(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_shulkerboxcoloring").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialShulkerBoxColoring
|
||||
RecipeData::CraftingSpecialShulkerBoxColoring(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type
|
||||
== ResourceLocation::new("minecraft:crafting_special_suspiciousstew").unwrap()
|
||||
{
|
||||
RecipeData::CraftingSpecialSuspiciousStew
|
||||
RecipeData::CraftingSpecialSuspiciousStew(SimpleRecipe::read_from(buf)?)
|
||||
} else if recipe_type == ResourceLocation::new("minecraft:smelting").unwrap() {
|
||||
RecipeData::Smelting(CookingRecipe::read_from(buf)?)
|
||||
} else if recipe_type == ResourceLocation::new("minecraft:blasting").unwrap() {
|
||||
|
@ -203,7 +222,7 @@ impl McBufReadable for Recipe {
|
|||
} else if recipe_type == ResourceLocation::new("minecraft:campfire_cooking").unwrap() {
|
||||
RecipeData::CampfireCooking(CookingRecipe::read_from(buf)?)
|
||||
} else if recipe_type == ResourceLocation::new("minecraft:stonecutting").unwrap() {
|
||||
RecipeData::Stonecutting(StoneCuttingRecipe::read_from(buf)?)
|
||||
RecipeData::Stonecutting(StoneCutterRecipe::read_from(buf)?)
|
||||
} else if recipe_type == ResourceLocation::new("minecraft:smithing").unwrap() {
|
||||
RecipeData::Smithing(SmithingRecipe::read_from(buf)?)
|
||||
} else {
|
||||
|
|
|
@ -7,6 +7,7 @@ use bytes::BytesMut;
|
|||
use flate2::read::ZlibDecoder;
|
||||
use futures::StreamExt;
|
||||
use log::{log_enabled, trace};
|
||||
use std::backtrace::Backtrace;
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
io::{Cursor, Read},
|
||||
|
@ -17,10 +18,11 @@ use tokio_util::codec::{BytesCodec, FramedRead};
|
|||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ReadPacketError {
|
||||
#[error("Error reading packet {packet_name} ({packet_id}): {source}")]
|
||||
#[error("Error reading packet {packet_name} (id {packet_id}): {source}")]
|
||||
Parse {
|
||||
packet_id: u32,
|
||||
packet_name: String,
|
||||
#[backtrace]
|
||||
source: BufReadError,
|
||||
},
|
||||
#[error("Unknown packet id {id} in state {state_name}")]
|
||||
|
|
|
@ -2,6 +2,7 @@ use crate::{packets::ProtocolPacket, read::MAXIMUM_UNCOMPRESSED_LENGTH};
|
|||
use async_compression::tokio::bufread::ZlibEncoder;
|
||||
use azalea_buf::McBufVarWritable;
|
||||
use azalea_crypto::Aes128CfbEnc;
|
||||
use log::trace;
|
||||
use std::fmt::Debug;
|
||||
use thiserror::Error;
|
||||
use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||
|
@ -78,6 +79,7 @@ where
|
|||
P: ProtocolPacket + Debug,
|
||||
W: AsyncWrite + Unpin + Send,
|
||||
{
|
||||
trace!("Sending packet: {:?}", packet);
|
||||
let mut buf = packet_encoder(packet).unwrap();
|
||||
if let Some(threshold) = compression_threshold {
|
||||
buf = compression_encoder(&buf, threshold).await.unwrap();
|
||||
|
|
Loading…
Add table
Reference in a new issue