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