Skip to content

Commit

Permalink
Commands: Support gamemode command to use numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 13, 2024
1 parent 2eb4c86 commit 32a172f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
27 changes: 22 additions & 5 deletions pumpkin/src/commands/gamemode.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use num_traits::FromPrimitive;
use pumpkin_text::TextComponent;

use crate::entity::player::GameMode;

use super::Command;

pub struct GamemodeCommand {}
Expand All @@ -12,20 +17,32 @@ impl<'a> Command<'a> for GamemodeCommand {
let args: Vec<&str> = command.split_whitespace().collect();

if args.len() != 2 {
// TODO: red
player.send_system_message("Usage: /gamemode <mode>".into());
player.send_system_message(TextComponent::from("Usage: /gamemode <mode>").color_named(pumpkin_text::color::NamedColor::Red));
return;
}

let mode_str = args[1].to_lowercase();
match mode_str.parse() {
Ok(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(_) => {
// TODO: red
player.send_system_message("Invalid gamemode".into());
// try to parse from number
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));
}
}
}
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 @@ -50,7 +50,7 @@ pub enum ChatMode {
Hidden,
}

#[derive(Clone, Copy, PartialEq, Serialize, Deserialize, FromPrimitive, ToPrimitive)]
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, FromPrimitive, ToPrimitive)]
pub enum GameMode {
Undefined = -1,
Survival,
Expand Down

0 comments on commit 32a172f

Please sign in to comment.