diff options
| author | mat <github@matdoes.dev> | 2022-06-19 22:01:54 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-06-19 22:01:54 -0500 |
| commit | c9a070f711a6fdaf505f522cb8809749c9190e38 (patch) | |
| tree | c64f5d6aaa4f489538d2e81814b31b5abca71a9e /azalea-client/src | |
| parent | d674633e856f0ac1683172ad5655ff7026c6eae6 (diff) | |
| download | azalea-drasl-c9a070f711a6fdaf505f522cb8809749c9190e38.tar.xz | |
Fix some clippy warnings
Diffstat (limited to 'azalea-client/src')
| -rw-r--r-- | azalea-client/src/account.rs | 9 | ||||
| -rw-r--r-- | azalea-client/src/client.rs | 61 |
2 files changed, 42 insertions, 28 deletions
diff --git a/azalea-client/src/account.rs b/azalea-client/src/account.rs index 00a5d0f6..0bcad630 100644 --- a/azalea-client/src/account.rs +++ b/azalea-client/src/account.rs @@ -1,12 +1,5 @@ use crate::Client; -use azalea_protocol::{ - packets::game::{ - clientbound_player_chat_packet::ClientboundPlayerChatPacket, - clientbound_system_chat_packet::ClientboundSystemChatPacket, - }, - ServerAddress, -}; -use std::fmt::Debug; +use azalea_protocol::ServerAddress; ///! Connect to Minecraft servers. diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index dc2fe70f..3dd206b5 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -20,7 +20,8 @@ use azalea_protocol::{ }, resolver, ServerAddress, }; -use azalea_world::{ChunkStorage, EntityStorage, World}; +use azalea_world::World; +use owning_ref::OwningRef; use std::{ fmt::Debug, sync::{Arc, Mutex}, @@ -42,7 +43,7 @@ pub enum Event { #[derive(Debug, Clone)] pub enum ChatPacket { System(ClientboundSystemChatPacket), - Player(ClientboundPlayerChatPacket), + Player(Box<ClientboundPlayerChatPacket>), } // impl ChatPacket { @@ -65,6 +66,7 @@ pub struct Client { /// Whether we should ignore errors when decoding packets. const IGNORE_ERRORS: bool = !cfg!(debug_assertions); +#[derive(Debug)] struct HandleError(String); impl Client { @@ -172,9 +174,17 @@ impl Client { loop { let r = conn.lock().await.read().await; match r { - Ok(packet) => { - Self::handle(&packet, &tx, &state, &conn).await; - } + 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); + } + } + }, Err(e) => { if IGNORE_ERRORS { println!("Error: {:?}", e); @@ -242,7 +252,9 @@ impl Client { .expect("name is not a string") == p.dimension_type.to_string() }) - .expect(&format!("No dimension_type with name {}", p.dimension_type)) + .unwrap_or_else(|| { + panic!("No dimension_type with name {}", p.dimension_type) + }) .as_compound() .unwrap() .get("element") @@ -256,13 +268,11 @@ impl Client { .expect("height tag is not an int")) .try_into() .expect("height is not a u32"); - let min_y = (*dimension_type + let min_y = *dimension_type .get("min_y") .expect("No min_y tag") .as_int() - .expect("min_y tag is not an int")) - .try_into() - .expect("min_y is not an i32"); + .expect("min_y tag is not an int"); state.world = Some(World::new(16, height, min_y)); } @@ -308,7 +318,7 @@ impl Client { GamePacket::ClientboundUpdateRecipesPacket(_p) => { println!("Got update recipes packet"); } - GamePacket::ClientboundEntityEventPacket(p) => { + GamePacket::ClientboundEntityEventPacket(_p) => { // println!("Got entity event packet {:?}", p); } GamePacket::ClientboundRecipePacket(_p) => { @@ -356,13 +366,13 @@ impl Client { .expect("World doesn't exist! We should've gotten a login packet by now.") .add_entity(entity); } - GamePacket::ClientboundSetEntityDataPacket(p) => { + GamePacket::ClientboundSetEntityDataPacket(_p) => { // println!("Got set entity data packet {:?}", p); } - GamePacket::ClientboundUpdateAttributesPacket(p) => { + GamePacket::ClientboundUpdateAttributesPacket(_p) => { // println!("Got update attributes packet {:?}", p); } - GamePacket::ClientboundEntityVelocityPacket(p) => { + GamePacket::ClientboundEntityVelocityPacket(_p) => { // println!("Got entity velocity packet {:?}", p); } GamePacket::ClientboundSetEntityLinkPacket(p) => { @@ -389,9 +399,9 @@ impl Client { GamePacket::ClientboundSetExperiencePacket(p) => { println!("Got set experience packet {:?}", p); } - GamePacket::ClientboundTeleportEntityPacket(p) => { + GamePacket::ClientboundTeleportEntityPacket(_p) => { // println!("Got teleport entity packet {:?}", p); - let state_lock = state.lock()?; + // let state_lock = state.lock()?; // let entity = state_lock // .world @@ -407,13 +417,13 @@ impl Client { GamePacket::ClientboundUpdateAdvancementsPacket(p) => { println!("Got update advancements packet {:?}", p); } - GamePacket::ClientboundRotateHeadPacket(p) => { + GamePacket::ClientboundRotateHeadPacket(_p) => { // println!("Got rotate head packet {:?}", p); } - GamePacket::ClientboundMoveEntityPosPacket(p) => { + GamePacket::ClientboundMoveEntityPosPacket(_p) => { // println!("Got move entity pos packet {:?}", p); } - GamePacket::ClientboundMoveEntityPosRotPacket(p) => { + GamePacket::ClientboundMoveEntityPosRotPacket(_p) => { // println!("Got move entity pos rot packet {:?}", p); } GamePacket::ClientboundMoveEntityRotPacket(p) => { @@ -431,7 +441,8 @@ impl Client { } GamePacket::ClientboundPlayerChatPacket(p) => { println!("Got player chat packet {:?}", p); - tx.send(Event::Chat(ChatPacket::Player(p.clone()))).unwrap(); + tx.send(Event::Chat(ChatPacket::Player(Box::new(p.clone())))) + .unwrap(); } GamePacket::ClientboundSystemChatPacket(p) => { println!("Got system chat packet {:?}", p); @@ -475,6 +486,16 @@ impl Client { pub async fn next(&mut self) -> Option<Event> { self.event_receiver.recv().await } + + /// Gets the `World` the client is in. + /// + /// This is basically a shortcut for `let world = 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<std::sync::MutexGuard<ClientState>, World> { + let state_lock: std::sync::MutexGuard<ClientState> = 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!")) + } } impl<T> From<std::sync::PoisonError<T>> for HandleError { |
