diff --git a/pumpkin-protocol/src/client/play/c_player_info_update.rs b/pumpkin-protocol/src/client/play/c_player_info_update.rs index b62108680..054ebb028 100644 --- a/pumpkin-protocol/src/client/play/c_player_info_update.rs +++ b/pumpkin-protocol/src/client/play/c_player_info_update.rs @@ -7,12 +7,12 @@ use super::PlayerAction; #[packet(0x3E)] pub struct CPlayerInfoUpdate<'a> { pub actions: i8, - pub players: &'a [Player], + pub players: &'a [Player<'a>], } -pub struct Player { +pub struct Player<'a> { pub uuid: uuid::Uuid, - pub actions: Vec, + pub actions: Vec>, } impl<'a> CPlayerInfoUpdate<'a> { diff --git a/pumpkin-protocol/src/client/play/player_action.rs b/pumpkin-protocol/src/client/play/player_action.rs index 3a5f2a80a..48baf4eca 100644 --- a/pumpkin-protocol/src/client/play/player_action.rs +++ b/pumpkin-protocol/src/client/play/player_action.rs @@ -1,9 +1,9 @@ use crate::{Property, VarInt}; -pub enum PlayerAction { +pub enum PlayerAction<'a> { AddPlayer { - name: String, - properties: Vec, + name: &'a str, + properties: &'a [Property], }, InitializeChat(u8), /// Gamemode ? diff --git a/pumpkin-protocol/src/packet_encoder.rs b/pumpkin-protocol/src/packet_encoder.rs index 9e0d9f525..55c4da7cc 100644 --- a/pumpkin-protocol/src/packet_encoder.rs +++ b/pumpkin-protocol/src/packet_encoder.rs @@ -26,7 +26,6 @@ pub struct PacketEncoder { impl PacketEncoder { pub fn append_packet(&mut self, packet: &P) -> Result<(), PacketError> { let start_len = self.buf.len(); - let mut writer = (&mut self.buf).writer(); let mut packet_buf = ByteBuffer::empty(); diff --git a/pumpkin/src/client/authentication.rs b/pumpkin/src/client/authentication.rs index 20a3c28e7..03853578b 100644 --- a/pumpkin/src/client/authentication.rs +++ b/pumpkin/src/client/authentication.rs @@ -77,15 +77,15 @@ pub async fn authenticate( match response.status() { StatusCode::OK => {} StatusCode::NO_CONTENT => Err(AuthError::UnverifiedUsername)?, - other => Err(AuthError::UnknownStatusCode(other.as_str().to_string()))?, + other => Err(AuthError::UnknownStatusCode(other))?, } let profile: GameProfile = response.json().await.map_err(|_| AuthError::FailedParse)?; Ok(profile) } -pub fn unpack_textures(property: Property, config: &TextureConfig) -> Result<(), TextureError> { +pub fn unpack_textures(property: &Property, config: &TextureConfig) -> Result<(), TextureError> { let from64 = general_purpose::STANDARD - .decode(property.value) + .decode(&property.value) .map_err(|e| TextureError::DecodeError(e.to_string()))?; let textures: ProfileTextures = serde_json::from_slice(&from64).map_err(|e| TextureError::JSONError(e.to_string()))?; @@ -120,7 +120,7 @@ pub enum AuthError { #[error("Failed to parse JSON into Game Profile")] FailedParse, #[error("Unknown Status Code")] - UnknownStatusCode(String), + UnknownStatusCode(StatusCode), } #[derive(Error, Debug)] diff --git a/pumpkin/src/client/client_packet.rs b/pumpkin/src/client/client_packet.rs index 67b091230..a38b4059c 100644 --- a/pumpkin/src/client/client_packet.rs +++ b/pumpkin/src/client/client_packet.rs @@ -135,13 +135,12 @@ impl Client { self.kick("Your account can't join"); } } else { - for allowed in ADVANCED_CONFIG + for allowed in &ADVANCED_CONFIG .authentication .player_profile .allowed_actions - .clone() { - if !p.contains(&allowed) { + if !p.contains(allowed) { self.kick("Your account can't join"); } } @@ -152,7 +151,7 @@ impl Client { Err(e) => self.kick(&e.to_string()), } } - for property in gameprofile.as_ref().unwrap().properties.clone() { + for property in &gameprofile.as_ref().unwrap().properties { // TODO: use this (this was the todo here before, ill add it again cuz its prob here for a reason) let _ = unpack_textures(property, &ADVANCED_CONFIG.authentication.textures); } @@ -165,7 +164,7 @@ impl Client { self.set_compression(Some((threshold, level))); } - if let Some(profile) = gameprofile.as_ref().cloned() { + if let Some(profile) = gameprofile.as_ref() { let packet = CLoginSuccess::new(&profile.id, &profile.name, &profile.properties, false); self.send_packet(&packet); } else { @@ -222,16 +221,23 @@ impl Client { client_information: SClientInformationConfig, ) { dbg!("got client settings"); - *self.config.lock() = Some(PlayerConfig { - locale: client_information.locale, - view_distance: client_information.view_distance, - chat_mode: ChatMode::from_i32(client_information.chat_mode.into()).unwrap(), - chat_colors: client_information.chat_colors, - skin_parts: client_information.skin_parts, - main_hand: Hand::from_i32(client_information.main_hand.into()).unwrap(), - text_filtering: client_information.text_filtering, - server_listing: client_information.server_listing, - }); + if let (Some(main_hand), Some(chat_mode)) = ( + Hand::from_i32(client_information.main_hand.into()), + ChatMode::from_i32(client_information.chat_mode.into()), + ) { + *self.config.lock() = Some(PlayerConfig { + locale: client_information.locale, + view_distance: client_information.view_distance, + chat_mode, + chat_colors: client_information.chat_colors, + skin_parts: client_information.skin_parts, + main_hand, + text_filtering: client_information.text_filtering, + server_listing: client_information.server_listing, + }); + } else { + self.kick("Invalid hand or chat type") + } } pub fn handle_plugin_message(&self, _server: &Arc, plugin_message: SPluginMessage) { diff --git a/pumpkin/src/client/mod.rs b/pumpkin/src/client/mod.rs index af7f2ec9f..7178841c0 100644 --- a/pumpkin/src/client/mod.rs +++ b/pumpkin/src/client/mod.rs @@ -16,10 +16,9 @@ use authentication::GameProfile; use crossbeam::atomic::AtomicCell; use mio::{event::Event, net::TcpStream, Token}; use parking_lot::Mutex; -use pumpkin_core::text::TextComponent; use pumpkin_protocol::{ bytebuf::{packet_id::Packet, DeserializerError}, - client::{config::CConfigDisconnect, login::CLoginDisconnect, play::CPlayDisconnect}, + client::{config::CConfigDisconnect, login::CLoginDisconnect}, packet_decoder::PacketDecoder, packet_encoder::PacketEncoder, server::{ @@ -366,11 +365,6 @@ impl Client { self.try_send_packet(&CConfigDisconnect::new(reason)) .unwrap_or_else(|_| self.close()); } - // So we can also kick on errors, but generally should use Player::kick - ConnectionState::Play => { - self.try_send_packet(&CPlayDisconnect::new(&TextComponent::text(reason))) - .unwrap_or_else(|_| self.close()); - } _ => { log::warn!("Can't kick in {:?} State", self.connection_state) } diff --git a/pumpkin/src/client/player_packet.rs b/pumpkin/src/client/player_packet.rs index 139c0b862..759d4cb79 100644 --- a/pumpkin/src/client/player_packet.rs +++ b/pumpkin/src/client/player_packet.rs @@ -326,7 +326,7 @@ impl Player { Some(TextComponent::text(&message)), FilterType::PassThrough, 1.into(), - TextComponent::text(&gameprofile.name.clone()), + TextComponent::text(&gameprofile.name), None, )) diff --git a/pumpkin/src/server/connection_cache.rs b/pumpkin/src/server/connection_cache.rs index 49a65c4c2..6fdd618dd 100644 --- a/pumpkin/src/server/connection_cache.rs +++ b/pumpkin/src/server/connection_cache.rs @@ -85,10 +85,7 @@ impl CachedStatus { } fn load_icon(path: &str) -> String { - let icon = match image::open(path).map_err(|e| panic!("error loading icon: {}", e)) { - Ok(icon) => icon, - Err(_) => return "".into(), - }; + let icon = image::open(path).expect("Failed to load icon"); let dimension = icon.dimensions(); assert!(dimension.0 == 64, "Icon width must be 64"); assert!(dimension.1 == 64, "Icon height must be 64"); diff --git a/pumpkin/src/world/mod.rs b/pumpkin/src/world/mod.rs index 308ce4abf..8fe4eae04 100644 --- a/pumpkin/src/world/mod.rs +++ b/pumpkin/src/world/mod.rs @@ -129,8 +129,8 @@ impl World { uuid: gameprofile.id, actions: vec![ PlayerAction::AddPlayer { - name: gameprofile.name.clone(), - properties: gameprofile.properties.clone(), + name: &gameprofile.name, + properties: &gameprofile.properties, }, PlayerAction::UpdateListed(true), ], @@ -139,27 +139,28 @@ impl World { // here we send all the infos of already joined players let mut entries = Vec::new(); - for (_, playerr) in self - .current_players - .lock() - .iter() - .filter(|(c, _)| **c != player.client.token) { - let gameprofile = &playerr.gameprofile; - entries.push(pumpkin_protocol::client::play::Player { - uuid: gameprofile.id, - actions: vec![ - PlayerAction::AddPlayer { - name: gameprofile.name.clone(), - properties: gameprofile.properties.clone(), - }, - PlayerAction::UpdateListed(true), - ], - }) + let current_players = self.current_players.lock(); + for (_, playerr) in current_players + .iter() + .filter(|(c, _)| **c != player.client.token) + { + let gameprofile = &playerr.gameprofile; + entries.push(pumpkin_protocol::client::play::Player { + uuid: gameprofile.id, + actions: vec![ + PlayerAction::AddPlayer { + name: &gameprofile.name, + properties: &gameprofile.properties, + }, + PlayerAction::UpdateListed(true), + ], + }) + } + player + .client + .send_packet(&CPlayerInfoUpdate::new(0x01 | 0x08, &entries)); } - player - .client - .send_packet(&CPlayerInfoUpdate::new(0x01 | 0x08, &entries)); let gameprofile = &player.gameprofile;