Skip to main content

azalea_protocol/common/
number_formatting.rs

1//! Contains a few ways to style numbers. At the time of writing, Minecraft only
2//! uses this for rendering scoreboard objectives.
3
4use std::io::{self, Cursor, Write};
5
6use azalea_buf::AzBuf;
7use azalea_chat::FormattedText;
8use azalea_registry::builtin::NumberFormatKind;
9use simdnbt::owned::Nbt;
10
11#[derive(Clone, Debug, PartialEq)]
12pub enum NumberFormat {
13    Blank,
14    Styled { style: Nbt },
15    Fixed { value: FormattedText },
16}
17
18impl AzBuf for NumberFormat {
19    fn azalea_read(
20        ctx: azalea_buf::Ctx,
21        buf: &mut Cursor<&[u8]>,
22    ) -> Result<Self, azalea_buf::BufReadError> {
23        let kind = NumberFormatKind::azalea_read(ctx, buf)?;
24        match kind {
25            NumberFormatKind::Blank => Ok(NumberFormat::Blank),
26            NumberFormatKind::Styled => Ok(NumberFormat::Styled {
27                style: simdnbt::owned::read(buf)?,
28            }),
29            NumberFormatKind::Fixed => Ok(NumberFormat::Fixed {
30                value: FormattedText::azalea_read(ctx, buf)?,
31            }),
32        }
33    }
34    fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
35        match self {
36            NumberFormat::Blank => NumberFormatKind::Blank.azalea_write(ctx, buf)?,
37            NumberFormat::Styled { style } => {
38                NumberFormatKind::Styled.azalea_write(ctx, buf)?;
39                style.azalea_write(ctx, buf)?;
40            }
41            NumberFormat::Fixed { value } => {
42                NumberFormatKind::Fixed.azalea_write(ctx, buf)?;
43                value.azalea_write(ctx, buf)?;
44            }
45        }
46        Ok(())
47    }
48}