chunkedge_protocol/packets/play/
set_display_objective_s2c.rs

1use bevy_ecs::prelude::Component;
2use chunkedge_binary::{Decode, Encode};
3
4use super::set_player_team_s2c::TeamColor;
5use crate::Packet;
6
7#[derive(Copy, Clone, Debug, Encode, Decode, Packet)]
8pub struct SetDisplayObjectiveS2c<'a> {
9    pub position: ScoreboardPosition,
10    pub score_name: &'a str,
11}
12
13/// Defines where a scoreboard is displayed.
14#[derive(Copy, Clone, PartialEq, Eq, Debug, Component, Default)]
15pub enum ScoreboardPosition {
16    /// Display the scoreboard in the player list (the one you see when you
17    /// press tab), as a yellow number next to players' names.
18    List,
19    /// Display the scoreboard on the sidebar.
20    #[default]
21    Sidebar,
22    /// Display the scoreboard below players' name tags in the world.
23    BelowName,
24    /// Display the scoreboard on the sidebar, visible only to one team.
25    SidebarTeam(TeamColor),
26}
27
28impl Encode for ScoreboardPosition {
29    fn encode(&self, w: impl std::io::Write) -> anyhow::Result<()> {
30        match self {
31            ScoreboardPosition::List => 0_u8.encode(w),
32            ScoreboardPosition::Sidebar => 1_u8.encode(w),
33            ScoreboardPosition::BelowName => 2_u8.encode(w),
34            ScoreboardPosition::SidebarTeam(TeamColor::Black) => 3_u8.encode(w),
35            ScoreboardPosition::SidebarTeam(TeamColor::DarkBlue) => 4_u8.encode(w),
36            ScoreboardPosition::SidebarTeam(TeamColor::DarkGreen) => 5_u8.encode(w),
37            ScoreboardPosition::SidebarTeam(TeamColor::DarkCyan) => 6_u8.encode(w),
38            ScoreboardPosition::SidebarTeam(TeamColor::DarkRed) => 7_u8.encode(w),
39            ScoreboardPosition::SidebarTeam(TeamColor::Purple) => 8_u8.encode(w),
40            ScoreboardPosition::SidebarTeam(TeamColor::Gold) => 9_u8.encode(w),
41            ScoreboardPosition::SidebarTeam(TeamColor::Gray) => 10_u8.encode(w),
42            ScoreboardPosition::SidebarTeam(TeamColor::DarkGray) => 11_u8.encode(w),
43            ScoreboardPosition::SidebarTeam(TeamColor::Blue) => 12_u8.encode(w),
44            ScoreboardPosition::SidebarTeam(TeamColor::BrightGreen) => 13_u8.encode(w),
45            ScoreboardPosition::SidebarTeam(TeamColor::Cyan) => 14_u8.encode(w),
46            ScoreboardPosition::SidebarTeam(TeamColor::Red) => 15_u8.encode(w),
47            ScoreboardPosition::SidebarTeam(TeamColor::Pink) => 16_u8.encode(w),
48            ScoreboardPosition::SidebarTeam(TeamColor::Yellow) => 17_u8.encode(w),
49            ScoreboardPosition::SidebarTeam(TeamColor::White) => 18_u8.encode(w),
50            ScoreboardPosition::SidebarTeam(_) => {
51                Err(anyhow::anyhow!("Invalid scoreboard display position"))
52            }
53        }
54    }
55}
56
57impl<'a> Decode<'a> for ScoreboardPosition {
58    fn decode(r: &mut &'a [u8]) -> anyhow::Result<Self> {
59        let value = u8::decode(r)?;
60        match value {
61            0 => Ok(ScoreboardPosition::List),
62            1 => Ok(ScoreboardPosition::Sidebar),
63            2 => Ok(ScoreboardPosition::BelowName),
64            3 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::Black)),
65            4 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::DarkBlue)),
66            5 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::DarkGreen)),
67            6 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::DarkCyan)),
68            7 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::DarkRed)),
69            8 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::Purple)),
70            9 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::Gold)),
71            10 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::Gray)),
72            11 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::DarkGray)),
73            12 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::Blue)),
74            13 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::BrightGreen)),
75            14 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::Cyan)),
76            15 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::Red)),
77            16 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::Pink)),
78            17 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::Yellow)),
79            18 => Ok(ScoreboardPosition::SidebarTeam(TeamColor::White)),
80            _ => Err(anyhow::anyhow!("Invalid scoreboard display position")),
81        }
82    }
83}