Skip to content

Commit

Permalink
Make CommandSender not mut
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Oct 22, 2024
1 parent 9eac0f0 commit b8b4a8c
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/commands/arg_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/commands/cmd_echest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
// {
Expand Down
9 changes: 5 additions & 4 deletions pumpkin/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/src/entity/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down
9 changes: 5 additions & 4 deletions pumpkin/src/rcon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
}
}
Expand Down

0 comments on commit b8b4a8c

Please sign in to comment.