azalea_chat/
numbers.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Contains a few ways to style numbers. At the time of writing, Minecraft only
//! uses this for rendering scoreboard objectives.

use std::io::{Cursor, Write};

#[cfg(feature = "azalea-buf")]
use azalea_buf::{McBufReadable, McBufWritable};
use azalea_registry::NumberFormatKind;
use simdnbt::owned::Nbt;

use crate::FormattedText;

#[derive(Clone, Debug)]
pub enum NumberFormat {
    Blank,
    Styled { style: Nbt },
    Fixed { value: FormattedText },
}

#[cfg(feature = "azalea-buf")]
impl McBufReadable for NumberFormat {
    fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
        let kind = NumberFormatKind::read_from(buf)?;
        match kind {
            NumberFormatKind::Blank => Ok(NumberFormat::Blank),
            NumberFormatKind::Styled => Ok(NumberFormat::Styled {
                style: simdnbt::owned::read(buf)?,
            }),
            NumberFormatKind::Fixed => Ok(NumberFormat::Fixed {
                value: FormattedText::read_from(buf)?,
            }),
        }
    }
}

#[cfg(feature = "azalea-buf")]
impl McBufWritable for NumberFormat {
    fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
        match self {
            NumberFormat::Blank => NumberFormatKind::Blank.write_into(buf)?,
            NumberFormat::Styled { style } => {
                NumberFormatKind::Styled.write_into(buf)?;
                style.write_into(buf)?;
            }
            NumberFormat::Fixed { value } => {
                NumberFormatKind::Fixed.write_into(buf)?;
                value.write_into(buf)?;
            }
        }
        Ok(())
    }
}