Skip to main content

steel_registry/
trim_material.rs

1use rustc_hash::FxHashMap;
2use simdnbt::ToNbtTag;
3use simdnbt::owned::NbtTag;
4use steel_utils::Identifier;
5
6/// Represents an armor trim material definition from the data packs.
7#[derive(Debug)]
8pub struct TrimMaterial {
9    pub key: Identifier,
10    pub asset_name: String,
11    pub description: StyledTextComponent,
12    pub override_armor_assets: FxHashMap<Identifier, String>,
13}
14
15/// Represents a translatable text component that can also include styling.
16#[derive(Debug)]
17pub struct StyledTextComponent {
18    pub translate: String,
19    pub color: Option<String>,
20}
21
22impl ToNbtTag for &TrimMaterial {
23    fn to_nbt_tag(self) -> NbtTag {
24        use simdnbt::owned::NbtCompound;
25        let mut compound = NbtCompound::new();
26        compound.insert("asset_name", self.asset_name.as_str());
27        let mut desc = NbtCompound::new();
28        desc.insert("translate", self.description.translate.as_str());
29        if let Some(color) = &self.description.color {
30            desc.insert("color", color.as_str());
31        }
32        compound.insert("description", NbtTag::Compound(desc));
33        let mut overrides = NbtCompound::new();
34        for (key, value) in &self.override_armor_assets {
35            let key_str = key.to_string();
36            overrides.insert(key_str.as_str(), value.as_str());
37        }
38        compound.insert("override_armor_assets", NbtTag::Compound(overrides));
39        NbtTag::Compound(compound)
40    }
41}
42
43pub type TrimMaterialRef = &'static TrimMaterial;
44
45pub struct TrimMaterialRegistry {
46    trim_materials_by_id: Vec<TrimMaterialRef>,
47    trim_materials_by_key: FxHashMap<Identifier, usize>,
48    allows_registering: bool,
49}
50
51impl TrimMaterialRegistry {
52    #[must_use]
53    pub fn new() -> Self {
54        Self {
55            trim_materials_by_id: Vec::new(),
56            trim_materials_by_key: FxHashMap::default(),
57            allows_registering: true,
58        }
59    }
60}
61
62crate::impl_standard_methods!(
63    TrimMaterialRegistry,
64    TrimMaterialRef,
65    trim_materials_by_id,
66    trim_materials_by_key,
67    allows_registering
68);
69
70crate::impl_registry!(
71    TrimMaterialRegistry,
72    TrimMaterial,
73    trim_materials_by_id,
74    trim_materials_by_key,
75    trim_materials
76);