Skip to main content

steel_registry/data_component_predicate/
registry_predicates.rs

1use super::*;
2
3/// Predicate over armor trim material and pattern holders.
4#[derive(Debug, Clone, PartialEq)]
5pub struct TrimPredicate {
6    material: Option<RegistryHolderSet<TrimMaterial>>,
7    pattern: Option<RegistryHolderSet<TrimPattern>>,
8}
9
10impl TrimPredicate {
11    #[must_use]
12    pub const fn new(
13        material: Option<RegistryHolderSet<TrimMaterial>>,
14        pattern: Option<RegistryHolderSet<TrimPattern>>,
15    ) -> Self {
16        Self { material, pattern }
17    }
18
19    #[must_use]
20    pub const fn material(&self) -> Option<&RegistryHolderSet<TrimMaterial>> {
21        self.material.as_ref()
22    }
23
24    #[must_use]
25    pub const fn pattern(&self) -> Option<&RegistryHolderSet<TrimPattern>> {
26        self.pattern.as_ref()
27    }
28}
29
30impl DataComponentPredicateCodec for TrimPredicate {
31    fn from_nbt_value(tag: &NbtTag) -> Option<Self> {
32        let compound = tag.compound()?;
33        Some(Self {
34            material: decode_optional(compound, "material", RegistryHolderSet::from_owned_nbt)?,
35            pattern: decode_optional(compound, "pattern", RegistryHolderSet::from_owned_nbt)?,
36        })
37    }
38
39    fn to_nbt_value(&self) -> NbtTag {
40        let mut compound = NbtCompound::new();
41        if let Some(material) = &self.material {
42            compound.insert("material", material.clone().to_nbt_tag());
43        }
44        if let Some(pattern) = &self.pattern {
45            compound.insert("pattern", pattern.clone().to_nbt_tag());
46        }
47        NbtTag::Compound(compound)
48    }
49}
50
51impl HashComponent for TrimPredicate {
52    fn hash_component(&self, hasher: &mut ComponentHasher) {
53        let mut entries = Vec::new();
54        if let Some(material) = &self.material {
55            push_hash_entry(&mut entries, "material", material);
56        }
57        if let Some(pattern) = &self.pattern {
58            push_hash_entry(&mut entries, "pattern", pattern);
59        }
60        hash_entries(hasher, &mut entries);
61    }
62}
63
64impl_predicate_downcast_type!(TrimPredicate, "steel:data_component_predicate/trim");
65
66/// Predicate over a jukebox-playable song holder.
67#[derive(Debug, Clone, PartialEq)]
68pub struct JukeboxPlayablePredicate(Option<RegistryHolderSet<JukeboxSong>>);
69
70impl JukeboxPlayablePredicate {
71    #[must_use]
72    pub const fn new(song: Option<RegistryHolderSet<JukeboxSong>>) -> Self {
73        Self(song)
74    }
75
76    #[must_use]
77    pub const fn song(&self) -> Option<&RegistryHolderSet<JukeboxSong>> {
78        self.0.as_ref()
79    }
80}
81
82impl DataComponentPredicateCodec for JukeboxPlayablePredicate {
83    fn from_nbt_value(tag: &NbtTag) -> Option<Self> {
84        let compound = tag.compound()?;
85        decode_optional(compound, "song", RegistryHolderSet::from_owned_nbt).map(Self)
86    }
87
88    fn to_nbt_value(&self) -> NbtTag {
89        let mut compound = NbtCompound::new();
90        if let Some(song) = &self.0 {
91            compound.insert("song", song.clone().to_nbt_tag());
92        }
93        NbtTag::Compound(compound)
94    }
95}
96
97impl HashComponent for JukeboxPlayablePredicate {
98    fn hash_component(&self, hasher: &mut ComponentHasher) {
99        let mut entries = Vec::new();
100        if let Some(song) = &self.0 {
101            push_hash_entry(&mut entries, "song", song);
102        }
103        hash_entries(hasher, &mut entries);
104    }
105}
106
107impl_predicate_downcast_type!(
108    JukeboxPlayablePredicate,
109    "steel:data_component_predicate/jukebox_playable"
110);
111
112/// Predicate over registered villager variants.
113#[derive(Debug, Clone, PartialEq)]
114pub struct VillagerTypePredicate(RegistryHolderSet<VillagerType>);
115
116impl VillagerTypePredicate {
117    #[must_use]
118    pub const fn new(villager_types: RegistryHolderSet<VillagerType>) -> Self {
119        Self(villager_types)
120    }
121
122    #[must_use]
123    pub const fn villager_types(&self) -> &RegistryHolderSet<VillagerType> {
124        &self.0
125    }
126}
127
128impl DataComponentPredicateCodec for VillagerTypePredicate {
129    fn from_nbt_value(tag: &NbtTag) -> Option<Self> {
130        RegistryHolderSet::from_owned_nbt(tag).map(Self)
131    }
132
133    fn to_nbt_value(&self) -> NbtTag {
134        self.0.clone().to_nbt_tag()
135    }
136}
137
138impl HashComponent for VillagerTypePredicate {
139    fn hash_component(&self, hasher: &mut ComponentHasher) {
140        self.0.hash_component(hasher);
141    }
142}
143
144impl_predicate_downcast_type!(
145    VillagerTypePredicate,
146    "steel:data_component_predicate/villager_variant"
147);