azalea_protocol/common/
client_information.rs

1use std::io::{self, Cursor};
2
3use azalea_buf::AzBuf;
4use azalea_core::bitset::FixedBitSet;
5use azalea_entity::HumanoidArm;
6
7/// Some of the "settings" for this client that are sent to the server,
8/// including render distance.
9///
10/// This should only be present on local players.
11#[cfg_attr(feature = "bevy_ecs", derive(bevy_ecs::component::Component))]
12#[derive(AzBuf, Clone, Debug, Eq, PartialEq)]
13pub struct ClientInformation {
14    /// The locale of the client, formatted like "en_us".
15    pub language: String,
16    /// The view distance of the client in chunks, same as the render distance
17    /// in-game.
18    pub view_distance: u8,
19    /// The types of chat messages the client wants to receive.
20    ///
21    /// Note that this is enforced by the server, and many servers ignore this.
22    pub chat_visibility: ChatVisibility,
23    /// Whether the messages sent from the server should have colors.
24    ///
25    /// Note that many servers ignore this and always send colored messages.
26    pub chat_colors: bool,
27    pub model_customization: ModelCustomization,
28    pub main_hand: HumanoidArm,
29    pub text_filtering_enabled: bool,
30    /// Whether the client should show up as "Anonymous Player" in the server
31    /// list.
32    pub allows_listing: bool,
33    pub particle_status: ParticleStatus,
34}
35
36impl Default for ClientInformation {
37    fn default() -> Self {
38        Self {
39            language: "en_us".to_owned(),
40            view_distance: 8,
41            chat_visibility: ChatVisibility::default(),
42            chat_colors: true,
43            model_customization: ModelCustomization::default(),
44            main_hand: HumanoidArm::Right,
45            text_filtering_enabled: false,
46            allows_listing: false,
47            particle_status: ParticleStatus::default(),
48        }
49    }
50}
51
52#[derive(AzBuf, Clone, Copy, Debug, Default, Eq, PartialEq)]
53pub enum ChatVisibility {
54    /// All chat messages should be sent to the client.
55    #[default]
56    Full = 0,
57    /// Chat messages from other players should be not sent to the client, only
58    /// messages from the server like "Player joined the game" should be sent.
59    System = 1,
60    /// No chat messages should be sent to the client.
61    Hidden = 2,
62}
63
64#[derive(Clone, Copy, Debug, Eq, PartialEq)]
65pub struct ModelCustomization {
66    pub cape: bool,
67    pub jacket: bool,
68    pub left_sleeve: bool,
69    pub right_sleeve: bool,
70    pub left_pants: bool,
71    pub right_pants: bool,
72    pub hat: bool,
73}
74
75#[derive(AzBuf, Clone, Copy, Debug, Default, Eq, PartialEq)]
76pub enum ParticleStatus {
77    #[default]
78    All,
79    Decreased,
80    Minimal,
81}
82
83impl Default for ModelCustomization {
84    fn default() -> Self {
85        Self {
86            cape: true,
87            jacket: true,
88            left_sleeve: true,
89            right_sleeve: true,
90            left_pants: true,
91            right_pants: true,
92            hat: true,
93        }
94    }
95}
96
97impl AzBuf for ModelCustomization {
98    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
99        let set = FixedBitSet::<7>::azalea_read(buf)?;
100        Ok(Self {
101            cape: set.index(0),
102            jacket: set.index(1),
103            left_sleeve: set.index(2),
104            right_sleeve: set.index(3),
105            left_pants: set.index(4),
106            right_pants: set.index(5),
107            hat: set.index(6),
108        })
109    }
110    fn azalea_write(&self, buf: &mut impl io::Write) -> io::Result<()> {
111        let mut set = FixedBitSet::<7>::new();
112        if self.cape {
113            set.set(0);
114        }
115        if self.jacket {
116            set.set(1);
117        }
118        if self.left_sleeve {
119            set.set(2);
120        }
121        if self.right_sleeve {
122            set.set(3);
123        }
124        if self.left_pants {
125            set.set(4);
126        }
127        if self.right_pants {
128            set.set(5);
129        }
130        if self.hat {
131            set.set(6);
132        }
133        set.azalea_write(buf)
134    }
135}
136
137#[cfg(test)]
138mod tests {
139    use std::io::Cursor;
140
141    use azalea_buf::AzBuf;
142
143    use super::*;
144
145    #[test]
146    fn test_client_information() {
147        {
148            let data = ClientInformation::default();
149            let mut buf = Vec::new();
150            data.azalea_write(&mut buf).unwrap();
151            let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf);
152
153            let read_data = ClientInformation::azalea_read(&mut data_cursor).unwrap();
154            assert_eq!(read_data, data);
155        }
156
157        let data = ClientInformation {
158            language: "en_gb".to_owned(),
159            view_distance: 24,
160            chat_visibility: ChatVisibility::Hidden,
161            chat_colors: false,
162            model_customization: ModelCustomization {
163                cape: false,
164                jacket: false,
165                left_sleeve: true,
166                right_sleeve: false,
167                left_pants: true,
168                right_pants: false,
169                hat: true,
170            },
171            main_hand: HumanoidArm::Left,
172            text_filtering_enabled: true,
173            allows_listing: true,
174            particle_status: ParticleStatus::Decreased,
175        };
176        let mut buf = Vec::new();
177        data.azalea_write(&mut buf).unwrap();
178        let mut data_cursor: Cursor<&[u8]> = Cursor::new(&buf);
179
180        let read_data = ClientInformation::azalea_read(&mut data_cursor).unwrap();
181        assert_eq!(read_data, data);
182    }
183}