From 460bdfb8bbaf0969df6451a00fc3de214728aec0 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 25 Jun 2022 16:23:40 -0500 Subject: merge ClientState and Client --- azalea-client/src/client.rs | 184 ++++++++++++++++++-------------------------- 1 file changed, 73 insertions(+), 111 deletions(-) (limited to 'azalea-client/src/client.rs') diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 2bc73551..11b098ca 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -24,22 +24,15 @@ use azalea_protocol::{ resolver, ServerAddress, }; use azalea_world::Dimension; -use owning_ref::OwningRef; use std::{ fmt::Debug, sync::{Arc, Mutex}, }; use tokio::{ sync::mpsc::{self, UnboundedReceiver, UnboundedSender}, - time::{self, MissedTickBehavior}, + time::{self}, }; -#[derive(Default)] -pub struct ClientState { - pub player: Player, - pub world: Option, -} - #[derive(Debug, Clone)] pub enum Event { Login, @@ -64,11 +57,12 @@ pub enum ChatPacket { // } /// A player that you can control that is currently in a Minecraft server. +#[derive(Clone)] pub struct Client { - event_receiver: UnboundedReceiver, game_profile: GameProfile, pub conn: Arc>, - pub state: Arc>, + pub player: Arc>, + pub dimension: Arc>>, // game_loop } @@ -80,7 +74,10 @@ struct HandleError(String); impl Client { /// Connect to a Minecraft server with an account. - pub async fn join(account: &Account, address: &ServerAddress) -> Result { + pub async fn join( + account: &Account, + address: &ServerAddress, + ) -> Result<(Self, UnboundedReceiver), String> { let resolved_address = resolver::resolve_address(address).await?; let mut conn = HandshakeConnection::new(&resolved_address).await?; @@ -159,51 +156,39 @@ 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())), + player: Arc::new(Mutex::new(Player::default())), + dimension: Arc::new(Mutex::new(None)), }; // just start up the game loop and we're ready! - let game_loop_state = client.state.clone(); + let game_loop_state = client.clone(); // if you get an error right here that means you're doing something with locks wrong // read the error to see where the issue is // you might be able to just drop the lock or put it in its own scope to fix - tokio::spawn(Self::protocol_loop( - conn.clone(), - tx.clone(), - game_loop_state.clone(), - game_profile.clone(), - )); - tokio::spawn(Self::game_tick_loop(conn, tx, game_loop_state)); - - Ok(client) + tokio::spawn(Self::protocol_loop(client.clone(), tx.clone())); + tokio::spawn(Self::game_tick_loop(client.clone(), tx.clone())); + + Ok((client, rx)) } - async fn protocol_loop( - conn: Arc>, - tx: UnboundedSender, - state: Arc>, - game_profile: GameProfile, - ) { + async fn protocol_loop(client: Client, tx: UnboundedSender) { loop { - let r = conn.lock().await.read().await; + let r = client.conn.lock().await.read().await; match r { - 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); - } + Ok(packet) => match Self::handle(&packet, &client, &tx).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); @@ -220,18 +205,14 @@ impl Client { async fn handle( packet: &GamePacket, + client: &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_lock = state.lock()?; - // // write p into login.txt // std::io::Write::write_all( // &mut std::fs::File::create("login.txt").unwrap(), @@ -292,23 +273,28 @@ impl Client { .as_int() .expect("min_y tag is not an int"); + let mut dimension_lock = client.dimension.lock().unwrap(); // the 16 here is our render distance // i'll make this an actual setting later - state_lock.world = Some(Dimension::new(16, height, min_y)); + *dimension_lock = Some(Dimension::new(16, height, min_y)); - let entity = Entity::new(p.player_id, game_profile.uuid, EntityPos::default()); - state_lock - .world + let entity = + Entity::new(p.player_id, client.game_profile.uuid, EntityPos::default()); + dimension_lock .as_mut() .expect( "Dimension doesn't exist! We should've gotten a login packet by now.", ) .add_entity(entity); - state_lock.player.set_entity_id(p.player_id); + let mut player_lock = client.player.lock().unwrap(); + + player_lock.set_entity_id(p.player_id); } - conn.lock() + client + .conn + .lock() .await .write( ServerboundCustomPayloadPacket { @@ -360,12 +346,17 @@ impl Client { println!("Got player position packet {:?}", p); let (new_pos, y_rot, x_rot) = { - 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 + let player_lock = client.player.lock().unwrap(); + let player_entity_id = player_lock.entity_id; + drop(player_lock); + + let mut dimension_lock = client.dimension.lock().unwrap(); + let dimension = dimension_lock.as_mut().unwrap(); + + let player_entity = dimension .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; @@ -416,14 +407,14 @@ impl Client { y: new_pos_y, z: new_pos_z, }; - world + dimension .move_entity(player_entity_id, new_pos) .expect("The player entity should always exist"); (new_pos, y_rot, x_rot) }; - let mut conn_lock = conn.lock().await; + let mut conn_lock = client.conn.lock().await; conn_lock .write(ServerboundAcceptTeleportationPacket { id: p.id }.get()) .await; @@ -447,9 +438,9 @@ impl Client { } GamePacket::ClientboundSetChunkCacheCenterPacket(p) => { println!("Got chunk cache center packet {:?}", p); - state + client + .dimension .lock()? - .world .as_mut() .unwrap() .update_view_center(&ChunkPos::new(p.x, p.z)); @@ -459,9 +450,9 @@ impl Client { let pos = ChunkPos::new(p.x, p.z); // let chunk = Chunk::read_with_world_height(&mut p.chunk_data); // println("chunk {:?}") - state + client + .dimension .lock()? - .world .as_mut() .expect("Dimension doesn't exist! We should've gotten a login packet by now.") .replace_with_packet_data(&pos, &mut p.chunk_data.data.as_slice()) @@ -473,9 +464,9 @@ impl Client { GamePacket::ClientboundAddEntityPacket(p) => { println!("Got add entity packet {:?}", p); let entity = Entity::from(p); - state + client + .dimension .lock()? - .world .as_mut() .expect("Dimension doesn't exist! We should've gotten a login packet by now.") .add_entity(entity); @@ -495,9 +486,9 @@ impl Client { GamePacket::ClientboundAddPlayerPacket(p) => { println!("Got add player packet {:?}", p); let entity = Entity::from(p); - state + client + .dimension .lock()? - .world .as_mut() .expect("Dimension doesn't exist! We should've gotten a login packet by now.") .add_entity(entity); @@ -521,10 +512,10 @@ impl Client { println!("Got set experience packet {:?}", p); } GamePacket::ClientboundTeleportEntityPacket(p) => { - let mut state_lock = state.lock()?; - let world = state_lock.world.as_mut().unwrap(); + let mut dimension_lock = client.dimension.lock()?; + let dimension = dimension_lock.as_mut().unwrap(); - world.move_entity( + dimension.move_entity( p.id, EntityPos { x: p.x, @@ -540,23 +531,25 @@ impl Client { // println!("Got rotate head packet {:?}", p); } GamePacket::ClientboundMoveEntityPosPacket(p) => { - let mut state_lock = state.lock()?; - let world = state_lock.world.as_mut().unwrap(); + let mut dimension_lock = client.dimension.lock()?; + let dimension = dimension_lock.as_mut().unwrap(); - world.move_entity_with_delta(p.entity_id, &p.delta)?; + dimension.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(); + let mut dimension_lock = client.dimension.lock()?; + let dimension = dimension_lock.as_mut().unwrap(); - world.move_entity_with_delta(p.entity_id, &p.delta)?; + dimension.move_entity_with_delta(p.entity_id, &p.delta)?; } GamePacket::ClientboundMoveEntityRotPacket(p) => { println!("Got move entity rot packet {:?}", p); } GamePacket::ClientboundKeepAlivePacket(p) => { println!("Got keep alive packet {:?}", p); - conn.lock() + client + .conn + .lock() .await .write(ServerboundKeepAlivePacket { id: p.id }.get()) .await; @@ -611,55 +604,24 @@ impl Client { Ok(()) } - pub async fn next(&mut self) -> Option { - self.event_receiver.recv().await - } - /// Runs game_tick every 50 milliseconds. - async fn game_tick_loop( - conn: Arc>, - tx: UnboundedSender, - state: Arc>, - ) { + async fn game_tick_loop(client: Client, tx: UnboundedSender) { let mut game_tick_interval = time::interval(time::Duration::from_millis(50)); // TODO: Minecraft bursts up to 10 ticks and then skips, we should too game_tick_interval.set_missed_tick_behavior(time::MissedTickBehavior::Burst); loop { game_tick_interval.tick().await; - Self::game_tick(&conn, &tx, &state).await; + Self::game_tick(&client, &tx).await; } } /// Runs every 50 milliseconds. - async fn game_tick( - conn: &Arc>, - tx: &UnboundedSender, - state: &Arc>, - ) { - if state.lock().unwrap().world.is_none() { + async fn game_tick(client: &Client, tx: &UnboundedSender) { + if client.dimension.lock().unwrap().is_none() { return; } tx.send(Event::GameTick).unwrap(); } - - /// Gets the `Dimension` the client is in. - /// - /// 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, Dimension> { - 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("Dimension 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 { -- cgit v1.2.3 From 978880b756aa052226f77c21e8e9de9b40070152 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 25 Jun 2022 16:40:12 -0500 Subject: Fix warnings --- azalea-client/src/client.rs | 2 -- azalea-client/src/movement.rs | 3 +-- azalea-core/src/particle/mod.rs | 2 +- azalea-entity/src/data.rs | 2 +- azalea-protocol/packet-macros/src/lib.rs | 4 ++-- .../src/packets/game/clientbound_declare_commands_packet.rs | 3 +-- .../src/packets/game/clientbound_level_particles_packet.rs | 2 +- bot/src/main.rs | 2 +- 8 files changed, 8 insertions(+), 12 deletions(-) (limited to 'azalea-client/src/client.rs') diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 11b098ca..9b3f7cdf 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -163,8 +163,6 @@ impl Client { // just start up the game loop and we're ready! - let game_loop_state = client.clone(); - // if you get an error right here that means you're doing something with locks wrong // read the error to see where the issue is // you might be able to just drop the lock or put it in its own scope to fix diff --git a/azalea-client/src/movement.rs b/azalea-client/src/movement.rs index 7ab4ddc2..5247e0f0 100644 --- a/azalea-client/src/movement.rs +++ b/azalea-client/src/movement.rs @@ -8,7 +8,7 @@ impl Client { let mut dimension_lock = self.dimension.lock().unwrap(); let dimension = dimension_lock.as_mut().unwrap(); - let mut player_lock = self.player.lock().unwrap(); + let player_lock = self.player.lock().unwrap(); let player_id = if let Some(player_lock) = player_lock.entity(dimension) { player_lock.id @@ -35,7 +35,6 @@ impl Client { .get(), ) .await; - println!("obtained lock on conn"); Ok(()) } diff --git a/azalea-core/src/particle/mod.rs b/azalea-core/src/particle/mod.rs index 3b2e9807..46f88407 100644 --- a/azalea-core/src/particle/mod.rs +++ b/azalea-core/src/particle/mod.rs @@ -256,7 +256,7 @@ impl McBufReadable for ParticleData { } impl McBufWritable for ParticleData { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + fn write_into(&self, _buf: &mut impl Write) -> Result<(), std::io::Error> { todo!() } } diff --git a/azalea-entity/src/data.rs b/azalea-entity/src/data.rs index f8468ecf..c9083893 100644 --- a/azalea-entity/src/data.rs +++ b/azalea-entity/src/data.rs @@ -116,7 +116,7 @@ impl McBufReadable for EntityDataValue { } impl McBufWritable for EntityDataValue { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + fn write_into(&self, _buf: &mut impl Write) -> Result<(), std::io::Error> { todo!(); } } diff --git a/azalea-protocol/packet-macros/src/lib.rs b/azalea-protocol/packet-macros/src/lib.rs index 2e40cbd1..e1ed80fa 100755 --- a/azalea-protocol/packet-macros/src/lib.rs +++ b/azalea-protocol/packet-macros/src/lib.rs @@ -1,9 +1,9 @@ use proc_macro::TokenStream; -use quote::{quote, ToTokens}; +use quote::quote; use syn::{ self, braced, parse::{Parse, ParseStream, Result}, - parse_macro_input, Data, DeriveInput, FieldsNamed, Ident, LitInt, Token, + parse_macro_input, DeriveInput, FieldsNamed, Ident, LitInt, Token, }; fn as_packet_derive(input: TokenStream, state: proc_macro2::TokenStream) -> TokenStream { 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 ee3f21a2..0e680701 100755 --- a/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_declare_commands_packet.rs @@ -1,4 +1,3 @@ -use super::GamePacket; use azalea_buf::McBuf; use azalea_buf::McBufVarReadable; use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable}; @@ -236,7 +235,7 @@ impl McBufReadable for BrigadierNodeStub { } impl McBufWritable for BrigadierNodeStub { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + fn write_into(&self, _buf: &mut impl Write) -> Result<(), std::io::Error> { todo!() } } 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 a9ce57ad..53975cca 100644 --- a/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs +++ b/azalea-protocol/src/packets/game/clientbound_level_particles_packet.rs @@ -51,7 +51,7 @@ impl McBufReadable for ClientboundLevelParticlesPacket { } impl McBufWritable for ClientboundLevelParticlesPacket { - fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { + fn write_into(&self, _buf: &mut impl Write) -> Result<(), std::io::Error> { todo!(); } } diff --git a/bot/src/main.rs b/bot/src/main.rs index 825025d4..948ca1d6 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -38,7 +38,7 @@ async fn main() -> Result<(), Box> { // // println!("block state: {:?}", c); // // } // } - Event::Chat(msg) => { + Event::Chat(_m) => { let new_pos = { let dimension_lock = client.dimension.lock().unwrap(); let dimension = dimension_lock.as_ref().unwrap(); -- cgit v1.2.3 From e8deda5d2e45eb634700614009cbcc5b35949e26 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 25 Jun 2022 17:37:29 -0500 Subject: clippo --- azalea-block/block-macros/src/lib.rs | 20 ++++++++------------ azalea-block/block-macros/src/utils.rs | 15 ++++++--------- azalea-block/src/lib.rs | 6 ++++-- azalea-buf/src/read.rs | 2 +- azalea-buf/src/write.rs | 2 +- azalea-chat/src/translatable_component.rs | 2 +- azalea-client/src/client.rs | 6 +++--- azalea-client/src/movement.rs | 22 +++++++++++----------- azalea-core/src/position.rs | 12 ++++++------ .../clientbound_section_blocks_update_packet.rs | 9 +++------ azalea-protocol/src/packets/mod.rs | 6 +++--- bot/src/main.rs | 2 +- 12 files changed, 48 insertions(+), 56 deletions(-) (limited to 'azalea-client/src/client.rs') diff --git a/azalea-block/block-macros/src/lib.rs b/azalea-block/block-macros/src/lib.rs index 6206bb65..01ce556d 100644 --- a/azalea-block/block-macros/src/lib.rs +++ b/azalea-block/block-macros/src/lib.rs @@ -36,7 +36,7 @@ struct BlockDefinition { properties_and_defaults: Vec, } impl PropertyAndDefault { - fn into_property_with_name_and_default(&self, name: String) -> PropertyWithNameAndDefault { + fn as_property_with_name_and_default(&self, name: String) -> PropertyWithNameAndDefault { PropertyWithNameAndDefault { name, struct_name: self.struct_name.clone(), @@ -110,11 +110,7 @@ impl Parse for BlockDefinition { let mut properties_and_defaults = Vec::new(); - loop { - let property = match content.parse() { - Ok(property) => property, - Err(_) => break, - }; + while let Ok(property) = content.parse() { content.parse::()?; let property_default = content.parse()?; properties_and_defaults.push(PropertyAndDefault { @@ -248,7 +244,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { for property_name in block_property_names { let property_variants = properties_map .get(property_name) - .expect(format!("Property '{}' not found", property_name).as_str()) + .unwrap_or_else(|| panic!("Property '{}' not found", property_name)) .clone(); block_properties_vec.push(property_variants); } @@ -274,13 +270,13 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { }; let mut property_name = property_struct_names_to_names .get(&property.struct_name.to_string()) - .expect(format!("Property '{}' is bad", property.struct_name).as_str()) + .unwrap_or_else(|| panic!("Property '{}' is bad", property.struct_name)) .clone(); if let Some(index) = index { property_name.push_str(&format!("_{}", &index.to_string())); } properties_with_name - .push(property.into_property_with_name_and_default(property_name.clone())); + .push(property.as_property_with_name_and_default(property_name.clone())); } // pub face: properties::Face, @@ -297,7 +293,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { { // let property_name_snake = // Ident::new(&property.to_string(), proc_macro2::Span::call_site()); - let name_ident = Ident::new(&name, proc_macro2::Span::call_site()); + let name_ident = Ident::new(name, proc_macro2::Span::call_site()); block_struct_fields.extend(quote! { pub #name_ident: #struct_name, }) @@ -317,7 +313,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { let first_state_id = state_id; // if there's no properties, then the block is just a single state - if block_properties_vec.len() == 0 { + if block_properties_vec.is_empty() { block_state_enum_variants.extend(quote! { #block_name_pascal_case, }); @@ -418,7 +414,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { let block_behavior = &block.behavior; let block_id = block.name.to_string(); - let from_block_to_state_match = if block.properties_and_defaults.len() > 0 { + let from_block_to_state_match = if !block.properties_and_defaults.is_empty() { quote! { match b { #from_block_to_state_match_inner diff --git a/azalea-block/block-macros/src/utils.rs b/azalea-block/block-macros/src/utils.rs index 019fd60f..82095d86 100644 --- a/azalea-block/block-macros/src/utils.rs +++ b/azalea-block/block-macros/src/utils.rs @@ -1,6 +1,6 @@ pub fn combinations_of(items: &[Vec]) -> Vec> { let mut combinations = Vec::new(); - if items.len() == 0 { + if items.is_empty() { return combinations; }; if items.len() == 1 { @@ -13,8 +13,7 @@ pub fn combinations_of(items: &[Vec]) -> Vec> { for i in 0..items[0].len() { let item = &items[0][i]; for other_combinations in combinations_of(&items[1..]) { - let mut combination = Vec::new(); - combination.push(item.clone()); + let mut combination = vec![item.clone()]; combination.extend(other_combinations); combinations.push(combination); } @@ -29,13 +28,11 @@ pub fn to_pascal_case(s: &str) -> String { for c in s.chars() { if c == '_' { prev_was_underscore = true; + } else if prev_was_underscore { + result.push(c.to_ascii_uppercase()); + prev_was_underscore = false; } else { - if prev_was_underscore { - result.push(c.to_ascii_uppercase()); - prev_was_underscore = false; - } else { - result.push(c); - } + result.push(c); } } result diff --git a/azalea-block/src/lib.rs b/azalea-block/src/lib.rs index a6de1e92..f07b1bce 100644 --- a/azalea-block/src/lib.rs +++ b/azalea-block/src/lib.rs @@ -7,8 +7,10 @@ pub use blocks::*; use std::mem; impl BlockState { - /// Transmutes a u32 to a block state. UB if the value is not a valid block - /// state. + /// Transmutes a u32 to a block state. + /// + /// # Safety + /// The `state_id` should be a valid block state. #[inline] pub unsafe fn from_u32_unsafe(state_id: u32) -> Self { mem::transmute::(state_id) diff --git a/azalea-buf/src/read.rs b/azalea-buf/src/read.rs index 92c4d79b..684404bc 100644 --- a/azalea-buf/src/read.rs +++ b/azalea-buf/src/read.rs @@ -231,7 +231,7 @@ impl McBufVarReadable for i64 { for i in 0..8 { buf.read_exact(&mut buffer) .map_err(|_| "Invalid VarLong".to_string())?; - ans |= ((buffer[0] & 0b0111_1111) as i64) << 7 * i; + ans |= ((buffer[0] & 0b0111_1111) as i64) << (7 * i); if buffer[0] & 0b1000_0000 == 0 { break; } diff --git a/azalea-buf/src/write.rs b/azalea-buf/src/write.rs index 38ddcf49..df7f56e0 100644 --- a/azalea-buf/src/write.rs +++ b/azalea-buf/src/write.rs @@ -191,7 +191,7 @@ impl McBufVarWritable for i64 { } // 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)?; + let _ = buf.write(&buffer)?; } Ok(()) } diff --git a/azalea-chat/src/translatable_component.rs b/azalea-chat/src/translatable_component.rs index fdef6465..6ffc5ccf 100755 --- a/azalea-chat/src/translatable_component.rs +++ b/azalea-chat/src/translatable_component.rs @@ -25,7 +25,7 @@ impl TranslatableComponent { } pub fn read(&self) -> Result { - let template = azalea_language::get(&self.key).unwrap_or_else(|| &self.key); + let template = azalea_language::get(&self.key).unwrap_or(&self.key); // decode the % things let mut result = String::new(); diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 9b3f7cdf..e2967a10 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -155,8 +155,8 @@ impl Client { // we got the GameConnection, so the server is now connected :) let client = Client { - game_profile: game_profile.clone(), - conn: conn.clone(), + game_profile, + conn, player: Arc::new(Mutex::new(Player::default())), dimension: Arc::new(Mutex::new(None)), }; @@ -167,7 +167,7 @@ impl Client { // read the error to see where the issue is // you might be able to just drop the lock or put it in its own scope to fix tokio::spawn(Self::protocol_loop(client.clone(), tx.clone())); - tokio::spawn(Self::game_tick_loop(client.clone(), tx.clone())); + tokio::spawn(Self::game_tick_loop(client.clone(), tx)); Ok((client, rx)) } diff --git a/azalea-client/src/movement.rs b/azalea-client/src/movement.rs index 5247e0f0..4f99984f 100644 --- a/azalea-client/src/movement.rs +++ b/azalea-client/src/movement.rs @@ -5,20 +5,20 @@ 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, new_pos: EntityPos) -> Result<(), String> { - let mut dimension_lock = self.dimension.lock().unwrap(); - let dimension = dimension_lock.as_mut().unwrap(); + { + let mut dimension_lock = self.dimension.lock().unwrap(); + let dimension = dimension_lock.as_mut().unwrap(); - let player_lock = self.player.lock().unwrap(); + let player_lock = self.player.lock().unwrap(); - let player_id = if let Some(player_lock) = player_lock.entity(dimension) { - player_lock.id - } else { - return Err("Player entity not found".to_string()); - }; + let player_id = if let Some(player_lock) = player_lock.entity(dimension) { + player_lock.id + } else { + return Err("Player entity not found".to_string()); + }; - dimension.move_entity(player_id, new_pos)?; - drop(dimension_lock); - drop(player_lock); + dimension.move_entity(player_id, new_pos)?; + } self.conn .lock() diff --git a/azalea-core/src/position.rs b/azalea-core/src/position.rs index 195558e8..64075aa7 100644 --- a/azalea-core/src/position.rs +++ b/azalea-core/src/position.rs @@ -182,9 +182,9 @@ impl From for ChunkPos { impl From<&BlockPos> for ChunkBlockPos { fn from(pos: &BlockPos) -> Self { ChunkBlockPos { - x: pos.x.rem_euclid(16).abs() as u8, + x: pos.x.rem_euclid(16).unsigned_abs() as u8, y: pos.y, - z: pos.z.rem_euclid(16).abs() as u8, + z: pos.z.rem_euclid(16).unsigned_abs() as u8, } } } @@ -192,9 +192,9 @@ impl From<&BlockPos> for ChunkBlockPos { impl From<&BlockPos> for ChunkSectionBlockPos { fn from(pos: &BlockPos) -> Self { ChunkSectionBlockPos { - x: pos.x.rem(16).abs() as u8, - y: pos.y.rem(16).abs() as u8, - z: pos.z.rem(16).abs() as u8, + x: pos.x.rem(16).unsigned_abs() as u8, + y: pos.y.rem(16).unsigned_abs() as u8, + z: pos.z.rem(16).unsigned_abs() as u8, } } } @@ -203,7 +203,7 @@ impl From<&ChunkBlockPos> for ChunkSectionBlockPos { fn from(pos: &ChunkBlockPos) -> Self { ChunkSectionBlockPos { x: pos.x, - y: pos.y.rem(16).abs() as u8, + y: pos.y.rem(16).unsigned_abs() as u8, z: pos.z, } } 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 60ebe26c..24f34f6e 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 @@ -22,15 +22,12 @@ impl McBufReadable for BlockStateWithPosition { let data = u64::var_read_from(buf)?; let position_part = data & 4095; let state = (data >> 12) as u32; - let position = ChunkSectionBlockPos { + let pos = ChunkSectionBlockPos { x: (position_part >> 8 & 15) as u8, - y: (position_part >> 0 & 15) as u8, + y: (position_part & 15) as u8, z: (position_part >> 4 & 15) as u8, }; - Ok(BlockStateWithPosition { - pos: position, - state: state, - }) + Ok(BlockStateWithPosition { pos, state }) } } diff --git a/azalea-protocol/src/packets/mod.rs b/azalea-protocol/src/packets/mod.rs index 2f439cd5..cf405e98 100755 --- a/azalea-protocol/src/packets/mod.rs +++ b/azalea-protocol/src/packets/mod.rs @@ -31,9 +31,9 @@ impl ConnectionProtocol { #[derive(Clone, Debug)] pub enum Packet { - Game(game::GamePacket), - Handshake(handshake::HandshakePacket), - Login(login::LoginPacket), + Game(Box), + Handshake(Box), + Login(Box), Status(Box), } diff --git a/bot/src/main.rs b/bot/src/main.rs index 948ca1d6..3ff30908 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -44,7 +44,7 @@ async fn main() -> Result<(), Box> { let dimension = dimension_lock.as_ref().unwrap(); let player = client.player.lock().unwrap(); let entity = player - .entity(&dimension) + .entity(dimension) .expect("Player entity is not in world"); entity.pos().add_y(0.5) }; -- cgit v1.2.3