Skip to main content

pumpkinplus/mirror_types/
interaction.rs

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