Skip to main content

steel_protocol/packets/status/
c_status_response.rs

1use serde::Serialize;
2use steel_macros::{ClientPacket, WriteTo};
3use steel_registry::packets::status::C_STATUS_RESPONSE;
4
5#[derive(Serialize, Clone, Debug)]
6pub struct Sample {
7    /// The player's name.
8    pub name: String,
9    /// The player's UUID.
10    pub id: String,
11}
12
13#[derive(Clone, Debug, Serialize)]
14pub struct Players {
15    pub max: i32,
16    pub online: i32,
17    pub sample: Vec<Sample>,
18}
19
20#[derive(Clone, Debug, Serialize)]
21pub struct Version {
22    pub name: &'static str,
23    pub protocol: i32,
24}
25
26#[derive(Clone, Debug, Serialize)]
27#[serde(rename_all = "camelCase")]
28pub struct Status {
29    pub description: String,
30    pub players: Option<Players>,
31    pub version: Option<Version>,
32    pub favicon: Option<String>,
33    pub enforces_secure_chat: bool,
34}
35
36#[derive(ClientPacket, WriteTo, Clone, Debug)]
37#[packet_id(Status = C_STATUS_RESPONSE)]
38pub struct CStatusResponse {
39    #[write(as = Json)]
40    status: Status,
41}
42
43impl CStatusResponse {
44    #[must_use]
45    pub const fn new(status: Status) -> Self {
46        Self { status }
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::Status;
53
54    #[test]
55    fn secure_chat_enforcement_uses_vanilla_json_name() {
56        let status = Status {
57            description: String::new(),
58            players: None,
59            version: None,
60            favicon: None,
61            enforces_secure_chat: true,
62        };
63        let json = serde_json::to_value(status).expect("status should serialize");
64
65        assert_eq!(
66            json.get("enforcesSecureChat"),
67            Some(&serde_json::json!(true))
68        );
69        assert!(json.get("enforce_secure_chat").is_none());
70    }
71}