|
1 | 1 | use crate::CommandCtx; |
2 | 2 | use feather_core::position; |
3 | | -use feather_core::util::Position; |
| 3 | +use feather_core::util::{Gamemode, Position}; |
4 | 4 | use feather_server_types::{Game, Name, NetworkId, Player}; |
5 | 5 | use fecs::{component, Entity, IntoQuery, Read, World}; |
6 | 6 | use lieutenant::{ArgumentKind, Input}; |
@@ -240,3 +240,35 @@ impl ArgumentKind<CommandCtx> for Coordinates { |
240 | 240 | Ok(Coordinates { x, y, z }) |
241 | 241 | } |
242 | 242 | } |
| 243 | + |
| 244 | +#[derive(Debug, Error)] |
| 245 | +pub enum GamemodeParseError { |
| 246 | + #[error("invalid gamemode string {0}")] |
| 247 | + InvalidGamemode(String), |
| 248 | +} |
| 249 | + |
| 250 | +/// A parsed gamemode string ("survival", "creative", ...) |
| 251 | +#[derive(Copy, Clone, Debug)] |
| 252 | +pub struct ParsedGamemode(pub Gamemode); |
| 253 | + |
| 254 | +impl ArgumentKind<CommandCtx> for ParsedGamemode { |
| 255 | + type ParseError = GamemodeParseError; |
| 256 | + |
| 257 | + fn satisfies<'a>(_ctx: &CommandCtx, input: &mut Input<'a>) -> bool { |
| 258 | + !input.advance_until(" ").is_empty() |
| 259 | + } |
| 260 | + |
| 261 | + fn parse<'a>(_ctx: &CommandCtx, input: &mut Input<'a>) -> Result<Self, Self::ParseError> { |
| 262 | + let s = input.advance_until(" "); |
| 263 | + |
| 264 | + let gamemode = match s { |
| 265 | + "survival" => Gamemode::Survival, |
| 266 | + "creative" => Gamemode::Creative, |
| 267 | + "spectator" => Gamemode::Spectator, |
| 268 | + "adventure" => Gamemode::Adventure, |
| 269 | + s => return Err(GamemodeParseError::InvalidGamemode(s.to_owned())), |
| 270 | + }; |
| 271 | + |
| 272 | + Ok(ParsedGamemode(gamemode)) |
| 273 | + } |
| 274 | +} |
0 commit comments