Skip to content

Commit

Permalink
less clones
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Sep 19, 2024
1 parent 8112f4a commit 14a1343
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 59 deletions.
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/client/play/c_player_info_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayerAction>,
pub actions: Vec<PlayerAction<'a>>,
}

impl<'a> CPlayerInfoUpdate<'a> {
Expand Down
6 changes: 3 additions & 3 deletions pumpkin-protocol/src/client/play/player_action.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{Property, VarInt};

pub enum PlayerAction {
pub enum PlayerAction<'a> {
AddPlayer {
name: String,
properties: Vec<Property>,
name: &'a str,
properties: &'a [Property],
},
InitializeChat(u8),
/// Gamemode ?
Expand Down
1 change: 0 additions & 1 deletion pumpkin-protocol/src/packet_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ pub struct PacketEncoder {
impl PacketEncoder {
pub fn append_packet<P: ClientPacket>(&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();
Expand Down
8 changes: 4 additions & 4 deletions pumpkin/src/client/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))?;
Expand Down Expand Up @@ -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)]
Expand Down
36 changes: 21 additions & 15 deletions pumpkin/src/client/client_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Expand All @@ -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);
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Server>, plugin_message: SPluginMessage) {
Expand Down
8 changes: 1 addition & 7 deletions pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl Player {
Some(TextComponent::text(&message)),
FilterType::PassThrough,
1.into(),
TextComponent::text(&gameprofile.name.clone()),
TextComponent::text(&gameprofile.name),
None,
))

Expand Down
5 changes: 1 addition & 4 deletions pumpkin/src/server/connection_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
43 changes: 22 additions & 21 deletions pumpkin/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
Expand All @@ -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;

Expand Down

0 comments on commit 14a1343

Please sign in to comment.