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.parameters.iter().map(|s| s.to_string()).collect();
55        compound.insert("parameters", params);
56        if let Some(style) = &dec.style {
57            let mut style_compound = NbtCompound::new();
58            if let Some(color) = style.color {
59                style_compound.insert("color", color);
60            }
61            if let Some(bold) = style.bold {
62                style_compound.insert("bold", bold);
63            }
64            if let Some(italic) = style.italic {
65                style_compound.insert("italic", italic);
66            }
67            if let Some(underlined) = style.underlined {
68                style_compound.insert("underlined", underlined);
69            }
70            if let Some(strikethrough) = style.strikethrough {
71                style_compound.insert("strikethrough", strikethrough);
72            }
73            if let Some(obfuscated) = style.obfuscated {
74                style_compound.insert("obfuscated", obfuscated);
75            }
76            compound.insert("style", NbtTag::Compound(style_compound));
77        }
78        compound
79    }
80}
81
82pub type ChatTypeRef = &'static ChatType;
83
84pub struct ChatTypeRegistry {
85    chat_types_by_id: Vec<ChatTypeRef>,
86    chat_types_by_key: FxHashMap<Identifier, usize>,
87    allows_registering: bool,
88}
89
90impl ChatTypeRegistry {
91    #[must_use]
92    pub fn new() -> Self {
93        Self {
94            chat_types_by_id: Vec::new(),
95            chat_types_by_key: FxHashMap::default(),
96            allows_registering: true,
97        }
98    }
99}
100
101crate::impl_standard_methods!(
102    ChatTypeRegistry,
103    ChatTypeRef,
104    chat_types_by_id,
105    chat_types_by_key,
106    allows_registering
107);
108
109crate::impl_registry!(
110    ChatTypeRegistry,
111    ChatType,
112    chat_types_by_id,
113    chat_types_by_key,
114    chat_types
115);