Skip to main content

azalea_protocol/packets/game/
c_boss_event.rs

1use std::{
2    io,
3    io::{Cursor, Write},
4};
5
6use azalea_buf::{AzBuf, AzBufVar, BufReadError, BufReadErrorRepr};
7use azalea_chat::FormattedText;
8use azalea_core::bitset::FixedBitSet;
9use azalea_protocol_macros::ClientboundGamePacket;
10use uuid::Uuid;
11
12#[derive(AzBuf, ClientboundGamePacket, Clone, Debug, PartialEq)]
13pub struct ClientboundBossEvent {
14    pub id: Uuid,
15    pub operation: Operation,
16}
17
18#[derive(Clone, Debug, PartialEq)]
19pub enum Operation {
20    Add(AddOperation),
21    Remove,
22    UpdateProgress(f32),
23    UpdateName(FormattedText),
24    UpdateStyle(Style),
25    UpdateProperties(Properties),
26}
27
28impl AzBuf for Operation {
29    fn azalea_read(ctx: azalea_buf::Ctx, buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
30        let operation_id = u32::azalea_read_var(ctx, buf)?;
31        Ok(match operation_id {
32            0 => Operation::Add(AddOperation::azalea_read(ctx, buf)?),
33            1 => Operation::Remove,
34            2 => Operation::UpdateProgress(f32::azalea_read(ctx, buf)?),
35            3 => Operation::UpdateName(FormattedText::azalea_read(ctx, buf)?),
36            4 => Operation::UpdateStyle(Style::azalea_read(ctx, buf)?),
37            5 => Operation::UpdateProperties(Properties::azalea_read(ctx, buf)?),
38            _ => {
39                return Err(BufReadErrorRepr::UnexpectedEnumVariant {
40                    id: operation_id as i32,
41                }
42                .into());
43            }
44        })
45    }
46    fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
47        match self {
48            Operation::Add(add) => {
49                0u32.azalea_write_var(ctx, buf)?;
50                add.azalea_write(ctx, buf)?;
51            }
52            Operation::Remove => {
53                1u32.azalea_write_var(ctx, buf)?;
54            }
55            Operation::UpdateProgress(progress) => {
56                2u32.azalea_write_var(ctx, buf)?;
57                progress.azalea_write(ctx, buf)?;
58            }
59            Operation::UpdateName(name) => {
60                3u32.azalea_write_var(ctx, buf)?;
61                name.azalea_write(ctx, buf)?;
62            }
63            Operation::UpdateStyle(style) => {
64                4u32.azalea_write_var(ctx, buf)?;
65                style.azalea_write(ctx, buf)?;
66            }
67            Operation::UpdateProperties(properties) => {
68                5u32.azalea_write_var(ctx, buf)?;
69                properties.azalea_write(ctx, buf)?;
70            }
71        }
72        Ok(())
73    }
74}
75
76#[derive(AzBuf, Clone, Debug, PartialEq)]
77pub struct AddOperation {
78    pub name: FormattedText,
79    pub progress: f32,
80    pub style: Style,
81    pub properties: Properties,
82}
83
84#[derive(AzBuf, Clone, Debug, PartialEq)]
85pub struct Style {
86    pub color: BossBarColor,
87    pub overlay: BossBarOverlay,
88}
89
90#[derive(AzBuf, Clone, Copy, Debug, PartialEq)]
91pub enum BossBarColor {
92    Pink = 0,
93    Blue = 1,
94    Red = 2,
95    Green = 3,
96    Yellow = 4,
97    Purple = 5,
98    White = 6,
99}
100
101#[derive(AzBuf, Clone, Copy, Debug, PartialEq)]
102pub enum BossBarOverlay {
103    Progress = 0,
104    Notched6 = 1,
105    Notched10 = 2,
106    Notched12 = 3,
107    Notched20 = 4,
108}
109
110#[derive(Clone, Debug, PartialEq)]
111pub struct Properties {
112    pub darken_screen: bool,
113    pub play_music: bool,
114    pub create_world_fog: bool,
115}
116
117impl AzBuf for Properties {
118    fn azalea_read(ctx: azalea_buf::Ctx, buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
119        let set = FixedBitSet::<3>::azalea_read(ctx, buf)?;
120        Ok(Self {
121            darken_screen: set.index(0),
122            play_music: set.index(1),
123            create_world_fog: set.index(2),
124        })
125    }
126    fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
127        let mut set = FixedBitSet::<3>::new();
128        if self.darken_screen {
129            set.set(0);
130        }
131        if self.play_music {
132            set.set(1);
133        }
134        if self.create_world_fog {
135            set.set(2);
136        }
137        set.azalea_write(ctx, buf)?;
138        Ok(())
139    }
140}