forked from Quozul/PicoLimbo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboss_bar_packet.rs
More file actions
132 lines (125 loc) · 3.29 KB
/
boss_bar_packet.rs
File metadata and controls
132 lines (125 loc) · 3.29 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use minecraft_protocol::prelude::*;
use pico_text_component::prelude::Component;
#[derive(PacketOut)]
pub struct BossBarPacket {
uuid: UuidAsLongs,
action: BossBarAction,
}
impl BossBarPacket {
pub fn add(
title: &Component,
health: f32,
color: BossBarColor,
division: BossBarDivision,
) -> Self {
let uuid = Uuid::new_v4();
Self {
uuid: uuid.into(),
action: BossBarAction::Add {
title: title.clone(),
health,
color: VarInt::from(color as i32),
division: VarInt::from(division as i32),
flags: 0,
},
}
}
}
#[allow(dead_code)]
enum BossBarAction {
Add {
title: Component,
health: f32,
color: VarInt,
division: VarInt,
/// Bit mask. 0x01: should darken sky, 0x02: is dragon bar (used to play end music), 0x04: create fog (previously was also controlled by 0x02).
flags: u8,
},
Remove,
UpdateHealth {
health: f32,
},
UpdateTitle {
title: Component,
},
UpdateStyle {
color: VarInt,
division: VarInt,
},
UpdateFlags {
flags: u8,
},
}
#[derive(Debug, Clone, Copy)]
#[repr(i32)]
pub enum BossBarColor {
Pink = 0,
Blue = 1,
Red = 2,
Green = 3,
Yellow = 4,
Purple = 5,
White = 6,
}
#[derive(Debug, Clone, Copy)]
#[repr(i32)]
pub enum BossBarDivision {
NoDivision = 0,
SixNotches = 1,
TenNotches = 2,
TwelveNotches = 3,
TwentyNotches = 4,
}
impl BossBarAction {
fn type_id(&self) -> VarInt {
match self {
Self::Add { .. } => 0.into(),
Self::Remove => 1.into(),
Self::UpdateHealth { .. } => 2.into(),
Self::UpdateTitle { .. } => 3.into(),
Self::UpdateStyle { .. } => 4.into(),
Self::UpdateFlags { .. } => 5.into(),
}
}
}
impl EncodePacket for BossBarAction {
fn encode(
&self,
writer: &mut BinaryWriter,
protocol_version: ProtocolVersion,
) -> Result<(), BinaryWriterError> {
self.type_id().encode(writer, protocol_version)?;
match self {
Self::Add {
title,
health,
color,
division,
flags,
} => {
title.encode(writer, protocol_version)?;
health.encode(writer, protocol_version)?;
color.encode(writer, protocol_version)?;
division.encode(writer, protocol_version)?;
flags.encode(writer, protocol_version)?;
}
Self::Remove => {
// Nothing to encode
}
Self::UpdateHealth { health } => {
health.encode(writer, protocol_version)?;
}
Self::UpdateTitle { title } => {
title.encode(writer, protocol_version)?;
}
Self::UpdateStyle { color, division } => {
color.encode(writer, protocol_version)?;
division.encode(writer, protocol_version)?;
}
Self::UpdateFlags { flags } => {
flags.encode(writer, protocol_version)?;
}
}
Ok(())
}
}