Skip to main content

steel_protocol/packets/game/
c_tab_list.rs

1use steel_macros::ClientPacket;
2use steel_registry::packets::play::C_TAB_LIST;
3use text_components::{TextComponent, resolving::TextResolutor};
4
5/// Packet to set the tab list header and footer.
6/// This allows servers to display custom text above and below the player list.
7#[derive(ClientPacket, Debug, Clone)]
8#[packet_id(Play = C_TAB_LIST)]
9pub struct CTabList {
10    /// The header text component (displayed above the player list)
11    pub header: TextComponent,
12    /// The footer text component (displayed below the player list)
13    pub footer: TextComponent,
14}
15
16impl CTabList {
17    /// Creates a new tab list packet with the specified header and footer.
18    #[must_use]
19    pub fn new<T: TextResolutor>(
20        header: &TextComponent,
21        footer: &TextComponent,
22        player: &T,
23    ) -> Self {
24        Self {
25            header: header.resolve(player),
26            footer: footer.resolve(player),
27        }
28    }
29
30    /// Creates a tab list packet with empty header and footer (clears them).
31    #[must_use]
32    pub const fn empty() -> Self {
33        Self {
34            header: TextComponent::new(),
35            footer: TextComponent::new(),
36        }
37    }
38
39    /// Creates a tab list packet with only a header.
40    #[must_use]
41    pub fn header_only<T: TextResolutor>(header: &TextComponent, player: &T) -> Self {
42        Self {
43            header: header.resolve(player),
44            footer: TextComponent::new(),
45        }
46    }
47
48    /// Creates a tab list packet with only a footer.
49    #[must_use]
50    pub fn footer_only<T: TextResolutor>(footer: &TextComponent, player: &T) -> Self {
51        Self {
52            header: TextComponent::new(),
53            footer: footer.resolve(player),
54        }
55    }
56}
57
58impl steel_utils::serial::WriteTo for CTabList {
59    fn write(&self, writer: &mut impl std::io::Write) -> std::io::Result<()> {
60        self.header.write(writer)?;
61        self.footer.write(writer)?;
62        Ok(())
63    }
64}