refactor(commands): from async to sync Part 3

This commit is contained in:
Alexander Medvedev committed 2026-08-26 19:21:27 +02:00
1 parent 5d841d6d3b
commit 66a5f53408
222 files changed
+11699 -12920

No files matched your search

+42 -53
View File
@@ -1,7 +1,6 @@
use crate::command::CommandResult;
use crate::{
command::{
CommandError, CommandExecutor, CommandSender,
CommandError, CommandExecutor, CommandResult, CommandSender,
args::{
Arg, ConsumedArgs,
gameprofile::{GameProfileSuggestionMode, GameProfilesArgumentConsumer},
@@ -25,47 +24,43 @@ const ARG_REASON: &str = "reason";
struct NoReasonExecutor;
impl CommandExecutor for NoReasonExecutor {
fn execute<'a>(
&'a self,
sender: &'a CommandSender,
server: &'a crate::server::Server,
args: &'a ConsumedArgs<'a>,
) -> CommandResult<'a> {
Box::pin(async move {
let Some(Arg::GameProfiles(targets)) = args.get(&ARG_TARGET) else {
return Err(InvalidConsumption(Some(ARG_TARGET.into())));
};
fn execute(
&self,
sender: &CommandSender,
server: &crate::server::Server,
args: &ConsumedArgs,
) -> CommandResult {
let Some(Arg::GameProfiles(targets)) = args.get(&ARG_TARGET) else {
return Err(InvalidConsumption(Some(ARG_TARGET.into())));
};
ban_players(sender, server, targets.as_slice(), None).await
})
ban_players(sender, server, targets.as_slice(), None)
}
}
struct ReasonExecutor;
impl CommandExecutor for ReasonExecutor {
fn execute<'a>(
&'a self,
sender: &'a CommandSender,
server: &'a crate::server::Server,
args: &'a ConsumedArgs<'a>,
) -> CommandResult<'a> {
Box::pin(async move {
let Some(Arg::GameProfiles(targets)) = args.get(&ARG_TARGET) else {
return Err(InvalidConsumption(Some(ARG_TARGET.into())));
};
fn execute(
&self,
sender: &CommandSender,
server: &crate::server::Server,
args: &ConsumedArgs,
) -> CommandResult {
let Some(Arg::GameProfiles(targets)) = args.get(&ARG_TARGET) else {
return Err(InvalidConsumption(Some(ARG_TARGET.into())));
};
let Some(Arg::Msg(reason)) = args.get(ARG_REASON) else {
return Err(InvalidConsumption(Some(ARG_REASON.into())));
};
let Some(Arg::Msg(reason)) = args.get(ARG_REASON) else {
return Err(InvalidConsumption(Some(ARG_REASON.into())));
};
ban_players(sender, server, targets.as_slice(), Some(reason)).await
})
ban_players(sender, server, targets.as_slice(), Some(reason))
}
}
/// Returns the number of players successfully banned.
async fn ban_players(
fn ban_players(
sender: &CommandSender,
server: &crate::server::Server,
targets: &[GameProfile],
@@ -73,7 +68,7 @@ async fn ban_players(
) -> Result<i32, CommandError> {
let mut count: usize = 0;
for target in targets {
if ban_profile(sender, server, target, reason.cloned()).await {
if ban_profile(sender, server, target, reason.cloned()) {
count += 1;
}
}
@@ -90,13 +85,13 @@ async fn ban_players(
}
/// Returns `true` if the player was successfully banned.
async fn ban_profile(
fn ban_profile(
sender: &CommandSender,
server: &crate::server::Server,
profile: &GameProfile,
reason: Option<String>,
) -> bool {
let mut banned_players = server.data.banned_player_list.write().await;
let mut banned_players = server.data.banned_player_list.write().unwrap();
let reason = reason.unwrap_or_else(|| "Banned by an operator.".to_string());
@@ -123,28 +118,22 @@ async fn ban_profile(
drop(banned_players);
// Send messages
sender
.send_message(TextComponent::translate_cross(
translation::java::COMMANDS_BAN_SUCCESS,
translation::bedrock::COMMANDS_BAN_SUCCESS,
[
TextComponent::text(profile.name.clone()),
TextComponent::text(reason),
],
))
.await;
sender.send_message(TextComponent::translate_cross(
translation::java::COMMANDS_BAN_SUCCESS,
translation::bedrock::COMMANDS_BAN_SUCCESS,
[
TextComponent::text(profile.name.clone()),
TextComponent::text(reason),
],
));
if let Some(player) = server.get_player_by_uuid(profile.id) {
player
.kick(
DisconnectReason::Kicked,
TextComponent::translate_cross(
translation::java::MULTIPLAYER_DISCONNECT_BANNED,
translation::bedrock::DISCONNECTIONSCREEN_TITLE_BANNEDBYHOST,
[],
),
)
.await;
let kick_msg = TextComponent::translate_cross(
translation::java::MULTIPLAYER_DISCONNECT_BANNED,
translation::bedrock::DISCONNECTIONSCREEN_TITLE_BANNEDBYHOST,
[],
);
player.kick(DisconnectReason::Kicked, &kick_msg);
}
true