minecraft_command_types/command/
effect.rs

1use crate::entity_selector::EntitySelector;
2use crate::resource_location::ResourceLocation;
3use minecraft_command_types_derive::HasMacro;
4use std::fmt::{Display, Formatter};
5
6#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
7pub enum EffectDuration {
8    Duration(i32),
9    Infinite,
10}
11
12impl Display for EffectDuration {
13    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
14        match self {
15            EffectDuration::Duration(duration) => duration.fmt(f),
16            EffectDuration::Infinite => f.write_str("infinite"),
17        }
18    }
19}
20
21#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
22pub enum EffectCommand {
23    Clear(Option<EntitySelector>, Option<ResourceLocation>),
24    Give(
25        EntitySelector,
26        ResourceLocation,
27        Option<EffectDuration>,
28        Option<i32>,
29        Option<bool>,
30    ),
31}
32
33impl Display for EffectCommand {
34    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
35        match self {
36            EffectCommand::Clear(selector, effect) => {
37                f.write_str("clear")?;
38
39                if let Some(selector) = selector {
40                    write!(f, " {}", selector)?;
41
42                    if let Some(effect) = effect {
43                        write!(f, " {}", effect)?;
44                    }
45                }
46
47                Ok(())
48            }
49            EffectCommand::Give(selector, effect, duration, amplifier, hide_particles) => {
50                write!(f, "give {} {}", selector, effect)?;
51
52                if let Some(duration) = duration {
53                    write!(f, " {}", duration)?;
54
55                    if let Some(amplifier) = amplifier {
56                        write!(f, " {}", amplifier)?;
57
58                        if let Some(hide_particles) = hide_particles {
59                            write!(f, " {}", hide_particles)?;
60                        }
61                    }
62                }
63
64                Ok(())
65            }
66        }
67    }
68}