From 5ca49e680ed8519456dc9a9af84321d4b69dcbb3 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 23 Jun 2022 15:12:17 -0500 Subject: azalea-buf --- azalea-core/src/delta.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 azalea-core/src/delta.rs (limited to 'azalea-core/src/delta.rs') diff --git a/azalea-core/src/delta.rs b/azalea-core/src/delta.rs new file mode 100644 index 00000000..32517e0d --- /dev/null +++ b/azalea-core/src/delta.rs @@ -0,0 +1,26 @@ +/// Only works for up to 8 blocks +#[derive(Clone, Debug, McBuf)] +pub struct PositionDelta { + xa: i16, + ya: i16, + za: i16, +} + +impl PositionDelta { + pub fn float(&self) -> (f64, f64, f64) { + ( + (self.xa as f64) / 4096.0, + (self.ya as f64) / 4096.0, + (self.za as f64) / 4096.0, + ) + } +} + +impl EntityPos { + pub fn apply_delta(&mut self, delta: &PositionDelta) { + let (x, y, z) = delta.float(); + self.x += x; + self.y += y; + self.z += z; + } +} -- cgit v1.2.3 From ba399c4440c2d182f55cba7fab068e1118bcc2b7 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 23 Jun 2022 15:28:33 -0500 Subject: switch things to use azalea-buf --- azalea-buf/Cargo.toml | 2 +- azalea-buf/buf-macros/src/lib.rs | 20 ++++++++++---------- azalea-buf/src/lib.rs | 3 ++- azalea-chat/src/component.rs | 5 ++++- azalea-core/src/delta.rs | 2 ++ azalea-core/src/particle/mod.rs | 2 ++ azalea-crypto/src/signing.rs | 3 +++ azalea-nbt/src/decode.rs | 3 ++- azalea-nbt/src/encode.rs | 1 + azalea-nbt/src/lib.rs | 9 +++++---- 10 files changed, 32 insertions(+), 18 deletions(-) (limited to 'azalea-core/src/delta.rs') diff --git a/azalea-buf/Cargo.toml b/azalea-buf/Cargo.toml index 79f9d64d..7497f947 100644 --- a/azalea-buf/Cargo.toml +++ b/azalea-buf/Cargo.toml @@ -1,6 +1,6 @@ [package] edition = "2021" -name = "azalea-buf" +name = "azalea_buf" version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/azalea-buf/buf-macros/src/lib.rs b/azalea-buf/buf-macros/src/lib.rs index 3afeaeed..4cc397a2 100644 --- a/azalea-buf/buf-macros/src/lib.rs +++ b/azalea-buf/buf-macros/src/lib.rs @@ -21,11 +21,11 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt syn::Type::Path(_) => { if f.attrs.iter().any(|a| a.path.is_ident("var")) { quote! { - let #field_name = crate::McBufVarReadable::var_read_into(buf)?; + let #field_name = azalea_buf::McBufVarReadable::var_read_into(buf)?; } } else { quote! { - let #field_name = crate::McBufReadable::read_into(buf)?; + let #field_name = azalea_buf::McBufReadable::read_into(buf)?; } } } @@ -40,7 +40,7 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt let read_field_names = named.iter().map(|f| &f.ident).collect::>(); quote! { - impl crate::McBufReadable for #ident { + impl azalea_buf::McBufReadable for #ident { fn read_into(buf: &mut impl std::io::Read) -> Result { #(#read_fields)* Ok(#ident { @@ -75,10 +75,10 @@ fn create_impl_mcbufreadable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt } quote! { - impl crate::McBufReadable for #ident { + impl azalea_buf::McBufReadable for #ident { fn read_into(buf: &mut impl std::io::Read) -> Result { - let id = crate::McBufVarReadable::var_read_into(buf)?; + let id = azalea_buf::McBufVarReadable::var_read_into(buf)?; match id { #match_contents _ => Err(format!("Unknown enum variant {}", id)), @@ -110,11 +110,11 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt syn::Type::Path(_) => { if f.attrs.iter().any(|attr| attr.path.is_ident("var")) { quote! { - crate::McBufVarWritable::var_write_into(&self.#field_name, buf)?; + azalea_buf::McBufVarWritable::var_write_into(&self.#field_name, buf)?; } } else { quote! { - crate::McBufWritable::write_into(&self.#field_name, buf)?; + azalea_buf::McBufWritable::write_into(&self.#field_name, buf)?; } } } @@ -128,7 +128,7 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt .collect::>(); quote! { - impl crate::McBufWritable for #ident { + impl azalea_buf::McBufWritable for #ident { fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { #(#write_fields)* Ok(()) @@ -138,9 +138,9 @@ fn create_impl_mcbufwritable(ident: &Ident, data: &Data) -> proc_macro2::TokenSt } syn::Data::Enum(syn::DataEnum { .. }) => { quote! { - impl crate::McBufWritable for #ident { + impl azalea_buf::McBufWritable for #ident { fn write_into(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { - crate::Writable::write_varint(buf, *self as i32) + azalea_buf::Writable::write_varint(buf, *self as i32) } } } diff --git a/azalea-buf/src/lib.rs b/azalea-buf/src/lib.rs index 2ba17ac2..bd9f43be 100644 --- a/azalea-buf/src/lib.rs +++ b/azalea-buf/src/lib.rs @@ -7,6 +7,7 @@ mod definitions; mod read; mod write; +pub use buf_macros::*; pub use definitions::*; pub use read::{read_varint_async, McBufReadable, McBufVarReadable, Readable}; pub use write::{McBufVarWritable, McBufWritable, Writable}; @@ -18,7 +19,7 @@ const MAX_STRING_LENGTH: u16 = 32767; #[cfg(test)] mod tests { use super::*; - use std::{collections::HashMap, io::Cursor}; + use std::io::Cursor; #[test] fn test_write_varint() { diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs index 872a0a5c..d307bcbc 100755 --- a/azalea-chat/src/component.rs +++ b/azalea-chat/src/component.rs @@ -1,3 +1,6 @@ +use std::io::{Read, Write}; + +use azalea_buf::{McBufReadable, McBufWritable}; use serde::{de, Deserialize, Deserializer}; use crate::{ @@ -267,7 +270,7 @@ impl<'de> Deserialize<'de> for Component { impl McBufReadable for Component { fn read_into(buf: &mut impl Read) -> Result { - let string = buf.read_utf()?; + let string = String::read_into(buf)?; let json: serde_json::Value = serde_json::from_str(string.as_str()) .map_err(|_| "Component isn't valid JSON".to_string())?; let component = Component::deserialize(json).map_err(|e| e.to_string())?; diff --git a/azalea-core/src/delta.rs b/azalea-core/src/delta.rs index 32517e0d..a4d02907 100644 --- a/azalea-core/src/delta.rs +++ b/azalea-core/src/delta.rs @@ -1,3 +1,5 @@ +pub use azalea_buf::McBuf; + /// Only works for up to 8 blocks #[derive(Clone, Debug, McBuf)] pub struct PositionDelta { diff --git a/azalea-core/src/particle/mod.rs b/azalea-core/src/particle/mod.rs index fc815a0b..5ca8ac8e 100644 --- a/azalea-core/src/particle/mod.rs +++ b/azalea-core/src/particle/mod.rs @@ -1,3 +1,5 @@ +use azalea_buf::{McBufReadable, McBufWritable}; +use std::io::{Read, Write}; #[derive(Debug, Clone, McBuf)] pub struct Particle { diff --git a/azalea-crypto/src/signing.rs b/azalea-crypto/src/signing.rs index a5280a18..4fc07d30 100644 --- a/azalea-crypto/src/signing.rs +++ b/azalea-crypto/src/signing.rs @@ -1,3 +1,6 @@ +use azalea_buf::{McBufReadable, McBufWritable}; +use std::io::{Read, Write}; + #[derive(Debug, Clone)] pub struct SaltSignaturePair { pub salt: u64, diff --git a/azalea-nbt/src/decode.rs b/azalea-nbt/src/decode.rs index 73cd613e..1c011839 100755 --- a/azalea-nbt/src/decode.rs +++ b/azalea-nbt/src/decode.rs @@ -1,5 +1,6 @@ use crate::Error; use crate::Tag; +use azalea_buf::McBufReadable; use byteorder::{ReadBytesExt, BE}; use flate2::read::{GzDecoder, ZlibDecoder}; use std::collections::HashMap; @@ -139,7 +140,7 @@ impl Tag { impl McBufReadable for Tag { fn read_into(buf: &mut impl Read) -> Result { - match Tag::read(self) { + match Tag::read(buf) { Ok(r) => Ok(r), // Err(e) => Err(e.to_string()), Err(e) => Err(e.to_string()).unwrap(), diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index 17d20270..3763ffd1 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -1,5 +1,6 @@ use crate::Error; use crate::Tag; +use azalea_buf::McBufWritable; use byteorder::{WriteBytesExt, BE}; use flate2::write::{GzEncoder, ZlibEncoder}; use std::collections::HashMap; diff --git a/azalea-nbt/src/lib.rs b/azalea-nbt/src/lib.rs index 8cca1f2b..cb2eb28e 100755 --- a/azalea-nbt/src/lib.rs +++ b/azalea-nbt/src/lib.rs @@ -9,23 +9,24 @@ pub use tag::Tag; #[cfg(test)] mod tests { use super::*; + use azalea_buf::{McBufReadable, McBufWritable}; use std::{collections::HashMap, io::Cursor}; #[test] fn mcbuf_nbt() { let mut buf = Vec::new(); - buf.write_nbt(&Tag::Compound(HashMap::from_iter(vec![( + let tag = Tag::Compound(HashMap::from_iter(vec![( "hello world".to_string(), Tag::Compound(HashMap::from_iter(vec![( "name".to_string(), Tag::String("Bananrama".to_string()), )])), - )]))) - .unwrap(); + )])); + tag.write_into(&mut buf).unwrap(); let mut buf = Cursor::new(buf); - let result = buf.read_nbt().unwrap(); + let result = Tag::read_into(&mut buf).unwrap(); assert_eq!( result, Tag::Compound(HashMap::from_iter(vec![( -- cgit v1.2.3 From 37c6618c16319a7f40fd2e165190407472598e84 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 23 Jun 2022 19:17:04 -0500 Subject: Fix everything so azalea-buf works --- Cargo.lock | 8 ++- azalea-buf/Cargo.toml | 1 + azalea-buf/src/definitions.rs | 23 ++++++- azalea-buf/src/lib.rs | 2 + azalea-buf/src/serializable_uuid.rs | 76 ++++++++++++++++++++++ azalea-buf/src/write.rs | 4 +- azalea-client/src/client.rs | 18 ++--- azalea-core/src/delta.rs | 1 + azalea-core/src/difficulty.rs | 15 +++-- azalea-core/src/direction.rs | 2 + azalea-core/src/game_type.rs | 48 ++++++++++---- azalea-core/src/lib.rs | 12 ++-- azalea-core/src/particle/mod.rs | 3 +- azalea-core/src/position.rs | 18 ++--- azalea-core/src/resource_location.rs | 16 +++-- azalea-core/src/serializable_uuid.rs | 73 --------------------- azalea-core/src/slot.rs | 24 ++++--- azalea-entity/Cargo.toml | 8 +-- azalea-entity/src/data.rs | 46 +++++++------ azalea-entity/src/lib.rs | 37 ++--------- azalea-protocol/Cargo.toml | 1 + azalea-protocol/packet-macros/src/lib.rs | 4 +- azalea-protocol/src/lib.rs | 1 - .../packets/game/clientbound_add_entity_packet.rs | 19 +++++- .../packets/game/clientbound_add_player_packet.rs | 19 +++++- .../src/packets/game/clientbound_animate_packet.rs | 3 +- .../game/clientbound_block_changed_ack_packet.rs | 3 +- .../game/clientbound_block_update_packet.rs | 3 +- .../game/clientbound_change_difficulty_packet.rs | 5 +- .../game/clientbound_chat_preview_packet.rs | 3 +- .../clientbound_container_set_content_packet.rs | 3 +- .../game/clientbound_custom_payload_packet.rs | 7 +- .../game/clientbound_declare_commands_packet.rs | 15 +++-- .../packets/game/clientbound_disconnect_packet.rs | 3 +- .../game/clientbound_entity_event_packet.rs | 3 +- .../game/clientbound_entity_velocity_packet.rs | 3 +- .../packets/game/clientbound_game_event_packet.rs | 3 +- .../game/clientbound_initialize_border_packet.rs | 3 +- .../packets/game/clientbound_keep_alive_packet.rs | 3 +- .../clientbound_level_chunk_with_light_packet.rs | 3 +- .../packets/game/clientbound_level_event_packet.rs | 3 +- .../game/clientbound_level_particles_packet.rs | 4 +- .../game/clientbound_light_update_packet.rs | 5 +- .../src/packets/game/clientbound_login_packet.rs | 7 +- .../game/clientbound_move_entity_pos_packet.rs | 5 +- .../game/clientbound_move_entity_posrot_packet.rs | 5 +- .../game/clientbound_move_entity_rot_packet.rs | 3 +- .../game/clientbound_player_abilities_packet.rs | 5 +- .../packets/game/clientbound_player_chat_packet.rs | 3 +- .../packets/game/clientbound_player_info_packet.rs | 5 +- .../game/clientbound_player_position_packet.rs | 5 +- .../src/packets/game/clientbound_recipe_packet.rs | 7 +- .../game/clientbound_remove_entities_packet.rs | 3 +- .../packets/game/clientbound_rotate_head_packet.rs | 3 +- .../clientbound_section_blocks_update_packet.rs | 5 +- .../packets/game/clientbound_server_data_packet.rs | 3 +- .../game/clientbound_set_carried_item_packet.rs | 3 +- .../clientbound_set_chunk_cache_center_packet.rs | 3 +- ...lientbound_set_default_spawn_position_packet.rs | 3 +- .../clientbound_set_display_chat_preview_packet.rs | 3 +- .../game/clientbound_set_entity_data_packet.rs | 5 +- .../game/clientbound_set_entity_link_packet.rs | 3 +- .../game/clientbound_set_equipment_packet.rs | 5 +- .../game/clientbound_set_experience_packet.rs | 3 +- .../packets/game/clientbound_set_health_packet.rs | 3 +- .../packets/game/clientbound_set_time_packet.rs | 3 +- .../src/packets/game/clientbound_sound_packet.rs | 3 +- .../packets/game/clientbound_system_chat_packet.rs | 3 +- .../game/clientbound_teleport_entity_packet.rs | 3 +- .../game/clientbound_update_advancements_packet.rs | 6 +- .../game/clientbound_update_attributes_packet.rs | 7 +- .../game/clientbound_update_recipes_packet.rs | 11 ++-- .../packets/game/clientbound_update_tags_packet.rs | 33 +++++++--- .../clientbound_update_view_distance_packet.rs | 3 +- .../game/serverbound_chat_command_packet.rs | 3 +- .../game/serverbound_chat_preview_packet.rs | 3 +- .../game/serverbound_custom_payload_packet.rs | 7 +- .../packets/game/serverbound_keep_alive_packet.rs | 3 +- .../game/serverbound_move_player_packet_pos.rs | 3 +- .../game/serverbound_move_player_packet_pos_rot.rs | 3 +- .../game/serverbound_move_player_packet_rot.rs | 3 +- .../serverbound_move_player_packet_status_only.rs | 3 +- .../packets/handshake/client_intention_packet.rs | 3 +- .../login/clientbound_custom_query_packet.rs | 6 +- .../login/clientbound_game_profile_packet.rs | 4 +- .../src/packets/login/clientbound_hello_packet.rs | 2 +- .../login/clientbound_login_compression_packet.rs | 2 +- .../login/clientbound_login_disconnect_packet.rs | 3 +- .../src/packets/login/serverbound_hello_packet.rs | 3 +- .../src/packets/login/serverbound_key_packet.rs | 5 +- azalea-protocol/src/packets/mod.rs | 11 ++-- .../status/clientbound_status_response_packet.rs | 2 +- .../status/serverbound_status_request_packet.rs | 3 +- azalea-protocol/src/read.rs | 7 +- azalea-protocol/src/write.rs | 3 +- azalea-world/Cargo.toml | 2 +- azalea-world/src/chunk.rs | 2 +- azalea-world/src/lib.rs | 30 ++++----- azalea-world/src/palette.rs | 2 +- 99 files changed, 491 insertions(+), 343 deletions(-) create mode 100644 azalea-buf/src/serializable_uuid.rs delete mode 100755 azalea-core/src/serializable_uuid.rs (limited to 'azalea-core/src/delta.rs') diff --git a/Cargo.lock b/Cargo.lock index 855b65de..aeea0fb1 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,6 +96,7 @@ dependencies = [ "buf-macros", "byteorder", "tokio", + "uuid", ] [[package]] @@ -150,8 +151,10 @@ dependencies = [ name = "azalea-entity" version = "0.1.0" dependencies = [ + "azalea-buf", + "azalea-chat", "azalea-core", - "azalea-protocol", + "azalea-nbt", "uuid", ] @@ -188,6 +191,7 @@ dependencies = [ "azalea-chat", "azalea-core", "azalea-crypto", + "azalea-entity", "azalea-nbt", "byteorder", "bytes", @@ -209,10 +213,10 @@ name = "azalea-world" version = "0.1.0" dependencies = [ "azalea-block", + "azalea-buf", "azalea-core", "azalea-entity", "azalea-nbt", - "azalea-protocol", "log", "nohash-hasher", ] diff --git a/azalea-buf/Cargo.toml b/azalea-buf/Cargo.toml index 79f9d64d..4321dace 100644 --- a/azalea-buf/Cargo.toml +++ b/azalea-buf/Cargo.toml @@ -9,3 +9,4 @@ version = "0.1.0" buf-macros = {path = "./buf-macros"} byteorder = "1.4.3" tokio = {version = "^1.19.2", features = ["io-util", "net", "macros"]} +uuid = "1.1.2" diff --git a/azalea-buf/src/definitions.rs b/azalea-buf/src/definitions.rs index e5d8e0c0..921fb63d 100644 --- a/azalea-buf/src/definitions.rs +++ b/azalea-buf/src/definitions.rs @@ -1,5 +1,8 @@ -use buf_macros::McBuf; -use std::ops::Deref; +use crate::{McBufReadable, McBufWritable}; +use std::{ + io::{Read, Write}, + ops::Deref, +}; /// A Vec that isn't prefixed by a VarInt with the size. #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -26,7 +29,7 @@ impl From<&str> for UnsizedByteArray { } /// Represents Java's BitSet, a list of bits. -#[derive(Debug, Clone, PartialEq, Eq, Hash, McBuf)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct BitSet { data: Vec, } @@ -37,3 +40,17 @@ impl BitSet { (self.data[index / 64] & (1u64 << (index % 64))) != 0 } } + +impl McBufReadable for BitSet { + fn read_into(buf: &mut impl Read) -> Result { + Ok(Self { + data: Vec::::read_into(buf)?, + }) + } +} + +impl McBufWritable for BitSet { + fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + self.data.write_into(buf) + } +} diff --git a/azalea-buf/src/lib.rs b/azalea-buf/src/lib.rs index bd9f43be..cd72f22e 100644 --- a/azalea-buf/src/lib.rs +++ b/azalea-buf/src/lib.rs @@ -5,11 +5,13 @@ mod definitions; mod read; +mod serializable_uuid; mod write; pub use buf_macros::*; pub use definitions::*; pub use read::{read_varint_async, McBufReadable, McBufVarReadable, Readable}; +pub use serializable_uuid::*; pub use write::{McBufVarWritable, McBufWritable, Writable}; // const DEFAULT_NBT_QUOTA: u32 = 2097152; diff --git a/azalea-buf/src/serializable_uuid.rs b/azalea-buf/src/serializable_uuid.rs new file mode 100644 index 00000000..10921aa6 --- /dev/null +++ b/azalea-buf/src/serializable_uuid.rs @@ -0,0 +1,76 @@ +use crate::{McBufReadable, McBufWritable, Readable}; +use std::io::{Read, Write}; +use uuid::Uuid; + +pub trait SerializableUuid { + fn to_int_array(&self) -> [u32; 4]; + fn from_int_array(array: [u32; 4]) -> Self; +} + +fn least_most_to_int_array(most: u64, least: u64) -> [u32; 4] { + [ + (most >> 32) as u32, + most as u32, + (least >> 32) as u32, + least as u32, + ] +} + +impl SerializableUuid for Uuid { + fn to_int_array(&self) -> [u32; 4] { + let most_significant_bits = (self.as_u128() >> 64) as u64; + let least_significant_bits = (self.as_u128() & 0xffffffffffffffff) as u64; + + least_most_to_int_array(most_significant_bits, least_significant_bits) + } + + fn from_int_array(array: [u32; 4]) -> Self { + let most = ((array[0] as u64) << 32) | ((array[1] as u64) & 0xFFFFFFFF); + let least = ((array[2] as u64) << 32) | ((array[3] as u64) & 0xFFFFFFFF); + + Uuid::from_u128(((most as u128) << 64) | least as u128) + } +} + +impl McBufReadable for Uuid { + fn read_into(buf: &mut impl Read) -> Result { + Ok(Uuid::from_int_array([ + Readable::read_int(buf)? as u32, + Readable::read_int(buf)? as u32, + Readable::read_int(buf)? as u32, + Readable::read_int(buf)? as u32, + ])) + } +} + +impl McBufWritable for Uuid { + fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + let [a, b, c, d] = self.to_int_array(); + a.write_into(buf)?; + b.write_into(buf)?; + c.write_into(buf)?; + d.write_into(buf)?; + Ok(()) + } +} + +// TODO: add a test for Uuid in McBuf +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn to_int_array() { + let u = Uuid::parse_str("6536bfed-8695-48fd-83a1-ecd24cf2a0fd").unwrap(); + assert_eq!( + u.to_int_array(), + [0x6536bfed, 0x869548fd, 0x83a1ecd2, 0x4cf2a0fd] + ); + } + + #[test] + fn from_int_array() { + let u = Uuid::from_int_array([0x6536bfed, 0x869548fd, 0x83a1ecd2, 0x4cf2a0fd]); + assert_eq!(u.to_string(), "6536bfed-8695-48fd-83a1-ecd24cf2a0fd"); + } +} diff --git a/azalea-buf/src/write.rs b/azalea-buf/src/write.rs index fdf58203..38ddcf49 100644 --- a/azalea-buf/src/write.rs +++ b/azalea-buf/src/write.rs @@ -189,7 +189,9 @@ impl McBufVarWritable for i64 { if value != 0 { buffer[0] |= 0b1000_0000; } - buf.write(&mut buffer)?; + // this only writes a single byte, so write_all isn't necessary + // the let _ = is so clippy doesn't complain + let _ = buf.write(&mut buffer)?; } Ok(()) } diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index a5259db9..a7841637 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -1,5 +1,5 @@ use crate::{Account, Player}; -use azalea_core::{resource_location::ResourceLocation, ChunkPos, EntityPos}; +use azalea_core::{ChunkPos, EntityPos, ResourceLocation}; use azalea_entity::Entity; use azalea_protocol::{ connect::{GameConnection, HandshakeConnection}, @@ -439,14 +439,14 @@ impl Client { let mut state_lock = state.lock()?; let world = state_lock.world.as_mut().unwrap(); - world.move_entity( - p.entity_id, - EntityPos { - x: p.x, - y: p.y, - z: p.z, - }, - )?; + // world.move_entity( + // p.entity_id, + // EntityPos { + // x: p.x, + // y: p.y, + // z: p.z, + // }, + // )?; } GamePacket::ClientboundMoveEntityRotPacket(p) => { println!("Got move entity rot packet {:?}", p); diff --git a/azalea-core/src/delta.rs b/azalea-core/src/delta.rs index a4d02907..339e52cd 100644 --- a/azalea-core/src/delta.rs +++ b/azalea-core/src/delta.rs @@ -1,3 +1,4 @@ +use crate::EntityPos; pub use azalea_buf::McBuf; /// Only works for up to 8 blocks diff --git a/azalea-core/src/difficulty.rs b/azalea-core/src/difficulty.rs index 7568f8e2..ebbb7708 100755 --- a/azalea-core/src/difficulty.rs +++ b/azalea-core/src/difficulty.rs @@ -1,11 +1,16 @@ -use std::fmt::{Debug, Error, Formatter}; +use std::{ + fmt::{Debug, Error, Formatter}, + io::{Read, Write}, +}; + +use azalea_buf::{McBufReadable, McBufWritable}; #[derive(Hash, Clone, Debug, PartialEq)] pub enum Difficulty { - PEACEFUL, - EASY, - NORMAL, - HARD, + PEACEFUL = 0, + EASY = 1, + NORMAL = 2, + HARD = 3, } pub enum Err { diff --git a/azalea-core/src/direction.rs b/azalea-core/src/direction.rs index bb655bdb..d3083922 100644 --- a/azalea-core/src/direction.rs +++ b/azalea-core/src/direction.rs @@ -1,3 +1,5 @@ +use azalea_buf::McBuf; + #[derive(Clone, Copy, Debug, McBuf)] pub enum Direction { Down = 0, diff --git a/azalea-core/src/game_type.rs b/azalea-core/src/game_type.rs index 963048dd..67c392b2 100755 --- a/azalea-core/src/game_type.rs +++ b/azalea-core/src/game_type.rs @@ -1,4 +1,7 @@ -#[derive(Hash, Clone, Debug)] +use azalea_buf::{McBufReadable, McBufWritable}; +use std::io::{Read, Write}; + +#[derive(Hash, Copy, Clone, Debug)] pub enum GameType { SURVIVAL, CREATIVE, @@ -17,8 +20,8 @@ impl GameType { } /// Get the id of the game type, but return -1 if the game type is invalid. - pub fn to_optional_id(game_type: &Option) -> i8 { - match game_type { + pub fn to_optional_id>>(game_type: T) -> i8 { + match game_type.into() { Some(game_type) => game_type.to_id() as i8, None => -1, } @@ -34,11 +37,12 @@ impl GameType { }) } - pub fn from_optional_id(id: i8) -> Result, String> { + pub fn from_optional_id(id: i8) -> Result { Ok(match id { -1 => None, id => Some(GameType::from_id(id as u8)?), - }) + } + .into()) } pub fn short_name(&self) -> &'static str { @@ -74,13 +78,7 @@ impl GameType { impl McBufReadable for GameType { fn read_into(buf: &mut impl Read) -> Result { - GameType::from_id(buf.read_byte()?) - } -} - -impl McBufReadable for Option { - fn read_into(buf: &mut impl Read) -> Result { - GameType::from_optional_id(buf.read_byte()? as i8) + GameType::from_id(u8::read_into(buf)?) } } @@ -90,8 +88,30 @@ impl McBufWritable for GameType { } } -impl McBufWritable for Option { +/// Rust doesn't let us `impl McBufReadable for Option` so we have to make a new type :( +#[derive(Hash, Copy, Clone, Debug)] +pub struct OptionalGameType(Option); + +impl From> for OptionalGameType { + fn from(game_type: Option) -> Self { + OptionalGameType(game_type) + } +} + +impl From for Option { + fn from(optional_game_type: OptionalGameType) -> Self { + optional_game_type.0 + } +} + +impl McBufReadable for OptionalGameType { + fn read_into(buf: &mut impl Read) -> Result { + GameType::from_optional_id(i8::read_into(buf)?) + } +} + +impl McBufWritable for OptionalGameType { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_byte(GameType::to_optional_id(self) as u8) + GameType::to_optional_id(*self).write_into(buf) } } diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs index 41c901c8..a1fa1fca 100755 --- a/azalea-core/src/lib.rs +++ b/azalea-core/src/lib.rs @@ -2,10 +2,14 @@ #![feature(int_roundings)] -pub mod difficulty; -pub mod game_type; -pub mod resource_location; -pub mod serializable_uuid; +mod difficulty; +pub use difficulty::*; + +mod resource_location; +pub use resource_location::*; + +mod game_type; +pub use game_type::*; mod slot; pub use slot::{Slot, SlotData}; diff --git a/azalea-core/src/particle/mod.rs b/azalea-core/src/particle/mod.rs index 5ca8ac8e..6eb53955 100644 --- a/azalea-core/src/particle/mod.rs +++ b/azalea-core/src/particle/mod.rs @@ -1,4 +1,5 @@ -use azalea_buf::{McBufReadable, McBufWritable}; +use crate::{BlockPos, Slot}; +use azalea_buf::{McBuf, McBufReadable, McBufVarReadable, McBufWritable}; use std::io::{Read, Write}; #[derive(Debug, Clone, McBuf)] diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index 9e7aca20..8caa5799 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -1,6 +1,9 @@ -use std::ops::Rem; - -use crate::resource_location::ResourceLocation; +use crate::ResourceLocation; +use azalea_buf::{McBufReadable, McBufWritable}; +use std::{ + io::{Read, Write}, + ops::Rem, +}; #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct BlockPos { @@ -202,11 +205,10 @@ impl McBufReadable for ChunkSectionPos { impl McBufWritable for BlockPos { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_long( - (((self.x & 0x3FFFFFF) as i64) << 38) - | (((self.z & 0x3FFFFFF) as i64) << 12) - | ((self.y & 0xFFF) as i64), - ) + let data = (((self.x & 0x3FFFFFF) as i64) << 38) + | (((self.z & 0x3FFFFFF) as i64) << 12) + | ((self.y & 0xFFF) as i64); + data.write_into(buf) } } diff --git a/azalea-core/src/resource_location.rs b/azalea-core/src/resource_location.rs index 6807714b..61ae8a20 100755 --- a/azalea-core/src/resource_location.rs +++ b/azalea-core/src/resource_location.rs @@ -1,5 +1,8 @@ //! A resource, like minecraft:stone +use azalea_buf::{McBufReadable, McBufWritable}; +use std::io::{Read, Write}; + #[derive(Hash, Clone, PartialEq, Eq)] pub struct ResourceLocation { pub namespace: String, @@ -44,18 +47,20 @@ impl std::fmt::Debug for ResourceLocation { impl McBufReadable for ResourceLocation { fn read_into(buf: &mut impl Read) -> Result { - let location_string = self.read_utf()?; + let location_string = String::read_into(buf)?; ResourceLocation::new(&location_string) } } impl McBufWritable for ResourceLocation { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - buf.write_utf(&self.to_string()) + self.to_string().write_into(buf) } } #[cfg(test)] mod tests { + use std::io::Cursor; + use super::*; #[test] @@ -86,13 +91,14 @@ mod tests { #[test] fn mcbuf_resource_location() { let mut buf = Vec::new(); - buf.write_resource_location(&ResourceLocation::new("minecraft:dirt").unwrap()) - .unwrap(); + ResourceLocation::new("minecraft:dirt") + .unwrap() + .write_into(&mut buf)?; let mut buf = Cursor::new(buf); assert_eq!( - buf.read_resource_location().unwrap(), + ResourceLocation::read_into(&mut buf).unwrap(), ResourceLocation::new("minecraft:dirt").unwrap() ); } diff --git a/azalea-core/src/serializable_uuid.rs b/azalea-core/src/serializable_uuid.rs deleted file mode 100755 index 2c7128ff..00000000 --- a/azalea-core/src/serializable_uuid.rs +++ /dev/null @@ -1,73 +0,0 @@ -use uuid::Uuid; - -pub trait SerializableUuid { - fn to_int_array(&self) -> [u32; 4]; - fn from_int_array(array: [u32; 4]) -> Self; -} - -fn least_most_to_int_array(most: u64, least: u64) -> [u32; 4] { - [ - (most >> 32) as u32, - most as u32, - (least >> 32) as u32, - least as u32, - ] -} - -impl SerializableUuid for Uuid { - fn to_int_array(&self) -> [u32; 4] { - let most_significant_bits = (self.as_u128() >> 64) as u64; - let least_significant_bits = (self.as_u128() & 0xffffffffffffffff) as u64; - - least_most_to_int_array(most_significant_bits, least_significant_bits) - } - - fn from_int_array(array: [u32; 4]) -> Self { - let most = ((array[0] as u64) << 32) | ((array[1] as u64) & 0xFFFFFFFF); - let least = ((array[2] as u64) << 32) | ((array[3] as u64) & 0xFFFFFFFF); - - Uuid::from_u128(((most as u128) << 64) | least as u128) - } -} - -impl McBufReadable for Uuid { - fn read_into(buf: &mut impl Read) -> Result { - Ok(Uuid::from_int_array([ - Readable::read_int(self)? as u32, - Readable::read_int(self)? as u32, - Readable::read_int(self)? as u32, - Readable::read_int(self)? as u32, - ])) - } -} - -impl McBufWritable for Uuid { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - let [a, b, c, d] = self.to_int_array(); - a.write_into(buf)?; - b.write_into(buf)?; - c.write_into(buf)?; - d.write_into(buf)?; - Ok(()) - } -} - -#[cfg(tests)] -mod tests { - use super::*; - - #[test] - fn to_int_array() { - let u = Uuid::parse_str("6536bfed-8695-48fd-83a1-ecd24cf2a0fd").unwrap(); - assert_eq!( - u.to_int_array(), - [0x6536bfed, 0x869548fd, 0x83a1ecd2, 0x4cf2a0fd] - ); - } - - #[test] - fn from_int_array() { - let u = Uuid::from_int_array([0x6536bfed, 0x869548fd, 0x83a1ecd2, 0x4cf2a0fd]); - assert_eq!(u.to_string(), "6536bfed-8695-48fd-83a1-ecd24cf2a0fd"); - } -} diff --git a/azalea-core/src/slot.rs b/azalea-core/src/slot.rs index e3b78289..6e622872 100644 --- a/azalea-core/src/slot.rs +++ b/azalea-core/src/slot.rs @@ -1,13 +1,17 @@ // TODO: have an azalea-inventory or azalea-container crate and put this there +use azalea_buf::{McBuf, McBufReadable, McBufWritable}; +use std::io::{Read, Write}; + #[derive(Debug, Clone)] pub enum Slot { - Present(SlotData), Empty, + Present(SlotData), } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, McBuf)] pub struct SlotData { + #[var] pub id: i32, pub count: u8, pub nbt: azalea_nbt::Tag, @@ -15,26 +19,20 @@ pub struct SlotData { impl McBufReadable for Slot { fn read_into(buf: &mut impl Read) -> Result { - let present = buf.read_boolean()?; + let present = bool::read_into(buf)?; if !present { return Ok(Slot::Empty); } - let id = buf.read_varint()?; - let count = buf.read_byte()?; - let nbt = buf.read_nbt()?; - Ok(Slot::Present(SlotData { id, count, nbt })) + let slot = SlotData::read_into(buf)?; + Ok(Slot::Present(slot)) } } impl McBufWritable for Slot { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { match self { - Slot::Empty => buf.write_byte(0)?, - Slot::Present(i) => { - buf.write_varint(i.id)?; - buf.write_byte(i.count)?; - buf.write_nbt(&i.nbt)?; - } + Slot::Empty => 0u8.write_into(buf)?, + Slot::Present(i) => i.write_into(buf)?, } Ok(()) diff --git a/azalea-entity/Cargo.toml b/azalea-entity/Cargo.toml index ae23267e..022a3343 100644 --- a/azalea-entity/Cargo.toml +++ b/azalea-entity/Cargo.toml @@ -6,10 +6,8 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +azalea-buf = {path = "../azalea-buf"} +azalea-chat = {path = "../azalea-chat"} azalea-core = {path = "../azalea-core"} -azalea-protocol = {path = "../azalea-protocol", optional = true} +azalea-nbt = {path = "../azalea-nbt"} uuid = "^1.1.2" - -[features] -default = ["protocol"] -protocol = ["dep:azalea-protocol"] diff --git a/azalea-entity/src/data.rs b/azalea-entity/src/data.rs index ddc6f57b..38ce671b 100644 --- a/azalea-entity/src/data.rs +++ b/azalea-entity/src/data.rs @@ -1,4 +1,12 @@ -pub type EntityMetadata = Vec; +use azalea_buf::McBufVarReadable; +use azalea_buf::{McBuf, McBufReadable, McBufWritable}; +use azalea_chat::component::Component; +use azalea_core::{BlockPos, Direction, Particle, Slot}; +use std::io::{Read, Write}; +use uuid::Uuid; + +#[derive(Clone, Debug)] +pub struct EntityMetadata(Vec); #[derive(Clone, Debug)] pub struct EntityDataItem { @@ -8,7 +16,7 @@ pub struct EntityDataItem { pub value: EntityDataValue, } -impl McBufReadable for Vec { +impl McBufReadable for EntityMetadata { fn read_into(buf: &mut impl Read) -> Result { let mut metadata = Vec::new(); loop { @@ -19,17 +27,17 @@ impl McBufReadable for Vec { let value = EntityDataValue::read_into(buf)?; metadata.push(EntityDataItem { index, value }); } - Ok(metadata) + Ok(EntityMetadata(metadata)) } } -impl McBufWritable for Vec { +impl McBufWritable for EntityMetadata { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { - for item in self { - buf.write_byte(item.index)?; + for item in &self.0 { + item.index.write_into(buf)?; item.value.write_into(buf)?; } - buf.write_byte(0xff)?; + 0xffu8.write_into(buf)?; Ok(()) } } @@ -63,20 +71,20 @@ pub enum EntityDataValue { impl McBufReadable for EntityDataValue { fn read_into(buf: &mut impl Read) -> Result { - let type_ = buf.read_varint()?; - Ok(match type_ { - 0 => EntityDataValue::Byte(buf.read_byte()?), - 1 => EntityDataValue::Int(buf.read_varint()?), - 2 => EntityDataValue::Float(buf.read_float()?), - 3 => EntityDataValue::String(buf.read_utf()?), + let data_type = i32::var_read_into(buf)?; + Ok(match data_type { + 0 => EntityDataValue::Byte(u8::read_into(buf)?), + 1 => EntityDataValue::Int(i32::read_into(buf)?), + 2 => EntityDataValue::Float(f32::read_into(buf)?), + 3 => EntityDataValue::String(String::read_into(buf)?), 4 => EntityDataValue::Component(Component::read_into(buf)?), 5 => EntityDataValue::OptionalComponent(Option::::read_into(buf)?), 6 => EntityDataValue::ItemStack(Slot::read_into(buf)?), - 7 => EntityDataValue::Boolean(buf.read_boolean()?), + 7 => EntityDataValue::Boolean(bool::read_into(buf)?), 8 => EntityDataValue::Rotations { - x: buf.read_float()?, - y: buf.read_float()?, - z: buf.read_float()?, + x: f32::read_into(buf)?, + y: f32::read_into(buf)?, + z: f32::read_into(buf)?, }, 9 => EntityDataValue::BlockPos(BlockPos::read_into(buf)?), 10 => EntityDataValue::OptionalBlockPos(Option::::read_into(buf)?), @@ -94,7 +102,7 @@ impl McBufReadable for EntityDataValue { 15 => EntityDataValue::Particle(Particle::read_into(buf)?), 16 => EntityDataValue::VillagerData(VillagerData::read_into(buf)?), 17 => EntityDataValue::OptionalUnsignedInt({ - let val = buf.read_varint()?; + let val = u32::var_read_into(buf)?; if val == 0 { None } else { @@ -102,7 +110,7 @@ impl McBufReadable for EntityDataValue { } }), 18 => EntityDataValue::Pose(Pose::read_into(buf)?), - _ => return Err(format!("Unknown entity data type: {}", type_)), + _ => return Err(format!("Unknown entity data type: {}", data_type)), }) } } diff --git a/azalea-entity/src/lib.rs b/azalea-entity/src/lib.rs index 2283142f..93ed5ea8 100644 --- a/azalea-entity/src/lib.rs +++ b/azalea-entity/src/lib.rs @@ -6,6 +6,7 @@ use azalea_protocol::packets::game::{ clientbound_add_entity_packet::ClientboundAddEntityPacket, clientbound_add_player_packet::ClientboundAddPlayerPacket, }; +pub use data::*; use uuid::Uuid; #[derive(Default, Debug)] @@ -13,10 +14,14 @@ pub struct Entity { /// The incrementing numerical id of the entity. pub id: u32, pub uuid: Uuid, - pos: EntityPos, + pub pos: EntityPos, } impl Entity { + pub fn new(id: u32, uuid: Uuid, pos: EntityPos) -> Self { + Self { id, uuid, pos } + } + pub fn pos(&self) -> &EntityPos { &self.pos } @@ -28,36 +33,6 @@ impl Entity { } } -#[cfg(feature = "protocol")] -impl From<&ClientboundAddEntityPacket> for Entity { - fn from(p: &ClientboundAddEntityPacket) -> Self { - Self { - id: p.id, - uuid: p.uuid, - pos: EntityPos { - x: p.x, - y: p.y, - z: p.z, - }, - } - } -} - -#[cfg(feature = "protocol")] -impl From<&ClientboundAddPlayerPacket> for Entity { - fn from(p: &ClientboundAddPlayerPacket) -> Self { - Self { - id: p.id, - uuid: p.uuid, - pos: EntityPos { - x: p.x, - y: p.y, - z: p.z, - }, - } - } -} - // #[cfg(test)] // mod tests { // #[test] diff --git a/azalea-protocol/Cargo.toml b/azalea-protocol/Cargo.toml index dadb212c..280879dd 100755 --- a/azalea-protocol/Cargo.toml +++ b/azalea-protocol/Cargo.toml @@ -14,6 +14,7 @@ azalea-buf = {path = "../azalea-buf"} azalea-chat = {path = "../azalea-chat"} azalea-core = {path = "../azalea-core", optional = true} azalea-crypto = {path = "../azalea-crypto"} +azalea-entity = {path = "../azalea-entity"} azalea-nbt = {path = "../azalea-nbt"} byteorder = "^1.4.3" bytes = "^1.1.0" diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 56b5dc52..58487cdd 100755 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -25,13 +25,13 @@ fn as_packet_derive(input: TokenStream, state: proc_macro2::TokenStream) -> Toke } pub fn write(&self, buf: &mut impl std::io::Write) -> Result<(), std::io::Error> { - crate::mc_buf::McBufWritable::write_into(self, buf) + azalea_buf::McBufWritable::write_into(self, buf) } pub fn read( buf: &mut impl std::io::Read, ) -> Result<#state, String> { - use crate::mc_buf::McBufReadable; + use azalea_buf::McBufReadable; Ok(Self::read_into(buf)?.get()) } } diff --git a/azalea-protocol/src/lib.rs b/azalea-protocol/src/lib.rs index cadbb437..b1d1a9c4 100755 --- a/azalea-protocol/src/lib.rs +++ b/azalea-protocol/src/lib.rs @@ -5,7 +5,6 @@ use std::str::FromStr; #[cfg(feature = "connecting")] pub mod connect; -pub mod mc_buf; #[cfg(feature = "packets")] pub mod packets; pub mod read; diff --git a/azalea-protocol/src/packets/game/clientbound_add_entity_packet.rs b/azalea-protocol/src/packets/game/clientbound_add_entity_packet.rs index 9ef7e05c..0fc7b817 100644 --- a/azalea-protocol/src/packets/game/clientbound_add_entity_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_add_entity_packet.rs @@ -1,4 +1,7 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_core::EntityPos; +use azalea_entity::Entity; +use packet_macros::GamePacket; use uuid::Uuid; #[derive(Clone, Debug, McBuf, GamePacket)] @@ -22,3 +25,17 @@ pub struct ClientboundAddEntityPacket { pub y_vel: i16, pub z_vel: i16, } + +impl From<&ClientboundAddEntityPacket> for Entity { + fn from(p: &ClientboundAddEntityPacket) -> Self { + Self::new( + p.id, + p.uuid, + EntityPos { + x: p.x, + y: p.y, + z: p.z, + }, + ) + } +} diff --git a/azalea-protocol/src/packets/game/clientbound_add_player_packet.rs b/azalea-protocol/src/packets/game/clientbound_add_player_packet.rs index 4dae88a5..ddadc73f 100644 --- a/azalea-protocol/src/packets/game/clientbound_add_player_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_add_player_packet.rs @@ -1,4 +1,7 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_core::EntityPos; +use azalea_entity::Entity; +use packet_macros::GamePacket; use uuid::Uuid; /// This packet is sent by the server when a player comes into visible range, not when a player joins. @@ -13,3 +16,17 @@ pub struct ClientboundAddPlayerPacket { pub x_rot: i8, pub y_rot: i8, } + +impl From<&ClientboundAddPlayerPacket> for Entity { + fn from(p: &ClientboundAddPlayerPacket) -> Self { + Self::new( + p.id, + p.uuid, + EntityPos { + x: p.x, + y: p.y, + z: p.z, + }, + ) + } +} diff --git a/azalea-protocol/src/packets/game/clientbound_animate_packet.rs b/azalea-protocol/src/packets/game/clientbound_animate_packet.rs index 0bba1a25..e554da0a 100644 --- a/azalea-protocol/src/packets/game/clientbound_animate_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_animate_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundAnimatePacket { diff --git a/azalea-protocol/src/packets/game/clientbound_block_changed_ack_packet.rs b/azalea-protocol/src/packets/game/clientbound_block_changed_ack_packet.rs index a580440c..2901cb82 100644 --- a/azalea-protocol/src/packets/game/clientbound_block_changed_ack_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_block_changed_ack_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundBlockChangedAckPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_block_update_packet.rs b/azalea-protocol/src/packets/game/clientbound_block_update_packet.rs index f068cc7d..769270ee 100644 --- a/azalea-protocol/src/packets/game/clientbound_block_update_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_block_update_packet.rs @@ -1,5 +1,6 @@ +use azalea_buf::McBuf; use azalea_core::BlockPos; -use packet_macros::{GamePacket, McBuf}; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundBlockUpdatePacket { diff --git a/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs b/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs index edda52d9..57cb1f69 100755 --- a/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_change_difficulty_packet.rs @@ -1,5 +1,6 @@ -use azalea_core::difficulty::Difficulty; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_core::Difficulty; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundChangeDifficultyPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_chat_preview_packet.rs b/azalea-protocol/src/packets/game/clientbound_chat_preview_packet.rs index 58dd0722..75bf0cf7 100644 --- a/azalea-protocol/src/packets/game/clientbound_chat_preview_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_chat_preview_packet.rs @@ -1,5 +1,6 @@ +use azalea_buf::McBuf; use azalea_chat::component::Component; -use packet_macros::{GamePacket, McBuf}; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundChatPreviewPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_container_set_content_packet.rs b/azalea-protocol/src/packets/game/clientbound_container_set_content_packet.rs index d38bbfda..721937a7 100644 --- a/azalea-protocol/src/packets/game/clientbound_container_set_content_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_container_set_content_packet.rs @@ -1,5 +1,6 @@ +use azalea_buf::McBuf; use azalea_core::Slot; -use packet_macros::{GamePacket, McBuf}; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundContainerSetContentPacket { 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 b9ccbba4..d01e7459 100755 --- a/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_custom_payload_packet.rs @@ -1,6 +1,7 @@ -use crate::mc_buf::UnsizedByteArray; -use azalea_core::resource_location::ResourceLocation; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_buf::UnsizedByteArray; +use azalea_core::ResourceLocation; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundCustomPayloadPacket { 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 648ca9e0..bf29a050 100755 --- a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs @@ -1,8 +1,9 @@ use super::GamePacket; -use crate::mc_buf::McBufVarReadable; -use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; -use azalea_core::resource_location::ResourceLocation; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_buf::McBufVarReadable; +use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use azalea_core::ResourceLocation; +use packet_macros::GamePacket; use std::{ hash::Hash, io::{Read, Write}, @@ -182,10 +183,10 @@ impl McBufReadable for BrigadierParser { 41 => Ok(BrigadierParser::Dimension), 42 => Ok(BrigadierParser::Time), 43 => Ok(BrigadierParser::ResourceOrTag { - registry_key: buf.read_resource_location()?, + registry_key: ResourceLocation::read_into(buf)?, }), 44 => Ok(BrigadierParser::Resource { - registry_key: buf.read_resource_location()?, + registry_key: ResourceLocation::read_into(buf)?, }), 45 => Ok(BrigadierParser::TemplateMirror), 46 => Ok(BrigadierParser::TemplateRotation), @@ -218,7 +219,7 @@ impl McBufReadable for BrigadierNodeStub { let _name = buf.read_utf()?; let _parser = BrigadierParser::read_into(buf)?; let _suggestions_type = if has_suggestions_type { - Some(buf.read_resource_location()?) + Some(ResourceLocation::read_into(buf)?) } else { None }; diff --git a/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs b/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs index c030d512..e5f35dc9 100644 --- a/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_disconnect_packet.rs @@ -1,5 +1,6 @@ +use azalea_buf::McBuf; use azalea_chat::component::Component; -use packet_macros::{GamePacket, McBuf}; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundDisconnectPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_entity_event_packet.rs b/azalea-protocol/src/packets/game/clientbound_entity_event_packet.rs index d0cc7222..a6f6e38d 100644 --- a/azalea-protocol/src/packets/game/clientbound_entity_event_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_entity_event_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; // we can't identify the status in azalea-protocol since they vary depending on the entity #[derive(Clone, Debug, McBuf, GamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_entity_velocity_packet.rs b/azalea-protocol/src/packets/game/clientbound_entity_velocity_packet.rs index 07218c4e..7e2fa606 100644 --- a/azalea-protocol/src/packets/game/clientbound_entity_velocity_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_entity_velocity_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundEntityVelocityPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_game_event_packet.rs b/azalea-protocol/src/packets/game/clientbound_game_event_packet.rs index dd5f08f6..f5cb13a1 100644 --- a/azalea-protocol/src/packets/game/clientbound_game_event_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_game_event_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundGameEventPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_initialize_border_packet.rs b/azalea-protocol/src/packets/game/clientbound_initialize_border_packet.rs index a522eba3..f759cfc9 100644 --- a/azalea-protocol/src/packets/game/clientbound_initialize_border_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_initialize_border_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundInitializeBorderPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_keep_alive_packet.rs b/azalea-protocol/src/packets/game/clientbound_keep_alive_packet.rs index 18628c86..adaca872 100644 --- a/azalea-protocol/src/packets/game/clientbound_keep_alive_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_keep_alive_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundKeepAlivePacket { diff --git a/azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs b/azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs index 43bda0b6..f105bd6a 100644 --- a/azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_level_chunk_with_light_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; use super::clientbound_light_update_packet::ClientboundLightUpdatePacketData; diff --git a/azalea-protocol/src/packets/game/clientbound_level_event_packet.rs b/azalea-protocol/src/packets/game/clientbound_level_event_packet.rs index 70926268..42c5f412 100644 --- a/azalea-protocol/src/packets/game/clientbound_level_event_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_level_event_packet.rs @@ -1,5 +1,6 @@ +use azalea_buf::McBuf; use azalea_core::BlockPos; -use packet_macros::{GamePacket, McBuf}; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundLevelEventPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs b/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs index 9ed08d8a..df1174b7 100644 --- a/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs @@ -1,5 +1,5 @@ -use crate::mc_buf::McBufVarReadable; -use crate::mc_buf::{McBufReadable, McBufWritable, ParticleData}; +use azalea_buf::{McBufReadable, McBufVarReadable, McBufWritable}; +use azalea_core::ParticleData; use packet_macros::GamePacket; use std::io::{Read, Write}; diff --git a/azalea-protocol/src/packets/game/clientbound_light_update_packet.rs b/azalea-protocol/src/packets/game/clientbound_light_update_packet.rs index f04987ac..adb6e33b 100644 --- a/azalea-protocol/src/packets/game/clientbound_light_update_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_light_update_packet.rs @@ -1,5 +1,6 @@ -use crate::mc_buf::BitSet; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::BitSet; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundLightUpdatePacket { diff --git a/azalea-protocol/src/packets/game/clientbound_login_packet.rs b/azalea-protocol/src/packets/game/clientbound_login_packet.rs index 6ddc6b5a..6b144bac 100755 --- a/azalea-protocol/src/packets/game/clientbound_login_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_login_packet.rs @@ -1,12 +1,13 @@ -use azalea_core::{game_type::GameType, resource_location::ResourceLocation, GlobalPos}; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_core::{GameType, GlobalPos, OptionalGameType, ResourceLocation}; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundLoginPacket { pub player_id: u32, pub hardcore: bool, pub game_type: GameType, - pub previous_game_type: Option, + pub previous_game_type: OptionalGameType, pub levels: Vec, pub registry_holder: azalea_nbt::Tag, pub dimension_type: ResourceLocation, diff --git a/azalea-protocol/src/packets/game/clientbound_move_entity_pos_packet.rs b/azalea-protocol/src/packets/game/clientbound_move_entity_pos_packet.rs index 714917b7..3dc789a9 100644 --- a/azalea-protocol/src/packets/game/clientbound_move_entity_pos_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_move_entity_pos_packet.rs @@ -1,5 +1,6 @@ -use azalea_core::EntityPos; -use packet_macros::{GamePacket, McBuf}; +use azalea_core::{EntityPos, PositionDelta}; +use packet_macros::GamePacket; +use azalea_buf::McBuf; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundMoveEntityPosPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_move_entity_posrot_packet.rs b/azalea-protocol/src/packets/game/clientbound_move_entity_posrot_packet.rs index b02a2981..dd1d96e1 100644 --- a/azalea-protocol/src/packets/game/clientbound_move_entity_posrot_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_move_entity_posrot_packet.rs @@ -1,5 +1,6 @@ -use super::clientbound_move_entity_pos_packet::PositionDelta; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_core::PositionDelta; +use packet_macros::GamePacket; /// This packet is sent by the server when an entity moves less then 8 blocks. #[derive(Clone, Debug, McBuf, GamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_move_entity_rot_packet.rs b/azalea-protocol/src/packets/game/clientbound_move_entity_rot_packet.rs index c8d0170b..85515628 100644 --- a/azalea-protocol/src/packets/game/clientbound_move_entity_rot_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_move_entity_rot_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundMoveEntityRotPacket { 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 c3387f7f..d351ab04 100755 --- a/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_abilities_packet.rs @@ -1,5 +1,6 @@ -use crate::mc_buf::{McBufReadable, McBufWritable, Readable}; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_buf::{McBufReadable, McBufWritable, Readable}; +use packet_macros::GamePacket; use std::io::{Read, Write}; #[derive(Clone, Debug, McBuf, GamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs index e6941f25..4aac93f4 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_chat_packet.rs @@ -1,6 +1,7 @@ +use azalea_buf::McBuf; use azalea_chat::component::Component; use azalea_crypto::SaltSignaturePair; -use packet_macros::{GamePacket, McBuf}; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundPlayerChatPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs index cb17f1f5..f216fde8 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs @@ -1,6 +1,7 @@ -use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use azalea_buf::McBuf; +use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; use azalea_chat::component::Component; -use packet_macros::{GamePacket, McBuf}; +use packet_macros::GamePacket; use std::io::{Read, Write}; use uuid::Uuid; diff --git a/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs b/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs index 0457269a..2c5504fa 100644 --- a/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_player_position_packet.rs @@ -1,5 +1,6 @@ -use crate::mc_buf::{McBufReadable, McBufWritable, Readable}; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_buf::{McBufReadable, McBufWritable, Readable}; +use packet_macros::GamePacket; use std::io::{Read, Write}; #[derive(Clone, Debug, McBuf, GamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs b/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs index e76504cc..a79bfcf1 100644 --- a/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_recipe_packet.rs @@ -1,6 +1,7 @@ -use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; -use azalea_core::resource_location::ResourceLocation; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use azalea_core::ResourceLocation; +use packet_macros::GamePacket; use std::io::{Read, Write}; #[derive(Clone, Debug, McBuf, GamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_remove_entities_packet.rs b/azalea-protocol/src/packets/game/clientbound_remove_entities_packet.rs index 8f51596d..8c76ec15 100644 --- a/azalea-protocol/src/packets/game/clientbound_remove_entities_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_remove_entities_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundRemoveEntitiesPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_rotate_head_packet.rs b/azalea-protocol/src/packets/game/clientbound_rotate_head_packet.rs index 71b485ae..dc7ec881 100644 --- a/azalea-protocol/src/packets/game/clientbound_rotate_head_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_rotate_head_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundRotateHeadPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_section_blocks_update_packet.rs b/azalea-protocol/src/packets/game/clientbound_section_blocks_update_packet.rs index 6c429edb..6ee27ed1 100644 --- a/azalea-protocol/src/packets/game/clientbound_section_blocks_update_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_section_blocks_update_packet.rs @@ -1,6 +1,7 @@ -use crate::mc_buf::{McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable}; +use azalea_buf::McBuf; +use azalea_buf::{McBufReadable, McBufVarReadable, McBufVarWritable, McBufWritable}; use azalea_core::{ChunkSectionBlockPos, ChunkSectionPos}; -use packet_macros::{GamePacket, McBuf}; +use packet_macros::GamePacket; use std::io::{Read, Write}; #[derive(Clone, Debug, McBuf, GamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_server_data_packet.rs b/azalea-protocol/src/packets/game/clientbound_server_data_packet.rs index 4c2d94e6..1dddfc1e 100644 --- a/azalea-protocol/src/packets/game/clientbound_server_data_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_server_data_packet.rs @@ -1,5 +1,6 @@ +use azalea_buf::McBuf; use azalea_chat::component::Component; -use packet_macros::{GamePacket, McBuf}; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundServerDataPacket { 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 index 003b6ccc..a4ecaaab 100755 --- a/azalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_carried_item_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; /// Sent to change the player's slot selection. #[derive(Clone, Debug, McBuf, GamePacket)] diff --git a/azalea-protocol/src/packets/game/clientbound_set_chunk_cache_center_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_chunk_cache_center_packet.rs index 7557c16b..ee86ec9d 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_chunk_cache_center_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_chunk_cache_center_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundSetChunkCacheCenterPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_set_default_spawn_position_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_default_spawn_position_packet.rs index 7ac42c5c..9e9a7b87 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_default_spawn_position_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_default_spawn_position_packet.rs @@ -1,5 +1,6 @@ +use azalea_buf::McBuf; use azalea_core::BlockPos; -use packet_macros::{GamePacket, McBuf}; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundSetDefaultSpawnPositionPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_set_display_chat_preview_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_display_chat_preview_packet.rs index 46a0d582..8e2cfe70 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_display_chat_preview_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_display_chat_preview_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundSetDisplayChatPreviewPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_set_entity_data_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_entity_data_packet.rs index 8a568689..cc76ddf1 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_entity_data_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_entity_data_packet.rs @@ -1,5 +1,6 @@ -use crate::mc_buf::EntityMetadata; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_entity::EntityMetadata; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundSetEntityDataPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_set_entity_link_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_entity_link_packet.rs index e6e3af67..ec1ee0ec 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_entity_link_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_entity_link_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundSetEntityLinkPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_set_equipment_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_equipment_packet.rs index 3acbd58f..aa352189 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_equipment_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_equipment_packet.rs @@ -1,7 +1,8 @@ +use azalea_buf::McBuf; use azalea_core::Slot; -use packet_macros::{GamePacket, McBuf}; +use packet_macros::GamePacket; -use crate::mc_buf::{McBufReadable, McBufWritable}; +use azalea_buf::{McBufReadable, McBufWritable}; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundSetEquipmentPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_set_experience_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_experience_packet.rs index bcb6393d..7387f6dc 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_experience_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_experience_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundSetExperiencePacket { diff --git a/azalea-protocol/src/packets/game/clientbound_set_health_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_health_packet.rs index 6c75cf63..b99fe86a 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_health_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_health_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundSetHealthPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_set_time_packet.rs b/azalea-protocol/src/packets/game/clientbound_set_time_packet.rs index 4cad0693..ea4437b7 100644 --- a/azalea-protocol/src/packets/game/clientbound_set_time_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_set_time_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundSetTimePacket { diff --git a/azalea-protocol/src/packets/game/clientbound_sound_packet.rs b/azalea-protocol/src/packets/game/clientbound_sound_packet.rs index fbc5830b..a8607599 100644 --- a/azalea-protocol/src/packets/game/clientbound_sound_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_sound_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundSoundPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_system_chat_packet.rs b/azalea-protocol/src/packets/game/clientbound_system_chat_packet.rs index dfa75a5b..c531fa1e 100644 --- a/azalea-protocol/src/packets/game/clientbound_system_chat_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_system_chat_packet.rs @@ -1,5 +1,6 @@ +use azalea_buf::McBuf; use azalea_chat::component::Component; -use packet_macros::{GamePacket, McBuf}; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundSystemChatPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_teleport_entity_packet.rs b/azalea-protocol/src/packets/game/clientbound_teleport_entity_packet.rs index c10db7b9..433b0727 100644 --- a/azalea-protocol/src/packets/game/clientbound_teleport_entity_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_teleport_entity_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundTeleportEntityPacket { diff --git a/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs index daa1ac93..43e4d69f 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_advancements_packet.rs @@ -1,7 +1,7 @@ -use crate::packets::{McBufReadable, McBufWritable}; +use azalea_buf::{McBuf, McBufReadable, McBufWritable}; use azalea_chat::component::Component; -use azalea_core::{resource_location::ResourceLocation, Slot}; -use packet_macros::{GamePacket, McBuf}; +use azalea_core::{ResourceLocation, Slot}; +use packet_macros::GamePacket; use std::{ collections::HashMap, io::{Read, Write}, diff --git a/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs b/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs index d0e7c9ee..3eca2a84 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs @@ -1,6 +1,7 @@ -use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; -use azalea_core::resource_location::ResourceLocation; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use azalea_core::ResourceLocation; +use packet_macros::GamePacket; use std::io::{Read, Write}; use uuid::Uuid; 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 27839919..05d57695 100644 --- a/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_recipes_packet.rs @@ -1,9 +1,10 @@ use std::io::{Read, Write}; -use azalea_core::{resource_location::ResourceLocation, Slot}; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_core::{ResourceLocation, Slot}; +use packet_macros::GamePacket; -use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundUpdateRecipesPacket { @@ -129,8 +130,8 @@ impl McBufWritable for Recipe { impl McBufReadable for Recipe { fn read_into(buf: &mut impl Read) -> Result { - let recipe_type = buf.read_resource_location()?; - let identifier = buf.read_resource_location()?; + let recipe_type = ResourceLocation::read_into(buf)?; + let identifier = ResourceLocation::read_into(buf)?; // rust doesn't let us match ResourceLocation so we have to do a big // if-else chain :( 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 60794f03..cf3bc069 100755 --- a/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_update_tags_packet.rs @@ -1,6 +1,8 @@ -use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable}; -use azalea_core::resource_location::ResourceLocation; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; +use azalea_core::ResourceLocation; +use packet_macros::GamePacket; +use std::ops::Deref; use std::{ collections::HashMap, io::{Read, Write}, @@ -8,7 +10,7 @@ use std::{ #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundUpdateTagsPacket { - pub tags: HashMap>, + pub tags: TagMap, } #[derive(Clone, Debug)] @@ -17,12 +19,15 @@ pub struct Tags { pub elements: Vec, } -impl McBufReadable for HashMap> { +#[derive(Clone, Debug)] +pub struct TagMap(HashMap>); + +impl McBufReadable for TagMap { fn read_into(buf: &mut impl Read) -> Result { let length = buf.read_varint()? as usize; let mut data = HashMap::with_capacity(length); for _ in 0..length { - let tag_type = buf.read_resource_location()?; + let tag_type = ResourceLocation::read_into(buf)?; let tags_count = buf.read_varint()? as usize; let mut tags_vec = Vec::with_capacity(tags_count); for _ in 0..tags_count { @@ -31,14 +36,14 @@ impl McBufReadable for HashMap> { } data.insert(tag_type, tags_vec); } - Ok(data) + Ok(TagMap(data)) } } -impl McBufWritable for HashMap> { +impl McBufWritable for TagMap { fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { buf.write_varint(self.len() as i32)?; - for (k, v) in self { + for (k, v) in &self.0 { k.write_into(buf)?; v.write_into(buf)?; } @@ -47,7 +52,7 @@ impl McBufWritable for HashMap> { } impl McBufReadable for Tags { fn read_into(buf: &mut impl Read) -> Result { - let name = buf.read_resource_location()?; + let name = ResourceLocation::read_into(buf)?; let elements = buf.read_int_id_list()?; Ok(Tags { name, elements }) } @@ -60,3 +65,11 @@ impl McBufWritable for Tags { Ok(()) } } + +impl Deref for TagMap { + type Target = HashMap>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} 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 8288bd73..c0c40f75 100755 --- 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,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundUpdateViewDistancePacket { diff --git a/azalea-protocol/src/packets/game/serverbound_chat_command_packet.rs b/azalea-protocol/src/packets/game/serverbound_chat_command_packet.rs index 9ae0b79f..6371463b 100644 --- a/azalea-protocol/src/packets/game/serverbound_chat_command_packet.rs +++ b/azalea-protocol/src/packets/game/serverbound_chat_command_packet.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ServerboundChatCommandPacket { diff --git a/azalea-protocol/src/packets/game/serverbound_chat_preview_packet.rs b/azalea-protocol/src/packets/game/serverbound_chat_preview_packet.rs index 60535f69..32711d45 100644 --- a/azalea-protocol/src/packets/game/serverbound_chat_preview_packet.rs +++ b/azalea-protocol/src/packets/game/serverbound_chat_preview_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ServerboundChatPreviewPacket { diff --git a/azalea-protocol/src/packets/game/serverbound_custom_payload_packet.rs b/azalea-protocol/src/packets/game/serverbound_custom_payload_packet.rs index bef25b59..eecb920b 100644 --- a/azalea-protocol/src/packets/game/serverbound_custom_payload_packet.rs +++ b/azalea-protocol/src/packets/game/serverbound_custom_payload_packet.rs @@ -1,6 +1,7 @@ -use crate::mc_buf::UnsizedByteArray; -use azalea_core::resource_location::ResourceLocation; -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use azalea_buf::UnsizedByteArray; +use azalea_core::ResourceLocation; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ServerboundCustomPayloadPacket { diff --git a/azalea-protocol/src/packets/game/serverbound_keep_alive_packet.rs b/azalea-protocol/src/packets/game/serverbound_keep_alive_packet.rs index c430499e..1edc5a52 100644 --- a/azalea-protocol/src/packets/game/serverbound_keep_alive_packet.rs +++ b/azalea-protocol/src/packets/game/serverbound_keep_alive_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ServerboundKeepAlivePacket { diff --git a/azalea-protocol/src/packets/game/serverbound_move_player_packet_pos.rs b/azalea-protocol/src/packets/game/serverbound_move_player_packet_pos.rs index 65b28624..23f4050e 100644 --- a/azalea-protocol/src/packets/game/serverbound_move_player_packet_pos.rs +++ b/azalea-protocol/src/packets/game/serverbound_move_player_packet_pos.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ServerboundMovePlayerPacketPos { diff --git a/azalea-protocol/src/packets/game/serverbound_move_player_packet_pos_rot.rs b/azalea-protocol/src/packets/game/serverbound_move_player_packet_pos_rot.rs index 8070d1bf..9416461c 100644 --- a/azalea-protocol/src/packets/game/serverbound_move_player_packet_pos_rot.rs +++ b/azalea-protocol/src/packets/game/serverbound_move_player_packet_pos_rot.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ServerboundMovePlayerPacketPosRot { diff --git a/azalea-protocol/src/packets/game/serverbound_move_player_packet_rot.rs b/azalea-protocol/src/packets/game/serverbound_move_player_packet_rot.rs index 0c444d03..0952483a 100644 --- a/azalea-protocol/src/packets/game/serverbound_move_player_packet_rot.rs +++ b/azalea-protocol/src/packets/game/serverbound_move_player_packet_rot.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ServerboundMovePlayerPacketRot { diff --git a/azalea-protocol/src/packets/game/serverbound_move_player_packet_status_only.rs b/azalea-protocol/src/packets/game/serverbound_move_player_packet_status_only.rs index ef762caf..fb765c27 100644 --- a/azalea-protocol/src/packets/game/serverbound_move_player_packet_status_only.rs +++ b/azalea-protocol/src/packets/game/serverbound_move_player_packet_status_only.rs @@ -1,4 +1,5 @@ -use packet_macros::{GamePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ServerboundMovePlayerPacketStatusOnly { diff --git a/azalea-protocol/src/packets/handshake/client_intention_packet.rs b/azalea-protocol/src/packets/handshake/client_intention_packet.rs index 410c11ab..5f1987e2 100755 --- a/azalea-protocol/src/packets/handshake/client_intention_packet.rs +++ b/azalea-protocol/src/packets/handshake/client_intention_packet.rs @@ -1,5 +1,6 @@ use crate::packets::ConnectionProtocol; -use packet_macros::{HandshakePacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::HandshakePacket; use std::hash::Hash; #[derive(Hash, Clone, Debug, McBuf, HandshakePacket)] 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 1b1da87a..05310fb0 100755 --- a/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_custom_query_packet.rs @@ -1,6 +1,6 @@ -use crate::mc_buf::UnsizedByteArray; -use azalea_core::resource_location::ResourceLocation; -use packet_macros::{LoginPacket, McBuf}; +use azalea_buf::{McBuf, UnsizedByteArray}; +use azalea_core::ResourceLocation; +use packet_macros::LoginPacket; use std::hash::Hash; #[derive(Hash, Clone, Debug, McBuf, LoginPacket)] 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 dd11f7f7..bd2edf38 100755 --- a/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_game_profile_packet.rs @@ -1,10 +1,8 @@ use std::io::{Read, Write}; use super::LoginPacket; -use crate::mc_buf::McBufReadable; -use crate::mc_buf::{Readable, Writable}; use azalea_auth::game_profile::GameProfile; -use azalea_core::serializable_uuid::SerializableUuid; +use azalea_buf::{McBufReadable, Readable, SerializableUuid, Writable}; use uuid::Uuid; #[derive(Clone, Debug)] diff --git a/azalea-protocol/src/packets/login/clientbound_hello_packet.rs b/azalea-protocol/src/packets/login/clientbound_hello_packet.rs index 58d48ffe..f3724c18 100755 --- a/azalea-protocol/src/packets/login/clientbound_hello_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_hello_packet.rs @@ -1,5 +1,5 @@ +use azalea_buf::McBuf; use packet_macros::LoginPacket; -use packet_macros::McBuf; #[derive(Clone, Debug, McBuf, LoginPacket)] pub struct ClientboundHelloPacket { 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 b1323f50..19b2d58e 100755 --- a/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_login_compression_packet.rs @@ -3,7 +3,7 @@ use std::{ io::{Read, Write}, }; -use crate::mc_buf::{Readable, Writable}; +use azalea_buf::{Readable, Writable}; use super::LoginPacket; diff --git a/azalea-protocol/src/packets/login/clientbound_login_disconnect_packet.rs b/azalea-protocol/src/packets/login/clientbound_login_disconnect_packet.rs index 9ab09e3b..acc68c82 100644 --- a/azalea-protocol/src/packets/login/clientbound_login_disconnect_packet.rs +++ b/azalea-protocol/src/packets/login/clientbound_login_disconnect_packet.rs @@ -1,5 +1,6 @@ +use azalea_buf::McBuf; use azalea_chat::component::Component; -use packet_macros::{LoginPacket, McBuf}; +use packet_macros::LoginPacket; #[derive(Clone, Debug, McBuf, LoginPacket)] pub struct ClientboundLoginDisconnectPacket { diff --git a/azalea-protocol/src/packets/login/serverbound_hello_packet.rs b/azalea-protocol/src/packets/login/serverbound_hello_packet.rs index bed93f04..57a3e4d8 100755 --- a/azalea-protocol/src/packets/login/serverbound_hello_packet.rs +++ b/azalea-protocol/src/packets/login/serverbound_hello_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{LoginPacket, McBuf}; +use azalea_buf::McBuf; +use packet_macros::LoginPacket; #[derive(Clone, Debug, McBuf, LoginPacket)] pub struct ServerboundHelloPacket { diff --git a/azalea-protocol/src/packets/login/serverbound_key_packet.rs b/azalea-protocol/src/packets/login/serverbound_key_packet.rs index d57b122a..12b7c4ec 100644 --- a/azalea-protocol/src/packets/login/serverbound_key_packet.rs +++ b/azalea-protocol/src/packets/login/serverbound_key_packet.rs @@ -1,8 +1,9 @@ +use azalea_buf::McBuf; use azalea_crypto::SaltSignaturePair; -use packet_macros::{LoginPacket, McBuf}; +use packet_macros::LoginPacket; use std::io::{Read, Write}; -use crate::mc_buf::{McBufReadable, McBufWritable}; +use azalea_buf::{McBufReadable, McBufWritable}; #[derive(Clone, Debug, McBuf, LoginPacket)] pub struct ServerboundKeyPacket { diff --git a/azalea-protocol/src/packets/mod.rs b/azalea-protocol/src/packets/mod.rs index 1cc79b79..738baa09 100755 --- a/azalea-protocol/src/packets/mod.rs +++ b/azalea-protocol/src/packets/mod.rs @@ -3,14 +3,11 @@ pub mod handshake; pub mod login; pub mod status; -use std::io::{Read, Write}; - -use crate::{ - connect::PacketFlow, - mc_buf::{McBufReadable, McBufWritable, Readable, Writable}, -}; +use crate::connect::PacketFlow; +use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; use num_derive::FromPrimitive; use num_traits::FromPrimitive; +use std::io::{Read, Write}; pub const PROTOCOL_VERSION: u32 = 759; @@ -43,7 +40,7 @@ where fn write(&self, buf: &mut impl Write) -> Result<(), std::io::Error>; } -impl crate::mc_buf::McBufReadable for ConnectionProtocol { +impl azalea_buf::McBufReadable for ConnectionProtocol { fn read_into(buf: &mut impl Read) -> Result { ConnectionProtocol::from_i32(buf.read_varint()?) .ok_or_else(|| "Invalid intention".to_string()) 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 2acdb8c2..e7fb4f2b 100755 --- a/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs +++ b/azalea-protocol/src/packets/status/clientbound_status_response_packet.rs @@ -4,7 +4,7 @@ use azalea_chat::component::Component; use serde::Deserialize; use serde_json::Value; -use crate::mc_buf::Readable; +use azalea_buf::Readable; use super::StatusPacket; 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 3369e6a9..c19d7795 100755 --- a/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs +++ b/azalea-protocol/src/packets/status/serverbound_status_request_packet.rs @@ -1,4 +1,5 @@ -use packet_macros::{McBuf, StatusPacket}; +use azalea_buf::McBuf; +use packet_macros::StatusPacket; #[derive(Clone, Debug, McBuf, StatusPacket)] pub struct ServerboundStatusRequestPacket {} diff --git a/azalea-protocol/src/read.rs b/azalea-protocol/src/read.rs index b4ba17ea..e19b5538 100755 --- a/azalea-protocol/src/read.rs +++ b/azalea-protocol/src/read.rs @@ -1,8 +1,5 @@ -use crate::{ - connect::PacketFlow, - mc_buf::{read_varint_async, Readable}, - packets::ProtocolPacket, -}; +use crate::{connect::PacketFlow, packets::ProtocolPacket}; +use azalea_buf::{read_varint_async, Readable}; use azalea_crypto::Aes128CfbDec; use flate2::read::ZlibDecoder; use std::{ diff --git a/azalea-protocol/src/write.rs b/azalea-protocol/src/write.rs index 9291681c..ae9a6829 100755 --- a/azalea-protocol/src/write.rs +++ b/azalea-protocol/src/write.rs @@ -1,5 +1,6 @@ -use crate::{mc_buf::Writable, packets::ProtocolPacket, read::MAXIMUM_UNCOMPRESSED_LENGTH}; +use crate::{packets::ProtocolPacket, read::MAXIMUM_UNCOMPRESSED_LENGTH}; use async_compression::tokio::bufread::ZlibEncoder; +use azalea_buf::Writable; use azalea_crypto::Aes128CfbEnc; use std::fmt::Debug; use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt}; diff --git a/azalea-world/Cargo.toml b/azalea-world/Cargo.toml index 96942138..02f05b46 100644 --- a/azalea-world/Cargo.toml +++ b/azalea-world/Cargo.toml @@ -7,10 +7,10 @@ version = "0.1.0" [dependencies] azalea-block = {path = "../azalea-block"} +azalea-buf = {path = "../azalea-buf"} azalea-core = {path = "../azalea-core"} azalea-entity = {path = "../azalea-entity"} azalea-nbt = {path = "../azalea-nbt"} -azalea-protocol = {path = "../azalea-protocol"} log = "0.4.17" nohash-hasher = "0.2.0" diff --git a/azalea-world/src/chunk.rs b/azalea-world/src/chunk.rs index 77fa8786..27ef1ca7 100644 --- a/azalea-world/src/chunk.rs +++ b/azalea-world/src/chunk.rs @@ -2,8 +2,8 @@ use crate::palette::PalettedContainer; use crate::palette::PalettedContainerType; use crate::World; use azalea_block::BlockState; +use azalea_buf::{McBufReadable, McBufWritable}; use azalea_core::{BlockPos, ChunkBlockPos, ChunkPos, ChunkSectionBlockPos}; -use azalea_protocol::mc_buf::{McBufReadable, McBufWritable}; use std::fmt::Debug; use std::{ io::{Read, Write}, diff --git a/azalea-world/src/lib.rs b/azalea-world/src/lib.rs index 6e922bb9..cbc5c633 100644 --- a/azalea-world/src/lib.rs +++ b/azalea-world/src/lib.rs @@ -72,21 +72,21 @@ impl World { Ok(()) } - pub fn move_entity_with_delta(&mut self, entity_id: u32, delta: PositionDelta) -> Result<(), String> { - let entity = self - .entity_storage - .get_mut_by_id(entity_id) - .ok_or_else(|| "Moving entity that doesn't exist".to_string())?; - let old_chunk = ChunkPos::from(entity.pos()); - let new_chunk = ChunkPos::from(&new_pos); - // this is fine because we update the chunk below - entity.unsafe_move(new_pos); - if old_chunk != new_chunk { - self.entity_storage - .update_entity_chunk(entity_id, &old_chunk, &new_chunk); - } - Ok(()) - } + // pub fn move_entity_with_delta(&mut self, entity_id: u32, delta: PositionDelta) -> Result<(), String> { + // let entity = self + // .entity_storage + // .get_mut_by_id(entity_id) + // .ok_or_else(|| "Moving entity that doesn't exist".to_string())?; + // let old_chunk = ChunkPos::from(entity.pos()); + // let new_chunk = ChunkPos::from(&new_pos); + // // this is fine because we update the chunk below + // entity.unsafe_move(new_pos); + // if old_chunk != new_chunk { + // self.entity_storage + // .update_entity_chunk(entity_id, &old_chunk, &new_chunk); + // } + // Ok(()) + // } pub fn add_entity(&mut self, entity: Entity) { self.entity_storage.insert(entity); diff --git a/azalea-world/src/palette.rs b/azalea-world/src/palette.rs index be6f022c..4779dc4b 100644 --- a/azalea-world/src/palette.rs +++ b/azalea-world/src/palette.rs @@ -1,4 +1,4 @@ -use azalea_protocol::mc_buf::{McBufReadable, McBufVarReadable, McBufWritable, Readable, Writable}; +use azalea_buf::{McBufReadable, McBufVarReadable, McBufWritable, Readable, Writable}; use std::io::{Read, Write}; use crate::BitStorage; -- cgit v1.2.3 From 2cdbdcaa27c812e569c7c1a13d83182446e7f18b Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 23 Jun 2022 21:31:19 -0500 Subject: move_entity_with_delta --- azalea-client/src/client.rs | 14 +++----- azalea-core/src/delta.rs | 10 +++--- azalea-core/src/resource_location.rs | 3 +- azalea-entity/src/lib.rs | 2 +- .../game/clientbound_move_entity_pos_packet.rs | 6 ++-- azalea-world/src/lib.rs | 40 +++++++++++++--------- bot/src/main.rs | 2 +- 7 files changed, 42 insertions(+), 35 deletions(-) (limited to 'azalea-core/src/delta.rs') diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index a7841637..6efe521b 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -433,20 +433,16 @@ impl Client { // println!("Got rotate head packet {:?}", p); } GamePacket::ClientboundMoveEntityPosPacket(p) => { - // println!("Got move entity pos packet {:?}", p); + let mut state_lock = state.lock()?; + let world = state_lock.world.as_mut().unwrap(); + + world.move_entity_with_delta(p.entity_id, &p.delta)?; } GamePacket::ClientboundMoveEntityPosRotPacket(p) => { let mut state_lock = state.lock()?; let world = state_lock.world.as_mut().unwrap(); - // world.move_entity( - // p.entity_id, - // EntityPos { - // x: p.x, - // y: p.y, - // z: p.z, - // }, - // )?; + world.move_entity_with_delta(p.entity_id, &p.delta)?; } GamePacket::ClientboundMoveEntityRotPacket(p) => { println!("Got move entity rot packet {:?}", p); diff --git a/azalea-core/src/delta.rs b/azalea-core/src/delta.rs index 339e52cd..41923ffb 100644 --- a/azalea-core/src/delta.rs +++ b/azalea-core/src/delta.rs @@ -20,10 +20,12 @@ impl PositionDelta { } impl EntityPos { - pub fn apply_delta(&mut self, delta: &PositionDelta) { + pub fn with_delta(&self, delta: &PositionDelta) -> EntityPos { let (x, y, z) = delta.float(); - self.x += x; - self.y += y; - self.z += z; + EntityPos { + x: self.x + x, + y: self.y + y, + z: self.z + z, + } } } diff --git a/azalea-core/src/resource_location.rs b/azalea-core/src/resource_location.rs index 61ae8a20..acca0c58 100755 --- a/azalea-core/src/resource_location.rs +++ b/azalea-core/src/resource_location.rs @@ -93,7 +93,8 @@ mod tests { let mut buf = Vec::new(); ResourceLocation::new("minecraft:dirt") .unwrap() - .write_into(&mut buf)?; + .write_into(&mut buf) + .unwrap(); let mut buf = Cursor::new(buf); diff --git a/azalea-entity/src/lib.rs b/azalea-entity/src/lib.rs index 93ed5ea8..065413a5 100644 --- a/azalea-entity/src/lib.rs +++ b/azalea-entity/src/lib.rs @@ -14,7 +14,7 @@ pub struct Entity { /// The incrementing numerical id of the entity. pub id: u32, pub uuid: Uuid, - pub pos: EntityPos, + pos: EntityPos, } impl Entity { diff --git a/azalea-protocol/src/packets/game/clientbound_move_entity_pos_packet.rs b/azalea-protocol/src/packets/game/clientbound_move_entity_pos_packet.rs index 3dc789a9..63428dd8 100644 --- a/azalea-protocol/src/packets/game/clientbound_move_entity_pos_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_move_entity_pos_packet.rs @@ -1,11 +1,11 @@ -use azalea_core::{EntityPos, PositionDelta}; -use packet_macros::GamePacket; use azalea_buf::McBuf; +use azalea_core::PositionDelta; +use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundMoveEntityPosPacket { #[var] - pub entity_id: i32, + pub entity_id: u32, pub delta: PositionDelta, pub on_ground: bool, } diff --git a/azalea-world/src/lib.rs b/azalea-world/src/lib.rs index cbc5c633..bc73c13d 100644 --- a/azalea-world/src/lib.rs +++ b/azalea-world/src/lib.rs @@ -6,7 +6,7 @@ mod entity; mod palette; use azalea_block::BlockState; -use azalea_core::{BlockPos, ChunkPos, EntityPos}; +use azalea_core::{BlockPos, ChunkPos, EntityPos, PositionDelta}; use azalea_entity::Entity; pub use bit_storage::BitStorage; pub use chunk::{Chunk, ChunkStorage}; @@ -61,6 +61,7 @@ impl World { .entity_storage .get_mut_by_id(entity_id) .ok_or_else(|| "Moving entity that doesn't exist".to_string())?; + let old_chunk = ChunkPos::from(entity.pos()); let new_chunk = ChunkPos::from(&new_pos); // this is fine because we update the chunk below @@ -72,21 +73,28 @@ impl World { Ok(()) } - // pub fn move_entity_with_delta(&mut self, entity_id: u32, delta: PositionDelta) -> Result<(), String> { - // let entity = self - // .entity_storage - // .get_mut_by_id(entity_id) - // .ok_or_else(|| "Moving entity that doesn't exist".to_string())?; - // let old_chunk = ChunkPos::from(entity.pos()); - // let new_chunk = ChunkPos::from(&new_pos); - // // this is fine because we update the chunk below - // entity.unsafe_move(new_pos); - // if old_chunk != new_chunk { - // self.entity_storage - // .update_entity_chunk(entity_id, &old_chunk, &new_chunk); - // } - // Ok(()) - // } + pub fn move_entity_with_delta( + &mut self, + entity_id: u32, + delta: &PositionDelta, + ) -> Result<(), String> { + let entity = self + .entity_storage + .get_mut_by_id(entity_id) + .ok_or_else(|| "Moving entity that doesn't exist".to_string())?; + let new_pos = entity.pos().with_delta(delta); + + let old_chunk = ChunkPos::from(entity.pos()); + let new_chunk = ChunkPos::from(&new_pos); + // this is fine because we update the chunk below + + entity.unsafe_move(new_pos); + if old_chunk != new_chunk { + self.entity_storage + .update_entity_chunk(entity_id, &old_chunk, &new_chunk); + } + Ok(()) + } pub fn add_entity(&mut self, entity: Entity) { self.entity_storage.insert(entity); diff --git a/bot/src/main.rs b/bot/src/main.rs index 1cb209e5..02f802a5 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -6,7 +6,7 @@ async fn main() -> Result<(), Box> { println!("Hello, world!"); // let address = "95.111.249.143:10000"; - let address = "localhost:52722"; + let address = "localhost:49982"; // let response = azalea_client::ping::ping_server(&address.try_into().unwrap()) // .await // .unwrap(); -- cgit v1.2.3 From b030b0ea330c674415f7e30634957167b2fa6a6d Mon Sep 17 00:00:00 2001 From: mat Date: Fri, 24 Jun 2022 23:10:59 -0500 Subject: start adding moving --- Cargo.lock | 2 + azalea-auth/Cargo.toml | 2 +- azalea-client/Cargo.toml | 1 + azalea-client/src/account.rs | 4 +- azalea-client/src/client.rs | 153 +++++++++++++++++--- azalea-client/src/movement.rs | 24 +++- azalea-client/src/player.rs | 24 +++- azalea-core/src/delta.rs | 59 ++++++-- azalea-core/src/position.rs | 157 ++++++++++++++------- azalea-entity/src/lib.rs | 30 +++- .../game/clientbound_move_entity_pos_packet.rs | 4 +- .../game/clientbound_move_entity_posrot_packet.rs | 4 +- azalea-world/Cargo.toml | 1 + azalea-world/src/entity.rs | 27 +++- azalea-world/src/lib.rs | 13 +- bot/src/main.rs | 52 ++++--- 16 files changed, 433 insertions(+), 124 deletions(-) (limited to 'azalea-core/src/delta.rs') diff --git a/Cargo.lock b/Cargo.lock index aeea0fb1..b9a60580 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,6 +122,7 @@ dependencies = [ "azalea-world", "owning_ref", "tokio", + "uuid", ] [[package]] @@ -219,6 +220,7 @@ dependencies = [ "azalea-nbt", "log", "nohash-hasher", + "uuid", ] [[package]] diff --git a/azalea-auth/Cargo.toml b/azalea-auth/Cargo.toml index a7a11b53..fe306186 100755 --- a/azalea-auth/Cargo.toml +++ b/azalea-auth/Cargo.toml @@ -6,4 +6,4 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -uuid = "^1.1.2" +uuid = "1.1.2" diff --git a/azalea-client/Cargo.toml b/azalea-client/Cargo.toml index 46ea8039..b39d6a49 100755 --- a/azalea-client/Cargo.toml +++ b/azalea-client/Cargo.toml @@ -14,3 +14,4 @@ azalea-protocol = {path = "../azalea-protocol"} azalea-world = {path = "../azalea-world"} owning_ref = "0.4.1" tokio = {version = "1.19.2", features = ["sync"]} +uuid = "1.1.2" diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index 0bcad630..56f4918a 100644 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -1,8 +1,8 @@ +//! Connect to Minecraft servers. + use crate::Client; use azalea_protocol::ServerAddress; -///! Connect to Minecraft servers. - /// Something that can join Minecraft servers. pub struct Account { pub username: String, diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 6efe521b..943e0f9f 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -1,5 +1,6 @@ use crate::{Account, Player}; -use azalea_core::{ChunkPos, EntityPos, ResourceLocation}; +use azalea_auth::game_profile::GameProfile; +use azalea_core::{ChunkPos, EntityPos, PositionDelta, PositionDeltaTrait, ResourceLocation}; use azalea_entity::Entity; use azalea_protocol::{ connect::{GameConnection, HandshakeConnection}, @@ -63,6 +64,7 @@ pub enum ChatPacket { /// A player that you can control that is currently in a Minecraft server. pub struct Client { event_receiver: UnboundedReceiver, + game_profile: GameProfile, pub conn: Arc>, pub state: Arc>, // game_loop @@ -104,7 +106,7 @@ impl Client { ) .await; - let conn = loop { + let (conn, game_profile) = loop { let packet_result = conn.read().await; match packet_result { Ok(packet) => match packet { @@ -132,7 +134,7 @@ impl Client { } LoginPacket::ClientboundGameProfilePacket(p) => { println!("Got profile {:?}", p.game_profile); - break conn.game(); + break (conn.game(), p.game_profile); } LoginPacket::ClientboundLoginDisconnectPacket(p) => { println!("Got disconnect {:?}", p); @@ -154,6 +156,7 @@ impl Client { // we got the GameConnection, so the server is now connected :) let client = Client { + game_profile: game_profile.clone(), event_receiver: rx, conn: conn.clone(), state: Arc::new(Mutex::new(ClientState::default())), @@ -167,6 +170,7 @@ impl Client { conn.clone(), tx.clone(), game_loop_state.clone(), + game_profile.clone(), )); tokio::spawn(Self::game_tick_loop(conn, tx, game_loop_state)); @@ -177,21 +181,24 @@ impl Client { conn: Arc>, tx: UnboundedSender, state: Arc>, + game_profile: GameProfile, ) { loop { let r = conn.lock().await.read().await; match r { - Ok(packet) => match Self::handle(&packet, &tx, &state, &conn).await { - Ok(_) => {} - Err(e) => { - println!("Error handling packet: {:?}", e); - if IGNORE_ERRORS { - continue; - } else { - panic!("Error handling packet: {:?}", e); + Ok(packet) => { + match Self::handle(&packet, &tx, &state, &conn, &game_profile).await { + Ok(_) => {} + Err(e) => { + println!("Error handling packet: {:?}", e); + if IGNORE_ERRORS { + continue; + } else { + panic!("Error handling packet: {:?}", e); + } } } - }, + } Err(e) => { if IGNORE_ERRORS { println!("Error: {:?}", e); @@ -211,13 +218,14 @@ impl Client { tx: &UnboundedSender, state: &Arc>, conn: &Arc>, + game_profile: &GameProfile, ) -> Result<(), HandleError> { match packet { GamePacket::ClientboundLoginPacket(p) => { println!("Got login packet {:?}", p); { - let mut state = state.lock()?; + let mut state_lock = state.lock()?; // // write p into login.txt // std::io::Write::write_all( @@ -226,8 +234,6 @@ impl Client { // ) // .unwrap(); - state.player.entity.id = p.player_id; - // TODO: have registry_holder be a struct because this sucks rn // best way would be to add serde support to azalea-nbt @@ -281,7 +287,18 @@ impl Client { .as_int() .expect("min_y tag is not an int"); - state.world = Some(World::new(16, height, min_y)); + // the 16 here is our render distance + // i'll make this an actual setting later + state_lock.world = Some(World::new(16, height, min_y)); + + let entity = Entity::new(p.player_id, game_profile.uuid, EntityPos::default()); + state_lock + .world + .as_mut() + .expect("World doesn't exist! We should've gotten a login packet by now.") + .add_entity(entity); + + state_lock.player.set_entity_id(p.player_id); } conn.lock() @@ -334,6 +351,99 @@ impl Client { GamePacket::ClientboundPlayerPositionPacket(p) => { // TODO: reply with teleport confirm println!("Got player position packet {:?}", p); + + let mut state_lock = state.lock()?; + let player_entity_id = state_lock.player.entity_id; + let world = state_lock.world.as_mut().unwrap(); + let player_entity = world + .mut_entity_by_id(player_entity_id) + .expect("Player entity doesn't exist"); + let delta_movement = &player_entity.delta; + + let is_x_relative = p.relative_arguments.x; + let is_y_relative = p.relative_arguments.y; + let is_z_relative = p.relative_arguments.z; + + let (delta_x, new_pos_x) = if is_x_relative { + player_entity.old_pos.x += p.x; + (delta_movement.x(), player_entity.pos().x + p.x) + } else { + player_entity.old_pos.x = p.x; + (0.0, p.x) + }; + let (delta_y, new_pos_y) = if is_y_relative { + player_entity.old_pos.y += p.y; + (delta_movement.y(), player_entity.pos().y + p.y) + } else { + player_entity.old_pos.y = p.y; + (0.0, p.y) + }; + let (delta_z, new_pos_z) = if is_z_relative { + player_entity.old_pos.z += p.z; + (delta_movement.z(), player_entity.pos().z + p.z) + } else { + player_entity.old_pos.z = p.z; + (0.0, p.z) + }; + + let mut y_rot = p.y_rot; + let mut x_rot = p.x_rot; + if p.relative_arguments.x_rot { + y_rot += player_entity.x_rot; + } + if p.relative_arguments.y_rot { + x_rot += player_entity.y_rot; + } + + player_entity.delta = PositionDelta { + xa: delta_x, + ya: delta_y, + za: delta_z, + }; + player_entity.set_rotation(x_rot, y_rot); + // TODO: minecraft sets "xo", "yo", and "zo" here but idk what that means + // so investigate that ig + world + .move_entity( + player_entity_id, + EntityPos { + x: new_pos_x, + y: new_pos_y, + z: new_pos_z, + }, + ) + .expect("The player entity should always exist"); + + let mut state_lock = state.lock()?; + + let player = &state_lock.player; + let player_entity_id = player.entity_id; + + let world = state_lock.world.as_mut().unwrap(); + world.move_entity( + player_entity_id, + EntityPos { + x: p.x, + y: p.y, + z: p.z, + }, + )?; + + conn.lock() + .await + .write(ServerboundAcceptTeleportationPacket {}.get()) + .await; + conn.lock() + .await + .write( + ServerboundMovePlayerPacketPosRot { + identifier: ResourceLocation::new("brand").unwrap(), + // they don't have to know :) + data: "vanilla".into(), + } + .get(), + ) + .await; } GamePacket::ClientboundPlayerInfoPacket(p) => { println!("Got player info packet {:?}", p); @@ -534,13 +644,22 @@ impl Client { /// Gets the `World` the client is in. /// - /// This is basically a shortcut for `let world = client.state.lock().unwrap().world.as_ref().unwrap()`. + /// This is basically a shortcut for `client.state.lock().unwrap().world.as_ref().unwrap()`. /// If the client hasn't received a login packet yet, this will panic. pub fn world(&self) -> OwningRef, World> { let state_lock: std::sync::MutexGuard = self.state.lock().unwrap(); let state_lock_ref = OwningRef::new(state_lock); state_lock_ref.map(|state| state.world.as_ref().expect("World doesn't exist!")) } + + /// Gets the `Player` struct for our player. + /// + /// This is basically a shortcut for `client.state.lock().unwrap().player`. + pub fn player(&self) -> OwningRef, Player> { + let state_lock: std::sync::MutexGuard = self.state.lock().unwrap(); + let state_lock_ref = OwningRef::new(state_lock); + state_lock_ref.map(|state| &state.player) + } } impl From> for HandleError { diff --git a/azalea-client/src/movement.rs b/azalea-client/src/movement.rs index c9cd62e9..0402b15b 100644 --- a/azalea-client/src/movement.rs +++ b/azalea-client/src/movement.rs @@ -4,15 +4,29 @@ use azalea_protocol::packets::game::serverbound_move_player_packet_pos_rot::Serv impl Client { /// Set the client's position to the given coordinates. - pub async fn move_to(&mut self, pos: &EntityPos) { + pub async fn move_to(&mut self, new_pos: EntityPos) -> Result<(), String> { + let mut state_lock = self.state.lock().unwrap(); + + let world = state_lock.world.as_ref().unwrap(); + + let player = &state_lock.player; + let player_id = if let Some(player) = player.entity(world) { + player.id + } else { + return Err("Player entity not found".to_string()); + }; + + let world = state_lock.world.as_mut().unwrap(); + world.move_entity(player_id, new_pos)?; + self.conn .lock() .await .write( ServerboundMovePlayerPacketPosRot { - x: pos.x, - y: pos.y, - z: pos.z, + x: new_pos.x, + y: new_pos.y, + z: new_pos.z, x_rot: 0.0, y_rot: 0.0, on_ground: false, @@ -20,5 +34,7 @@ impl Client { .get(), ) .await; + + Ok(()) } } diff --git a/azalea-client/src/player.rs b/azalea-client/src/player.rs index 9a5ba8ab..ee0b9718 100644 --- a/azalea-client/src/player.rs +++ b/azalea-client/src/player.rs @@ -1,7 +1,27 @@ use azalea_entity::Entity; +use azalea_world::World; +use uuid::Uuid; #[derive(Default, Debug)] pub struct Player { - /// The entity attached to the player. There's some useful fields here. - pub entity: Entity, + /// The player's uuid. + pub uuid: Uuid, + /// The player's entity id. + pub entity_id: u32, +} + +impl Player { + /// Get the entity of the player in the world. + pub fn entity<'a>(&self, world: &'a World) -> Option<&'a Entity> { + // world.entity_by_uuid(&self.uuid) + world.entity_by_id(self.entity_id) + } + + pub fn set_uuid(&mut self, uuid: Uuid) { + self.uuid = uuid; + } + + pub fn set_entity_id(&mut self, entity_id: u32) { + self.entity_id = entity_id; + } } diff --git a/azalea-core/src/delta.rs b/azalea-core/src/delta.rs index 41923ffb..c0056411 100644 --- a/azalea-core/src/delta.rs +++ b/azalea-core/src/delta.rs @@ -1,15 +1,41 @@ use crate::EntityPos; pub use azalea_buf::McBuf; -/// Only works for up to 8 blocks -#[derive(Clone, Debug, McBuf)] +pub trait PositionDeltaTrait { + fn x(&self) -> f64; + fn y(&self) -> f64; + fn z(&self) -> f64; +} + +#[derive(Clone, Debug, McBuf, Default)] pub struct PositionDelta { - xa: i16, - ya: i16, - za: i16, + pub xa: f64, + pub ya: f64, + pub za: f64, +} + +/// Only works for up to 8 blocks +#[derive(Clone, Debug, McBuf, Default)] +pub struct PositionDelta8 { + pub xa: i16, + pub ya: i16, + pub za: i16, +} + +impl PositionDeltaTrait for PositionDelta { + fn x(&self) -> f64 { + self.xa + } + fn y(&self) -> f64 { + self.ya + } + fn z(&self) -> f64 { + self.za + } } -impl PositionDelta { +impl PositionDelta8 { + #[deprecated] pub fn float(&self) -> (f64, f64, f64) { ( (self.xa as f64) / 4096.0, @@ -19,13 +45,24 @@ impl PositionDelta { } } +impl PositionDeltaTrait for PositionDelta8 { + fn x(&self) -> f64 { + (self.xa as f64) / 4096.0 + } + fn y(&self) -> f64 { + (self.ya as f64) / 4096.0 + } + fn z(&self) -> f64 { + (self.za as f64) / 4096.0 + } +} + impl EntityPos { - pub fn with_delta(&self, delta: &PositionDelta) -> EntityPos { - let (x, y, z) = delta.float(); + pub fn with_delta(&self, delta: &dyn PositionDeltaTrait) -> EntityPos { EntityPos { - x: self.x + x, - y: self.y + y, - z: self.z + z, + x: self.x + delta.x(), + y: self.y + delta.y(), + z: self.z + delta.z(), } } } diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index 8caa5799..de8e2516 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -5,6 +5,12 @@ use std::{ ops::Rem, }; +pub trait PositionXYZ { + fn add_x(&self, n: T) -> Self; + fn add_y(&self, n: T) -> Self; + fn add_z(&self, n: T) -> Self; +} + #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct BlockPos { pub x: i32, @@ -30,6 +36,30 @@ impl Rem for BlockPos { } } +impl PositionXYZ for BlockPos { + fn add_x(&self, n: i32) -> Self { + BlockPos { + x: self.x + n, + y: self.y, + z: self.z, + } + } + fn add_y(&self, n: i32) -> Self { + BlockPos { + x: self.x, + y: self.y + n, + z: self.z, + } + } + fn add_z(&self, n: i32) -> Self { + BlockPos { + x: self.x, + y: self.y, + z: self.z + n, + } + } +} + #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] pub struct ChunkPos { pub x: i32, @@ -42,15 +72,6 @@ impl ChunkPos { } } -impl From<&BlockPos> for ChunkPos { - fn from(pos: &BlockPos) -> Self { - ChunkPos { - x: pos.x.div_floor(16), - z: pos.z.div_floor(16), - } - } -} - /// The coordinates of a chunk section in the world. #[derive(Clone, Copy, Debug, Default)] pub struct ChunkSectionPos { @@ -64,23 +85,6 @@ impl ChunkSectionPos { ChunkSectionPos { x, y, z } } } - -impl From for ChunkSectionPos { - fn from(pos: BlockPos) -> Self { - ChunkSectionPos { - x: pos.x.div_floor(16), - y: pos.y.div_floor(16), - z: pos.z.div_floor(16), - } - } -} - -impl From for ChunkPos { - fn from(pos: ChunkSectionPos) -> Self { - ChunkPos { x: pos.x, z: pos.z } - } -} - /// The coordinates of a block inside a chunk. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct ChunkBlockPos { @@ -94,17 +98,6 @@ impl ChunkBlockPos { ChunkBlockPos { x, y, z } } } - -impl From<&BlockPos> for ChunkBlockPos { - fn from(pos: &BlockPos) -> Self { - ChunkBlockPos { - x: pos.x.rem_euclid(16).abs() as u8, - y: pos.y, - z: pos.z.rem_euclid(16).abs() as u8, - } - } -} - /// The coordinates of a block inside a chunk section. #[derive(Clone, Copy, Debug, Default)] pub struct ChunkSectionBlockPos { @@ -122,6 +115,80 @@ impl ChunkSectionBlockPos { } } +/// A block pos with an attached dimension +#[derive(Debug, Clone)] +pub struct GlobalPos { + pub pos: BlockPos, + // this is actually a ResourceKey in Minecraft, but i don't think it matters? + pub dimension: ResourceLocation, +} + +#[derive(Debug, Clone, Copy, Default)] +pub struct EntityPos { + pub x: f64, + pub y: f64, + pub z: f64, +} + +impl PositionXYZ for EntityPos { + fn add_x(&self, n: f64) -> Self { + EntityPos { + x: self.x + n, + y: self.y, + z: self.z, + } + } + fn add_y(&self, n: f64) -> Self { + EntityPos { + x: self.x, + y: self.y + n, + z: self.z, + } + } + fn add_z(&self, n: f64) -> Self { + EntityPos { + x: self.x, + y: self.y, + z: self.z + n, + } + } +} + +impl From<&BlockPos> for ChunkPos { + fn from(pos: &BlockPos) -> Self { + ChunkPos { + x: pos.x.div_floor(16), + z: pos.z.div_floor(16), + } + } +} + +impl From for ChunkSectionPos { + fn from(pos: BlockPos) -> Self { + ChunkSectionPos { + x: pos.x.div_floor(16), + y: pos.y.div_floor(16), + z: pos.z.div_floor(16), + } + } +} + +impl From for ChunkPos { + fn from(pos: ChunkSectionPos) -> Self { + ChunkPos { x: pos.x, z: pos.z } + } +} + +impl From<&BlockPos> for ChunkBlockPos { + fn from(pos: &BlockPos) -> Self { + ChunkBlockPos { + x: pos.x.rem_euclid(16).abs() as u8, + y: pos.y, + z: pos.z.rem_euclid(16).abs() as u8, + } + } +} + impl From<&BlockPos> for ChunkSectionBlockPos { fn from(pos: &BlockPos) -> Self { ChunkSectionBlockPos { @@ -141,22 +208,6 @@ impl From<&ChunkBlockPos> for ChunkSectionBlockPos { } } } - -/// A block pos with an attached dimension -#[derive(Debug, Clone)] -pub struct GlobalPos { - pub pos: BlockPos, - // this is actually a ResourceKey in Minecraft, but i don't think it matters? - pub dimension: ResourceLocation, -} - -#[derive(Debug, Clone, Default)] -pub struct EntityPos { - pub x: f64, - pub y: f64, - pub z: f64, -} - impl From<&EntityPos> for BlockPos { fn from(pos: &EntityPos) -> Self { BlockPos { diff --git a/azalea-entity/src/lib.rs b/azalea-entity/src/lib.rs index 065413a5..63c717d3 100644 --- a/azalea-entity/src/lib.rs +++ b/azalea-entity/src/lib.rs @@ -1,11 +1,6 @@ mod data; -use azalea_core::EntityPos; -#[cfg(feature = "protocol")] -use azalea_protocol::packets::game::{ - clientbound_add_entity_packet::ClientboundAddEntityPacket, - clientbound_add_player_packet::ClientboundAddPlayerPacket, -}; +use azalea_core::{EntityPos, PositionDelta}; pub use data::*; use uuid::Uuid; @@ -14,12 +9,27 @@ pub struct Entity { /// The incrementing numerical id of the entity. pub id: u32, pub uuid: Uuid, + /// The position of the entity right now. pos: EntityPos, + /// The position of the entity last tick. + pub old_pos: EntityPos, + pub delta: PositionDelta, + + pub x_rot: f32, + pub y_rot: f32, } impl Entity { pub fn new(id: u32, uuid: Uuid, pos: EntityPos) -> Self { - Self { id, uuid, pos } + Self { + id, + uuid, + pos, + old_pos: pos, + delta: PositionDelta::default(), + x_rot: 0.0, + y_rot: 0.0, + } } pub fn pos(&self) -> &EntityPos { @@ -31,6 +41,12 @@ impl Entity { pub fn unsafe_move(&mut self, new_pos: EntityPos) { self.pos = new_pos; } + + pub fn set_rotation(&mut self, x_rot: f32, y_rot: f32) { + self.x_rot = x_rot % 360.0; + self.y_rot = y_rot.clamp(-90.0, 90.0) % 360.0; + // TODO: minecraft also sets yRotO and xRotO to xRot and yRot ... but idk what they're used for so + } } // #[cfg(test)] diff --git a/azalea-protocol/src/packets/game/clientbound_move_entity_pos_packet.rs b/azalea-protocol/src/packets/game/clientbound_move_entity_pos_packet.rs index 63428dd8..cd3e3148 100644 --- a/azalea-protocol/src/packets/game/clientbound_move_entity_pos_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_move_entity_pos_packet.rs @@ -1,11 +1,11 @@ use azalea_buf::McBuf; -use azalea_core::PositionDelta; +use azalea_core::PositionDelta8; use packet_macros::GamePacket; #[derive(Clone, Debug, McBuf, GamePacket)] pub struct ClientboundMoveEntityPosPacket { #[var] pub entity_id: u32, - pub delta: PositionDelta, + pub delta: PositionDelta8, pub on_ground: bool, } diff --git a/azalea-protocol/src/packets/game/clientbound_move_entity_posrot_packet.rs b/azalea-protocol/src/packets/game/clientbound_move_entity_posrot_packet.rs index dd1d96e1..e3422ac0 100644 --- a/azalea-protocol/src/packets/game/clientbound_move_entity_posrot_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_move_entity_posrot_packet.rs @@ -1,5 +1,5 @@ use azalea_buf::McBuf; -use azalea_core::PositionDelta; +use azalea_core::PositionDelta8; use packet_macros::GamePacket; /// This packet is sent by the server when an entity moves less then 8 blocks. @@ -7,7 +7,7 @@ use packet_macros::GamePacket; pub struct ClientboundMoveEntityPosRotPacket { #[var] pub entity_id: u32, - pub delta: PositionDelta, + pub delta: PositionDelta8, pub y_rot: i8, pub x_rot: i8, pub on_ground: bool, diff --git a/azalea-world/Cargo.toml b/azalea-world/Cargo.toml index 02f05b46..ca890d82 100644 --- a/azalea-world/Cargo.toml +++ b/azalea-world/Cargo.toml @@ -13,6 +13,7 @@ azalea-entity = {path = "../azalea-entity"} azalea-nbt = {path = "../azalea-nbt"} log = "0.4.17" nohash-hasher = "0.2.0" +uuid = "1.1.2" [profile.release] lto = true diff --git a/azalea-world/src/entity.rs b/azalea-world/src/entity.rs index e4e9864f..5219e410 100644 --- a/azalea-world/src/entity.rs +++ b/azalea-world/src/entity.rs @@ -3,12 +3,13 @@ use azalea_entity::Entity; use log::warn; use nohash_hasher::{IntMap, IntSet}; use std::collections::HashMap; +use uuid::Uuid; #[derive(Debug)] pub struct EntityStorage { by_id: IntMap, - // TODO: this doesn't work yet (should be updated in the set_pos method in azalea-entity) by_chunk: HashMap>, + by_uuid: HashMap, } impl EntityStorage { @@ -16,6 +17,7 @@ impl EntityStorage { Self { by_id: IntMap::default(), by_chunk: HashMap::default(), + by_uuid: HashMap::default(), } } @@ -26,6 +28,7 @@ impl EntityStorage { .entry(ChunkPos::from(entity.pos())) .or_default() .insert(entity.id); + self.by_uuid.insert(entity.uuid, entity.id); self.by_id.insert(entity.id, entity); } @@ -34,9 +37,13 @@ impl EntityStorage { pub fn remove_by_id(&mut self, id: u32) { if let Some(entity) = self.by_id.remove(&id) { let entity_chunk = ChunkPos::from(entity.pos()); + let entity_uuid = entity.uuid; if self.by_chunk.remove(&entity_chunk).is_none() { warn!("Tried to remove entity with id {id} from chunk {entity_chunk:?} but it was not found."); } + if self.by_uuid.remove(&entity_uuid).is_none() { + warn!("Tried to remove entity with id {id} from uuid {entity_uuid:?} but it was not found."); + } } else { warn!("Tried to remove entity with id {id} but it was not found.") } @@ -54,11 +61,27 @@ impl EntityStorage { self.by_id.get_mut(&id) } + /// Get a reference to an entity by its uuid. + #[inline] + pub fn get_by_uuid(&self, uuid: &Uuid) -> Option<&Entity> { + self.by_uuid.get(uuid).and_then(|id| self.by_id.get(id)) + } + + /// Get a mutable reference to an entity by its uuid. + #[inline] + pub fn get_mut_by_uuid(&mut self, uuid: &Uuid) -> Option<&mut Entity> { + self.by_uuid.get(uuid).and_then(|id| self.by_id.get_mut(id)) + } + /// Clear all entities in a chunk. pub fn clear_chunk(&mut self, chunk: &ChunkPos) { if let Some(entities) = self.by_chunk.remove(chunk) { for entity_id in entities { - self.by_id.remove(&entity_id); + if let Some(entity) = self.by_id.remove(&entity_id) { + self.by_uuid.remove(&entity.uuid); + } else { + warn!("While clearing chunk {chunk:?}, found an entity that isn't in by_id {entity_id}."); + } } } } diff --git a/azalea-world/src/lib.rs b/azalea-world/src/lib.rs index bc73c13d..3afa4fee 100644 --- a/azalea-world/src/lib.rs +++ b/azalea-world/src/lib.rs @@ -6,7 +6,7 @@ mod entity; mod palette; use azalea_block::BlockState; -use azalea_core::{BlockPos, ChunkPos, EntityPos, PositionDelta}; +use azalea_core::{BlockPos, ChunkPos, EntityPos, PositionDelta8}; use azalea_entity::Entity; pub use bit_storage::BitStorage; pub use chunk::{Chunk, ChunkStorage}; @@ -16,6 +16,7 @@ use std::{ ops::{Index, IndexMut}, sync::{Arc, Mutex}, }; +use uuid::Uuid; #[cfg(test)] mod tests { @@ -76,7 +77,7 @@ impl World { pub fn move_entity_with_delta( &mut self, entity_id: u32, - delta: &PositionDelta, + delta: &PositionDelta8, ) -> Result<(), String> { let entity = self .entity_storage @@ -112,6 +113,14 @@ impl World { self.entity_storage.get_by_id(id) } + pub fn mut_entity_by_id(&mut self, id: u32) -> Option<&mut Entity> { + self.entity_storage.get_mut_by_id(id) + } + + pub fn entity_by_uuid(&self, uuid: &Uuid) -> Option<&Entity> { + self.entity_storage.get_by_uuid(uuid) + } + /// Get an iterator over all entities. #[inline] pub fn entities(&self) -> std::collections::hash_map::Values<'_, u32, Entity> { diff --git a/bot/src/main.rs b/bot/src/main.rs index 02f802a5..2976920b 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -1,12 +1,12 @@ use azalea_client::{Account, Event}; -use azalea_core::BlockPos; +use azalea_core::PositionXYZ; #[tokio::main] async fn main() -> Result<(), Box> { println!("Hello, world!"); // let address = "95.111.249.143:10000"; - let address = "localhost:49982"; + let address = "localhost:65399"; // let response = azalea_client::ping::ping_server(&address.try_into().unwrap()) // .await // .unwrap(); @@ -20,23 +20,37 @@ async fn main() -> Result<(), Box> { match e { // TODO: have a "loaded" or "ready" event that fires when all chunks are loaded Event::Login => {} - Event::GameTick => { - let world = client.world(); - if let Some(b) = world.find_one_entity(|e| { - e.uuid == uuid::uuid!("6536bfed-8695-48fd-83a1-ecd24cf2a0fd") - }) { - // let world = state.world.as_ref().unwrap(); - // world. - println!("{:?}", b); - } - // world.get_block_state(state.player.entity.pos); - // println!("{}", p.message.to_ansi(None)); - // if p.message.to_ansi(None) == " ok" { - // let state = client.state.lock(); - // let world = state.world.as_ref().unwrap(); - // let c = world.get_block_state(&BlockPos::new(5, 78, -2)).unwrap(); - // println!("block state: {:?}", c); - // } + // Event::GameTick => { + // let world = client.world(); + // if let Some(b) = world.find_one_entity(|e| { + // e.uuid == uuid::uuid!("6536bfed-8695-48fd-83a1-ecd24cf2a0fd") + // }) { + // // let world = state.world.as_ref().unwrap(); + // // world. + // println!("{:?}", b); + // } + // // world.get_block_state(state.player.entity.pos); + // // println!("{}", p.message.to_ansi(None)); + // // if p.message.to_ansi(None) == " ok" { + // // let state = client.state.lock(); + // // let world = state.world.as_ref().unwrap(); + // // let c = world.get_block_state(&BlockPos::new(5, 78, -2)).unwrap(); + // // println!("block state: {:?}", c); + // // } + // } + Event::Chat(msg) => { + let new_pos = { + let state_lock = client.state.lock().unwrap(); + let world = state_lock.world.as_ref().unwrap(); + let player = &state_lock.player; + let entity = player + .entity(&world) + .expect("Player entity is not in world"); + entity.pos().add_y(0.5) + }; + + println!("{:?}", new_pos); + client.move_to(new_pos).await.unwrap(); } _ => {} } -- cgit v1.2.3