Pumpkin/pumpkin/src/command/commands/defaultgamemode.rs
Alexander Medvedev 71392b4a38 remove async_trait from Commands
This decreased compile time by 30%, Plugins also are not required anymore to use async_trait when adding custom Commands
2025-12-03 18:25:58 +01:00

64 lines
2.0 KiB
Rust

use crate::command::CommandResult;
use crate::command::args::gamemode::GamemodeArgumentConsumer;
use crate::command::args::{Arg, GetCloned};
use crate::command::dispatcher::CommandError::InvalidConsumption;
use crate::command::tree::builder::argument;
use crate::command::{CommandExecutor, CommandSender, args::ConsumedArgs, tree::CommandTree};
use pumpkin_config::BASIC_CONFIG;
use pumpkin_util::GameMode;
use pumpkin_util::text::TextComponent;
const NAMES: [&str; 1] = ["defaultgamemode"];
const DESCRIPTION: &str = "Change the default gamemode";
pub const ARG_GAMEMODE: &str = "gamemode";
pub struct DefaultGamemode {
pub gamemode: GameMode,
}
struct DefaultGamemodeExecutor;
impl CommandExecutor for DefaultGamemodeExecutor {
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::GameMode(gamemode)) = args.get_cloned(&ARG_GAMEMODE) else {
return Err(InvalidConsumption(Some(ARG_GAMEMODE.into())));
};
if BASIC_CONFIG.force_gamemode {
for player in server.get_all_players().await {
player.set_gamemode(gamemode).await;
}
}
let gamemode_string = format!("{gamemode:?}").to_lowercase();
let gamemode_string = format!("gameMode.{gamemode_string}");
sender
.send_message(TextComponent::translate(
"commands.defaultgamemode.success",
[TextComponent::translate(gamemode_string, [])],
))
.await;
//Change the default gamemode (not in configuration.toml)
server.defaultgamemode.lock().await.gamemode = gamemode;
Ok(())
})
}
}
#[must_use]
pub fn init_command_tree() -> CommandTree {
CommandTree::new(NAMES, DESCRIPTION)
.then(argument(ARG_GAMEMODE, GamemodeArgumentConsumer).execute(DefaultGamemodeExecutor))
}