azalea_core/registry_holder/
float_provider.rs

1use std::ops::Range;
2
3use azalea_registry::builtin::FloatProviderKind;
4use simdnbt::{
5    DeserializeError, FromNbtTag,
6    borrow::{NbtCompound, NbtTag},
7};
8
9use crate::registry_holder::get_in_compound;
10
11#[derive(Clone, Debug)]
12pub enum FloatProvider {
13    Constant(f32),
14    Uniform {
15        range: Range<f32>,
16    },
17    ClampedNormal {
18        mean: f32,
19        deviation: f32,
20        min: f32,
21        max: f32,
22    },
23    Trapezoid {
24        min: f32,
25        max: f32,
26        plateau: f32,
27    },
28}
29impl FromNbtTag for FloatProvider {
30    fn from_nbt_tag(tag: NbtTag) -> Option<Self> {
31        if let Some(f) = tag.float() {
32            return Some(Self::Constant(f));
33        }
34        if let Some(c) = tag.compound() {
35            return Self::from_compound(c).ok();
36        }
37        None
38    }
39}
40impl FloatProvider {
41    fn from_compound(nbt: NbtCompound) -> Result<Self, DeserializeError> {
42        let kind = get_in_compound(&nbt, "type")?;
43        match kind {
44            FloatProviderKind::Constant => Ok(Self::Constant(get_in_compound(&nbt, "value")?)),
45            FloatProviderKind::Uniform => {
46                let min_inclusive = get_in_compound(&nbt, "min_inclusive")?;
47                let max_exclusive = get_in_compound(&nbt, "max_exclusive")?;
48                Ok(Self::Uniform {
49                    range: min_inclusive..max_exclusive,
50                })
51            }
52            FloatProviderKind::ClampedNormal => {
53                let mean = get_in_compound(&nbt, "mean")?;
54                let deviation = get_in_compound(&nbt, "deviation")?;
55                let min = get_in_compound(&nbt, "min")?;
56                let max = get_in_compound(&nbt, "max")?;
57                Ok(Self::ClampedNormal {
58                    mean,
59                    deviation,
60                    min,
61                    max,
62                })
63            }
64            FloatProviderKind::Trapezoid => {
65                let min = get_in_compound(&nbt, "min")?;
66                let max = get_in_compound(&nbt, "max")?;
67                let plateau = get_in_compound(&nbt, "plateau")?;
68                Ok(Self::Trapezoid { min, max, plateau })
69            }
70        }
71    }
72}