aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/packets/status/c_status_response.rs
blob: ef2836434cdac31bf7f75b0e9f7e6916ea84b99f (plain)
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
use std::io::{self, Cursor, Write};

use azalea_buf::{AzBuf, BufReadError};
use azalea_chat::FormattedText;
use azalea_protocol_macros::ClientboundStatusPacket;
use serde::{Deserialize, Serialize};
use serde_json::value::Serializer;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Version {
    pub name: String,
    pub protocol: i32,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct SamplePlayer {
    pub id: String,
    pub name: String,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Players {
    pub max: i32,
    pub online: i32,
    #[serde(default)]
    pub sample: Vec<SamplePlayer>,
}

// the entire packet is just json, which is why it has deserialize
#[derive(ClientboundStatusPacket, Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ClientboundStatusResponse {
    pub description: FormattedText,
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub favicon: Option<String>,
    pub players: Players,
    pub version: Version,
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "enforcesSecureChat")]
    pub enforces_secure_chat: Option<bool>,
}

impl AzBuf for ClientboundStatusResponse {
    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<ClientboundStatusResponse, BufReadError> {
        let status_string = String::azalea_read(buf)?;
        let status_json: serde_json::Value = serde_json::from_str(status_string.as_str())?;

        Ok(ClientboundStatusResponse::deserialize(status_json)?)
    }
    fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> {
        let status_string = ClientboundStatusResponse::serialize(self, Serializer)
            .unwrap()
            .to_string();
        status_string.azalea_write(buf)?;
        Ok(())
    }
}