minecraft_command_types/command/
data.rs

1use crate::coordinate::Coordinates;
2use crate::entity_selector::EntitySelector;
3use crate::nbt_path::NbtPath;
4use crate::resource_location::ResourceLocation;
5use crate::snbt::SNBT;
6use minecraft_command_types_derive::HasMacro;
7use ordered_float::NotNan;
8use std::fmt::{Display, Formatter};
9
10#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
11pub enum DataTarget {
12    Block(Coordinates),
13    Entity(EntitySelector),
14    Storage(ResourceLocation),
15}
16
17impl Display for DataTarget {
18    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19        match self {
20            DataTarget::Block(coordinates) => {
21                write!(f, "block {}", coordinates)
22            }
23            DataTarget::Entity(selector) => {
24                write!(f, "entity {}", selector)
25            }
26            DataTarget::Storage(storage) => {
27                write!(f, "storage {}", storage)
28            }
29        }
30    }
31}
32
33#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
34pub enum DataCommandModification {
35    From(DataTarget, Option<NbtPath>),
36    String(DataTarget, Option<NbtPath>, Option<i32>, Option<i32>),
37    Value(SNBT),
38}
39
40impl Display for DataCommandModification {
41    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
42        match self {
43            DataCommandModification::From(source, path) => {
44                write!(f, "from {}", source)?;
45
46                if let Some(path) = path {
47                    write!(f, " {}", path)?;
48                }
49
50                Ok(())
51            }
52            DataCommandModification::String(source, path, start, end) => {
53                write!(f, "string {}", source)?;
54
55                if let Some(path) = path {
56                    write!(f, " {}", path)?;
57
58                    if let Some(start) = start {
59                        write!(f, " {}", start)?;
60
61                        if let Some(end) = end {
62                            write!(f, " {}", end)?;
63                        }
64                    }
65                }
66
67                Ok(())
68            }
69            DataCommandModification::Value(value) => {
70                write!(f, "value {}", value)
71            }
72        }
73    }
74}
75
76#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
77pub enum DataCommandModificationMode {
78    Append,
79    Prepend,
80    Insert(i32),
81    Merge,
82    Set,
83}
84
85impl Display for DataCommandModificationMode {
86    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
87        match self {
88            DataCommandModificationMode::Append => f.write_str("append"),
89            DataCommandModificationMode::Prepend => f.write_str("prepend"),
90            DataCommandModificationMode::Insert(index) => write!(f, "insert {}", index),
91            DataCommandModificationMode::Merge => f.write_str("merge"),
92            DataCommandModificationMode::Set => f.write_str("set"),
93        }
94    }
95}
96
97#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
98pub enum DataCommand {
99    Get(DataTarget, Option<NbtPath>, Option<NotNan<f32>>),
100    Merge(DataTarget, SNBT),
101    Modify(
102        DataTarget,
103        NbtPath,
104        DataCommandModificationMode,
105        DataCommandModification,
106    ),
107    Remove(DataTarget, NbtPath),
108}
109
110impl Display for DataCommand {
111    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
112        match self {
113            DataCommand::Get(target, path, scale) => {
114                write!(f, "get {}", target)?;
115
116                if let Some(path) = path {
117                    write!(f, " {}", path)?;
118
119                    if let Some(scale) = scale {
120                        write!(f, " {}", scale)?;
121                    }
122                }
123
124                Ok(())
125            }
126            DataCommand::Merge(target, nbt) => {
127                write!(f, "merge {} {}", target, nbt)
128            }
129            DataCommand::Modify(target, path, modification_mode, modification_command) => {
130                write!(
131                    f,
132                    "modify {} {} {} {}",
133                    target, path, modification_mode, modification_command
134                )
135            }
136            DataCommand::Remove(target, path) => {
137                write!(f, "remove {} {}", target, path)
138            }
139        }
140    }
141}