Make CommandSender not mut

This commit is contained in:
Snowiiii 2024-10-22 14:02:49 +02:00
parent 9eac0f01e1
commit b8b4a8ce8d
7 changed files with 15 additions and 13 deletions

View File

@ -22,7 +22,7 @@ codegen-units = 1
[workspace.dependencies] [workspace.dependencies]
log = "0.4" log = "0.4"
tokio = { version = "1.40", features = [ tokio = { version = "1.41", features = [
"fs", "fs",
"io-util", "io-util",
"macros", "macros",

View File

@ -429,7 +429,7 @@ impl Client {
/// Kicks the Client with a reason depending on the connection state /// Kicks the Client with a reason depending on the connection state
pub async fn kick(&self, reason: &str) { 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() { match self.connection_state.load() {
ConnectionState::Login => { ConnectionState::Login => {
self.try_send_packet(&CLoginDisconnect::new( self.try_send_packet(&CLoginDisconnect::new(

View File

@ -50,7 +50,7 @@ pub fn parse_arg_player(
.as_str(); .as_str();
match s { 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!(), "@p" => todo!(),
"@r" => todo!(), // todo: implement random player target selector "@r" => todo!(), // todo: implement random player target selector
"@a" | "@e" => todo!(), // todo: implement all players target selector "@a" | "@e" => todo!(), // todo: implement all players target selector

View File

@ -9,7 +9,7 @@ const DESCRIPTION: &str =
pub fn init_command_tree<'a>() -> CommandTree<'a> { pub fn init_command_tree<'a>() -> CommandTree<'a> {
CommandTree::new(NAMES, DESCRIPTION).execute(&|sender, server, _| { 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(); let entity_id = player.entity_id();
// player.open_container.store(Some(0)); // player.open_container.store(Some(0));
// { // {

View File

@ -21,17 +21,17 @@ mod tree_builder;
mod tree_format; mod tree_format;
pub enum CommandSender<'a> { pub enum CommandSender<'a> {
Rcon(&'a mut Vec<String>), Rcon(&'a tokio::sync::Mutex<Vec<String>>),
Console, Console,
Player(Arc<Player>), Player(Arc<Player>),
} }
impl<'a> CommandSender<'a> { 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 { match self {
CommandSender::Console => log::info!("{}", text.to_pretty_console()), CommandSender::Console => log::info!("{}", text.to_pretty_console()),
CommandSender::Player(c) => c.send_system_message(&text).await, 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 { pub const fn is_console(&self) -> bool {
matches!(self, CommandSender::Console) matches!(self, CommandSender::Console)
} }
pub fn as_mut_player(&mut self) -> Option<Arc<Player>> { #[must_use]
pub fn as_player(&self) -> Option<Arc<Player>> {
match self { match self {
CommandSender::Player(player) => Some(player.clone()), CommandSender::Player(player) => Some(player.clone()),
_ => None, _ => None,

View File

@ -286,7 +286,7 @@ impl Player {
.await .await
.unwrap_or_else(|_| self.client.close()); .unwrap_or_else(|_| self.client.close());
log::info!( log::info!(
"Kicked {} for {}", "Kicked Player {} for {}",
self.gameprofile.name, self.gameprofile.name,
reason.to_pretty_console() reason.to_pretty_console()
); );

View File

@ -118,20 +118,21 @@ impl RCONClient {
} }
ServerboundPacket::ExecCommand => { ServerboundPacket::ExecCommand => {
if self.logged_in { if self.logged_in {
let mut output = Vec::new(); let output = tokio::sync::Mutex::new(Vec::new());
let dispatcher = server.command_dispatcher.clone(); let dispatcher = server.command_dispatcher.clone();
dispatcher dispatcher
.handle_command( .handle_command(
&mut crate::commands::CommandSender::Rcon(&mut output), &mut crate::commands::CommandSender::Rcon(&output),
server, server,
packet.get_body(), packet.get_body(),
) )
.await; .await;
for line in output { let output = output.lock().await;
for line in output.iter() {
if config.logging.log_commands { if config.logging.log_commands {
log::info!("RCON ({}): {}", self.address, line); log::info!("RCON ({}): {}", self.address, line);
} }
self.send(ClientboundPacket::Output, packet.get_id(), &line) self.send(ClientboundPacket::Output, packet.get_id(), line)
.await?; .await?;
} }
} }