Skip to main content

pumpkinplus/mirror_types/
gamemode.rs

1//! Mirror of the API gamemode enum.
2//!
3//! Matches Minecraft's four gamemodes exactly.
4
5use serde::{Deserialize, Serialize};
6use std::fmt;
7
8/// Mirror of the API gamemode enum.
9///
10/// Matches Minecraft's four gamemodes exactly.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash)]
12#[serde(rename_all = "PascalCase")]
13pub enum GameMode {
14    Survival,
15    Creative,
16    Adventure,
17    Spectator,
18}
19
20impl GameMode {
21    /// Returns true if the given list is empty (allow-all) or contains this GameMode.
22    pub fn matches_config(&self, allowed: &[Self]) -> bool {
23        allowed.is_empty() || allowed.contains(self)
24    }
25}
26
27impl fmt::Display for GameMode {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        write!(f, "{:?}", self)
30    }
31}
32
33/// Convert from the upstream API gamemode to our mirror type.
34///
35/// Uses the debug representation as the canonical name, falling back
36/// to `Survival` if the upstream type emits something unexpected.
37impl From<pumpkin_plugin_api::player::GameMode> for GameMode {
38    fn from(value: pumpkin_plugin_api::player::GameMode) -> Self {
39        #[allow(unreachable_patterns)]
40        match value {
41            pumpkin_plugin_api::player::GameMode::Survival => Self::Survival,
42            pumpkin_plugin_api::player::GameMode::Creative => Self::Creative,
43            pumpkin_plugin_api::player::GameMode::Adventure => Self::Adventure,
44            pumpkin_plugin_api::player::GameMode::Spectator => Self::Spectator,
45            _ => Self::Survival,
46        }
47    }
48}