azalea_protocol/packets/game/
c_boss_event.rs

1use std::{
2    io,
3    io::{Cursor, Write},
4};
5
6use azalea_buf::{AzBuf, AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar, BufReadError};
7use azalea_chat::FormattedText;
8use azalea_core::bitset::FixedBitSet;
9use azalea_protocol_macros::ClientboundGamePacket;
10use uuid::Uuid;
11
12#[derive(Clone, Debug, AzBuf, PartialEq, ClientboundGamePacket)]
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 AzaleaRead 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}
46
47impl AzaleaWrite for Operation {
48    fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> {
49        match self {
50            Operation::Add(add) => {
51                0u32.azalea_write_var(buf)?;
52                add.azalea_write(buf)?;
53            }
54            Operation::Remove => {
55                1u32.azalea_write_var(buf)?;
56            }
57            Operation::UpdateProgress(progress) => {
58                2u32.azalea_write_var(buf)?;
59                progress.azalea_write(buf)?;
60            }
61            Operation::UpdateName(name) => {
62                3u32.azalea_write_var(buf)?;
63                name.azalea_write(buf)?;
64            }
65            Operation::UpdateStyle(style) => {
66                4u32.azalea_write_var(buf)?;
67                style.azalea_write(buf)?;
68            }
69            Operation::UpdateProperties(properties) => {
70                5u32.azalea_write_var(buf)?;
71                properties.azalea_write(buf)?;
72            }
73        }
74        Ok(())
75    }
76}
77
78#[derive(Clone, Debug, AzBuf, PartialEq)]
79pub struct AddOperation {
80    pub name: FormattedText,
81    pub progress: f32,
82    pub style: Style,
83    pub properties: Properties,
84}
85
86#[derive(Clone, Debug, AzBuf, PartialEq)]
87pub struct Style {
88    pub color: BossBarColor,
89    pub overlay: BossBarOverlay,
90}
91
92#[derive(AzBuf, Clone, Copy, Debug, PartialEq)]
93pub enum BossBarColor {
94    Pink = 0,
95    Blue = 1,
96    Red = 2,
97    Green = 3,
98    Yellow = 4,
99    Purple = 5,
100    White = 6,
101}
102
103#[derive(AzBuf, Clone, Copy, Debug, PartialEq)]
104pub enum BossBarOverlay {
105    Progress = 0,
106    Notched6 = 1,
107    Notched10 = 2,
108    Notched12 = 3,
109    Notched20 = 4,
110}
111
112#[derive(Clone, Debug, PartialEq)]
113pub struct Properties {
114    pub darken_screen: bool,
115    pub play_music: bool,
116    pub create_world_fog: bool,
117}
118
119impl AzaleaRead for Properties {
120    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
121        let set = FixedBitSet::<3>::azalea_read(buf)?;
122        Ok(Self {
123            darken_screen: set.index(0),
124            play_music: set.index(1),
125            create_world_fog: set.index(2),
126        })
127    }
128}
129
130impl AzaleaWrite for Properties {
131    fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> {
132        let mut set = FixedBitSet::<3>::new();
133        if self.darken_screen {
134            set.set(0);
135        }
136        if self.play_music {
137            set.set(1);
138        }
139        if self.create_world_fog {
140            set.set(2);
141        }
142        set.azalea_write(buf)?;
143        Ok(())
144    }
145}