Skip to main content

steel_registry/data_components/components/
map_post_processing.rs

1//! Vanilla `minecraft:map_post_processing` transient item component.
2
3use std::io::{Cursor, Result, Write};
4
5use steel_utils::codec::VarInt;
6use steel_utils::serial::{ReadFrom, WriteTo};
7
8/// Operation applied to a filled map after crafting.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum MapPostProcessing {
11    Lock,
12    Scale,
13}
14
15impl MapPostProcessing {
16    #[must_use]
17    pub const fn id(self) -> i32 {
18        match self {
19            Self::Lock => 0,
20            Self::Scale => 1,
21        }
22    }
23
24    const fn from_id(id: i32) -> Self {
25        match id {
26            1 => Self::Scale,
27            _ => Self::Lock,
28        }
29    }
30}
31
32impl WriteTo for MapPostProcessing {
33    fn write(&self, writer: &mut impl Write) -> Result<()> {
34        VarInt(self.id()).write(writer)
35    }
36}
37
38impl ReadFrom for MapPostProcessing {
39    fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
40        Ok(Self::from_id(VarInt::read(data)?.0))
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use std::io::Cursor;
47
48    use steel_utils::codec::VarInt;
49    use steel_utils::serial::{ReadFrom as _, WriteTo as _};
50
51    use super::MapPostProcessing;
52
53    #[test]
54    fn network_ids_match_vanilla() {
55        for (value, id) in [(MapPostProcessing::Lock, 0), (MapPostProcessing::Scale, 1)] {
56            let mut encoded = Vec::new();
57            value.write(&mut encoded).expect("value should encode");
58            assert_eq!(
59                VarInt::read(&mut Cursor::new(encoded.as_slice()))
60                    .expect("encoded ID should decode")
61                    .0,
62                id
63            );
64            assert_eq!(
65                MapPostProcessing::read(&mut Cursor::new(encoded.as_slice()))
66                    .expect("map post-processing value should decode"),
67                value
68            );
69        }
70    }
71
72    #[test]
73    fn out_of_bounds_network_ids_fall_back_to_lock() {
74        for id in [-1, 2, i32::MAX] {
75            let mut encoded = Vec::new();
76            VarInt(id).write(&mut encoded).expect("id should encode");
77            assert_eq!(
78                MapPostProcessing::read(&mut Cursor::new(encoded.as_slice()))
79                    .expect("out-of-bounds ID should decode"),
80                MapPostProcessing::Lock
81            );
82        }
83    }
84}