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
use std::borrow::Cow;

use bevy_ecs::prelude::{Bundle, Component};
use derive_more::{Deref, DerefMut};
use valence_entity::EntityLayerId;
use valence_server::protocol::packets::play::boss_bar_s2c::{
    BossBarAction, BossBarColor, BossBarDivision, BossBarFlags,
};
use valence_server::{Text, UniqueId};

/// The bundle of components that make up a boss bar.
#[derive(Bundle, Default)]
pub struct BossBarBundle {
    pub id: UniqueId,
    pub title: BossBarTitle,
    pub health: BossBarHealth,
    pub style: BossBarStyle,
    pub flags: BossBarFlags,
    pub layer: EntityLayerId,
}

/// The title of a boss bar.
#[derive(Component, Clone, Default, Deref, DerefMut)]
pub struct BossBarTitle(pub Text);

impl ToPacketAction for BossBarTitle {
    fn to_packet_action(&self) -> BossBarAction {
        BossBarAction::UpdateTitle(Cow::Borrowed(&self.0))
    }
}

/// The health of a boss bar.
#[derive(Component, Default, Deref, DerefMut)]
pub struct BossBarHealth(pub f32);

impl ToPacketAction for BossBarHealth {
    fn to_packet_action(&self) -> BossBarAction {
        BossBarAction::UpdateHealth(self.0)
    }
}

/// The style of a boss bar. This includes the color and division of the boss
/// bar.
#[derive(Component, Default)]
pub struct BossBarStyle {
    pub color: BossBarColor,
    pub division: BossBarDivision,
}

impl ToPacketAction for BossBarStyle {
    fn to_packet_action(&self) -> BossBarAction {
        BossBarAction::UpdateStyle(self.color, self.division)
    }
}

impl ToPacketAction for BossBarFlags {
    fn to_packet_action(&self) -> BossBarAction {
        BossBarAction::UpdateFlags(*self)
    }
}

/// Trait for converting a component to a boss bar action.
pub(crate) trait ToPacketAction {
    fn to_packet_action(&self) -> BossBarAction;
}