minecraft_command_types/command/
attribute.rs

1use crate::command::enums::attribute::AttributeAddModifier;
2use crate::resource_location::ResourceLocation;
3use minecraft_command_types_derive::HasMacro;
4use ordered_float::NotNan;
5use std::fmt::{Display, Formatter};
6
7type F32 = NotNan<f32>;
8
9#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
10pub enum BaseAttributeCommand {
11    /// Returns the base value of the specified attribute.
12    Get(Option<F32>),
13    /// Overwrites the base value of the specified attribute with the given value.
14    Set(F32),
15    /// Resets the base value of the specified attribute to its default value.
16    Reset,
17}
18
19impl Display for BaseAttributeCommand {
20    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21        match self {
22            BaseAttributeCommand::Get(scale) => {
23                f.write_str("get")?;
24
25                if let Some(scale) = scale {
26                    write!(f, " {}", scale)?;
27                }
28
29                Ok(())
30            }
31            BaseAttributeCommand::Set(value) => write!(f, "set {}", value),
32            BaseAttributeCommand::Reset => f.write_str("reset"),
33        }
34    }
35}
36
37#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
38pub enum ModifierAttributeCommand {
39    /// Adds an attribute modifier with the specified properties if no modifier with the same ID already existed.
40    Add(ResourceLocation, F32, AttributeAddModifier),
41    /// Removes the attribute modifier with the specified ID.
42    Remove(ResourceLocation),
43    /// Returns the value of the modifier with the specified ID.
44    Get(ResourceLocation, Option<F32>),
45}
46
47impl Display for ModifierAttributeCommand {
48    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49        match self {
50            ModifierAttributeCommand::Add(id, value, add_modifier) => {
51                write!(f, "add {} {} {}", id, value, add_modifier)
52            }
53            ModifierAttributeCommand::Remove(id) => {
54                write!(f, "remove {}", id)
55            }
56            ModifierAttributeCommand::Get(id, scale) => {
57                write!(f, "value get {}", id)?;
58
59                if let Some(scale) = scale {
60                    write!(f, " {}", scale)?;
61                }
62
63                Ok(())
64            }
65        }
66    }
67}
68
69#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
70pub enum AttributeCommand {
71    /// Returns the total value of the specified attribute.
72    Get(Option<F32>),
73    Base(BaseAttributeCommand),
74    Modifier(ModifierAttributeCommand),
75}
76
77impl Display for AttributeCommand {
78    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
79        match self {
80            AttributeCommand::Get(scale) => {
81                f.write_str("get")?;
82
83                if let Some(scale) = scale {
84                    write!(f, " {}", scale)?;
85                }
86
87                Ok(())
88            }
89            AttributeCommand::Base(base_command) => {
90                write!(f, "base {}", base_command)
91            }
92            AttributeCommand::Modifier(modifier_command) => {
93                write!(f, "modifier {}", modifier_command)
94            }
95        }
96    }
97}