Make CommandSender not mut
This commit is contained in:
parent
9eac0f01e1
commit
b8b4a8ce8d
@ -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",
|
||||
|
||||
@ -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(
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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));
|
||||
// {
|
||||
|
||||
@ -21,17 +21,17 @@ mod tree_builder;
|
||||
mod tree_format;
|
||||
|
||||
pub enum CommandSender<'a> {
|
||||
Rcon(&'a mut Vec<String>),
|
||||
Rcon(&'a tokio::sync::Mutex<Vec<String>>),
|
||||
Console,
|
||||
Player(Arc<Player>),
|
||||
}
|
||||
|
||||
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<Arc<Player>> {
|
||||
#[must_use]
|
||||
pub fn as_player(&self) -> Option<Arc<Player>> {
|
||||
match self {
|
||||
CommandSender::Player(player) => Some(player.clone()),
|
||||
_ => None,
|
||||
|
||||
@ -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()
|
||||
);
|
||||
|
||||
@ -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?;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user