Commands: Support gamemode command to use numbers

This commit is contained in:
Snowiiii 2024-08-13 12:49:34 +02:00
parent 2eb4c8624f
commit 32a172f3c0
2 changed files with 23 additions and 6 deletions

View File

@ -1,3 +1,8 @@
use num_traits::FromPrimitive;
use pumpkin_text::TextComponent;
use crate::entity::player::GameMode;
use super::Command; use super::Command;
pub struct GamemodeCommand {} pub struct GamemodeCommand {}
@ -12,8 +17,7 @@ impl<'a> Command<'a> for GamemodeCommand {
let args: Vec<&str> = command.split_whitespace().collect(); let args: Vec<&str> = command.split_whitespace().collect();
if args.len() != 2 { if args.len() != 2 {
// TODO: red player.send_system_message(TextComponent::from("Usage: /gamemode <mode>").color_named(pumpkin_text::color::NamedColor::Red));
player.send_system_message("Usage: /gamemode <mode>".into());
return; return;
} }
@ -21,11 +25,24 @@ impl<'a> Command<'a> for GamemodeCommand {
match mode_str.parse() { match mode_str.parse() {
Ok(mode) => { Ok(mode) => {
player.set_gamemode(mode); player.set_gamemode(mode);
player.send_system_message(format!("Set own game mode to {mode_str}").into()); player.send_system_message(format!("Set own game mode to {:?}", mode).into());
} }
Err(_) => { Err(_) => {
// TODO: red // try to parse from number
player.send_system_message("Invalid gamemode".into()); match mode_str.parse::<u8>() {
Ok(i) => match GameMode::from_u8(i) {
Some(mode) => {
player.set_gamemode(mode);
player.send_system_message(format!("Set own game mode to {:?}", mode).into());
return;
},
None => {},
},
Err(_) => {},
}
player.send_system_message(TextComponent::from("Invalid gamemode").color_named(pumpkin_text::color::NamedColor::Red));
} }
} }
} }

View File

@ -50,7 +50,7 @@ pub enum ChatMode {
Hidden, Hidden,
} }
#[derive(Clone, Copy, PartialEq, Serialize, Deserialize, FromPrimitive, ToPrimitive)] #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, FromPrimitive, ToPrimitive)]
pub enum GameMode { pub enum GameMode {
Undefined = -1, Undefined = -1,
Survival, Survival,