Skip to main content

azalea_world/palette/
mod.rs

1mod container;
2
3#[cfg(test)]
4mod tests;
5
6use std::{
7    fmt::Debug,
8    io::{self, Cursor, Write},
9};
10
11use azalea_buf::{AzBufVar, BufReadError};
12pub use container::*;
13
14/// A representation of the different types of chunk palettes Minecraft uses.
15#[derive(Clone, Debug, PartialEq)]
16pub enum Palette<S: PalletedContainerKind> {
17    /// ID of the corresponding entry in its global palette
18    SingleValue(S),
19    // in vanilla this keeps a `size` field that might be less than the length,
20    // but i'm not sure it's actually needed?
21    Linear(Vec<S>),
22    Hashmap(Vec<S>),
23    Global,
24}
25
26impl<S: PalletedContainerKind> Palette<S> {
27    #[inline]
28    pub fn value_for(&self, id: usize) -> S {
29        match self {
30            Palette::SingleValue(v) => *v,
31            Palette::Linear(v) => v.get(id).copied().unwrap_or_default(),
32            Palette::Hashmap(v) => v.get(id).copied().unwrap_or_default(),
33            Palette::Global => S::try_from(id as u32).unwrap_or_default(),
34        }
35    }
36}
37
38impl<S: PalletedContainerKind> Palette<S> {
39    pub fn write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
40        match self {
41            Palette::SingleValue(value) => {
42                (*value).into().azalea_write_var(ctx, buf)?;
43            }
44            Palette::Linear(values) => {
45                (values.len() as u32).azalea_write_var(ctx, buf)?;
46                for value in values {
47                    (*value).into().azalea_write_var(ctx, buf)?;
48                }
49            }
50            Palette::Hashmap(values) => {
51                (values.len() as u32).azalea_write_var(ctx, buf)?;
52                for value in values {
53                    (*value).into().azalea_write_var(ctx, buf)?;
54                }
55            }
56            Palette::Global => {}
57        }
58        Ok(())
59    }
60}
61
62impl PaletteKind {
63    pub fn read<S: PalletedContainerKind>(
64        &self,
65        ctx: azalea_buf::Ctx,
66        buf: &mut Cursor<&[u8]>,
67    ) -> Result<Palette<S>, BufReadError> {
68        Ok(match self {
69            // since they're read as varints it's actually fine to just use
70            // BlockStateIntegerRepr instead of the correct type (u32)
71            PaletteKind::SingleValue => Palette::SingleValue(
72                S::try_from(u32::azalea_read_var(ctx, buf)?).unwrap_or_default(),
73            ),
74            PaletteKind::Linear => Palette::Linear(
75                Vec::<u32>::azalea_read_var(ctx, buf)?
76                    .into_iter()
77                    .map(|v| S::try_from(v).unwrap_or_default())
78                    .collect(),
79            ),
80            PaletteKind::Hashmap => Palette::Hashmap(
81                Vec::<u32>::azalea_read_var(ctx, buf)?
82                    .into_iter()
83                    .map(|v| S::try_from(v).unwrap_or_default())
84                    .collect(),
85            ),
86            PaletteKind::Global => Palette::Global,
87        })
88    }
89
90    pub fn as_empty_palette<S: PalletedContainerKind>(&self) -> Palette<S> {
91        match self {
92            PaletteKind::SingleValue => Palette::SingleValue(S::default()),
93            PaletteKind::Linear => Palette::Linear(Vec::new()),
94            PaletteKind::Hashmap => Palette::Hashmap(Vec::new()),
95            PaletteKind::Global => Palette::Global,
96        }
97    }
98}
99
100impl<S: PalletedContainerKind> From<&Palette<S>> for PaletteKind {
101    fn from(palette: &Palette<S>) -> Self {
102        match palette {
103            Palette::SingleValue(_) => PaletteKind::SingleValue,
104            Palette::Linear(_) => PaletteKind::Linear,
105            Palette::Hashmap(_) => PaletteKind::Hashmap,
106            Palette::Global => PaletteKind::Global,
107        }
108    }
109}
110
111#[derive(Clone, Debug, Eq, PartialEq)]
112pub enum PaletteKind {
113    SingleValue,
114    Linear,
115    Hashmap,
116    Global,
117}