Skip to main content

steel_registry/stat/
custom.rs

1use rustc_hash::FxHashMap;
2use steel_utils::{DowncastType, DowncastTypeKey, Identifier};
3
4/// A custom stat definition, which represents a particular stat in the *General* tab in
5/// the Statistics menu. This just contains an identifier which identifies the stat.
6///
7/// These stats are called 'custom' because they are manually defined. Vanilla defines its
8/// own custom stats, which have been extracted to generate definitions used by Steel.
9///
10/// Therefore, 'custom' here should not be misinterpreted to mean Steel's custom stats.
11#[derive(Debug)]
12pub struct CustomStat {
13    pub key: Identifier,
14}
15
16pub type CustomStatRef = &'static CustomStat;
17
18/// Contains all the stats that can be found in the *General* tab in the Statistics menu.
19///
20/// These stats are called 'custom' because they are manually defined. Vanilla defines its
21/// own custom stats, which have been extracted to generate definitions used by Steel.
22///
23/// Therefore, 'custom' here should not be misinterpreted to mean Steel's custom stats.
24pub struct CustomStatRegistry {
25    custom_stats_by_id: Vec<CustomStatRef>,
26    custom_stats_by_key: FxHashMap<Identifier, usize>,
27    allows_registering: bool,
28}
29
30// SAFETY: This Steel-owned key uniquely identifies the custom stat registry.
31unsafe impl DowncastType for CustomStatRegistry {
32    const TYPE_KEY: DowncastTypeKey = DowncastTypeKey::new("steel:registry/custom_stat");
33}
34
35impl CustomStatRegistry {
36    #[must_use]
37    pub fn new() -> Self {
38        Self {
39            custom_stats_by_id: Vec::new(),
40            custom_stats_by_key: FxHashMap::default(),
41            allows_registering: true,
42        }
43    }
44}
45
46crate::impl_standard_methods!(
47    CustomStatRegistry,
48    CustomStatRef,
49    custom_stats_by_id,
50    custom_stats_by_key,
51    allows_registering
52);
53
54crate::impl_registry!(
55    CustomStatRegistry,
56    CustomStat,
57    custom_stats_by_id,
58    custom_stats_by_key,
59    custom_stats
60);