Skip to main content

steel_registry/
chat_type.rs

1use rustc_hash::FxHashMap;
2use simdnbt::ToNbtTag;
3use simdnbt::owned::NbtTag;
4use steel_utils::Identifier;
5
6/// Represents a chat type definition from the data packs.
7#[derive(Debug)]
8pub struct ChatType {
9    pub key: Identifier,
10    pub chat: ChatTypeDecoration,
11    pub narration: ChatTypeDecoration,
12}
13
14/// Defines the styling and translation for a part of a chat message.
15#[derive(Debug)]
16pub struct ChatTypeDecoration {
17    pub translation_key: &'static str,
18    pub parameters: &'static [&'static str],
19    pub style: Option<ChatStyle>,
20}
21
22/// Defines optional text styling, like color and formatting.
23#[derive(Debug)]
24pub struct ChatStyle {
25    pub color: Option<&'static str>,
26    pub bold: Option<bool>,
27    pub italic: Option<bool>,
28    pub underlined: Option<bool>,
29    pub strikethrough: Option<bool>,
30    pub obfuscated: Option<bool>,
31}
32
33impl ToNbtTag for &ChatType {
34    fn to_nbt_tag(self) -> NbtTag {
35        use simdnbt::owned::NbtCompound;
36        let mut compound = NbtCompound::new();
37        compound.insert(
38            "chat",
39            NbtTag::Compound(ChatType::decoration_to_nbt(&self.chat)),
40        );
41        compound.insert(
42            "narration",
43            NbtTag::Compound(ChatType::decoration_to_nbt(&self.narration)),
44        );
45        NbtTag::Compound(compound)
46    }
47}
48
49impl ChatType {
50    fn decoration_to_nbt(dec: &ChatTypeDecoration) -> simdnbt::owned::NbtCompound {
51        use simdnbt::owned::{NbtCompound, NbtTag};
52        let mut compound = NbtCompound::new();
53        compound.insert("translation_key", dec.translation_key);
54        let params: Vec<String> = dec
55            .parameters
56            .iter()
57            .map(std::string::ToString::to_string)
58            .collect();
59        compound.insert("parameters", params);
60        if let Some(style) = &dec.style {
61            let mut style_compound = NbtCompound::new();
62            if let Some(color) = style.color {
63                style_compound.insert("color", color);
64            }
65            if let Some(bold) = style.bold {
66                style_compound.insert("bold", bold);
67            }
68            if let Some(italic) = style.italic {
69                style_compound.insert("italic", italic);
70            }
71            if let Some(underlined) = style.underlined {
72                style_compound.insert("underlined", underlined);
73            }
74            if let Some(strikethrough) = style.strikethrough {
75                style_compound.insert("strikethrough", strikethrough);
76            }
77            if let Some(obfuscated) = style.obfuscated {
78                style_compound.insert("obfuscated", obfuscated);
79            }
80            compound.insert("style", NbtTag::Compound(style_compound));
81        }
82        compound
83    }
84}
85
86pub type ChatTypeRef = &'static ChatType;
87
88pub struct ChatTypeRegistry {
89    chat_types_by_id: Vec<ChatTypeRef>,
90    chat_types_by_key: FxHashMap<Identifier, usize>,
91    allows_registering: bool,
92}
93
94impl ChatTypeRegistry {
95    #[must_use]
96    pub fn new() -> Self {
97        Self {
98            chat_types_by_id: Vec::new(),
99            chat_types_by_key: FxHashMap::default(),
100            allows_registering: true,
101        }
102    }
103}
104
105crate::impl_standard_methods!(
106    ChatTypeRegistry,
107    ChatTypeRef,
108    chat_types_by_id,
109    chat_types_by_key,
110    allows_registering
111);
112
113crate::impl_registry!(
114    ChatTypeRegistry,
115    ChatType,
116    chat_types_by_id,
117    chat_types_by_key,
118    chat_types
119);