From b8b4a8ce8d67a565eb79f0500513cda56d8196d9 Mon Sep 17 00:00:00 2001 From: Snowiiii Date: Tue, 22 Oct 2024 14:02:49 +0200 Subject: [PATCH] Make CommandSender not mut --- Cargo.toml | 2 +- pumpkin/src/client/mod.rs | 2 +- pumpkin/src/commands/arg_player.rs | 2 +- pumpkin/src/commands/cmd_echest.rs | 2 +- pumpkin/src/commands/mod.rs | 9 +++++---- pumpkin/src/entity/player.rs | 2 +- pumpkin/src/rcon/mod.rs | 9 +++++---- 7 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e31cdf617..339bb41d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ codegen-units = 1 [workspace.dependencies] log = "0.4" -tokio = { version = "1.40", features = [ +tokio = { version = "1.41", features = [ "fs", "io-util", "macros", diff --git a/pumpkin/src/client/mod.rs b/pumpkin/src/client/mod.rs index af0ebe82a..d1946d3f5 100644 --- a/pumpkin/src/client/mod.rs +++ b/pumpkin/src/client/mod.rs @@ -429,7 +429,7 @@ impl Client { /// Kicks the Client with a reason depending on the connection state pub async fn kick(&self, reason: &str) { - log::info!("Kicking id {} for {}", self.id, reason); + log::info!("Kicking Client id {} for {}", self.id, reason); match self.connection_state.load() { ConnectionState::Login => { self.try_send_packet(&CLoginDisconnect::new( diff --git a/pumpkin/src/commands/arg_player.rs b/pumpkin/src/commands/arg_player.rs index b232b8253..15d26ace9 100644 --- a/pumpkin/src/commands/arg_player.rs +++ b/pumpkin/src/commands/arg_player.rs @@ -50,7 +50,7 @@ pub fn parse_arg_player( .as_str(); match s { - "@s" if src.is_player() => Ok(src.as_mut_player().unwrap()), + "@s" if src.is_player() => Ok(src.as_player().unwrap()), "@p" => todo!(), "@r" => todo!(), // todo: implement random player target selector "@a" | "@e" => todo!(), // todo: implement all players target selector diff --git a/pumpkin/src/commands/cmd_echest.rs b/pumpkin/src/commands/cmd_echest.rs index a279e908a..252ee26bb 100644 --- a/pumpkin/src/commands/cmd_echest.rs +++ b/pumpkin/src/commands/cmd_echest.rs @@ -9,7 +9,7 @@ const DESCRIPTION: &str = pub fn init_command_tree<'a>() -> CommandTree<'a> { CommandTree::new(NAMES, DESCRIPTION).execute(&|sender, server, _| { - if let Some(player) = sender.as_mut_player() { + if let Some(player) = sender.as_player() { let entity_id = player.entity_id(); // player.open_container.store(Some(0)); // { diff --git a/pumpkin/src/commands/mod.rs b/pumpkin/src/commands/mod.rs index 01dcac893..36fa2abed 100644 --- a/pumpkin/src/commands/mod.rs +++ b/pumpkin/src/commands/mod.rs @@ -21,17 +21,17 @@ mod tree_builder; mod tree_format; pub enum CommandSender<'a> { - Rcon(&'a mut Vec), + Rcon(&'a tokio::sync::Mutex>), Console, Player(Arc), } impl<'a> CommandSender<'a> { - pub async fn send_message(&mut self, text: TextComponent<'a>) { + pub async fn send_message(&self, text: TextComponent<'a>) { match self { CommandSender::Console => log::info!("{}", text.to_pretty_console()), CommandSender::Player(c) => c.send_system_message(&text).await, - CommandSender::Rcon(s) => s.push(text.to_pretty_console()), + CommandSender::Rcon(s) => s.lock().await.push(text.to_pretty_console()), } } @@ -44,7 +44,8 @@ impl<'a> CommandSender<'a> { pub const fn is_console(&self) -> bool { matches!(self, CommandSender::Console) } - pub fn as_mut_player(&mut self) -> Option> { + #[must_use] + pub fn as_player(&self) -> Option> { match self { CommandSender::Player(player) => Some(player.clone()), _ => None, diff --git a/pumpkin/src/entity/player.rs b/pumpkin/src/entity/player.rs index 26b239b73..75e8905e4 100644 --- a/pumpkin/src/entity/player.rs +++ b/pumpkin/src/entity/player.rs @@ -286,7 +286,7 @@ impl Player { .await .unwrap_or_else(|_| self.client.close()); log::info!( - "Kicked {} for {}", + "Kicked Player {} for {}", self.gameprofile.name, reason.to_pretty_console() ); diff --git a/pumpkin/src/rcon/mod.rs b/pumpkin/src/rcon/mod.rs index 84c82998d..b7b41352a 100644 --- a/pumpkin/src/rcon/mod.rs +++ b/pumpkin/src/rcon/mod.rs @@ -118,20 +118,21 @@ impl RCONClient { } ServerboundPacket::ExecCommand => { if self.logged_in { - let mut output = Vec::new(); + let output = tokio::sync::Mutex::new(Vec::new()); let dispatcher = server.command_dispatcher.clone(); dispatcher .handle_command( - &mut crate::commands::CommandSender::Rcon(&mut output), + &mut crate::commands::CommandSender::Rcon(&output), server, packet.get_body(), ) .await; - for line in output { + let output = output.lock().await; + for line in output.iter() { if config.logging.log_commands { log::info!("RCON ({}): {}", self.address, line); } - self.send(ClientboundPacket::Output, packet.get_id(), &line) + self.send(ClientboundPacket::Output, packet.get_id(), line) .await?; } }