aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/packets/game/clientbound_player_info_packet.rs
blob: cb17f1f5121013e1b7296eaaadaab5e1c48e2fec (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::mc_buf::{McBufReadable, McBufWritable, Readable, Writable};
use azalea_chat::component::Component;
use packet_macros::{GamePacket, McBuf};
use std::io::{Read, Write};
use uuid::Uuid;

#[derive(Clone, Debug, McBuf, GamePacket)]
pub struct ClientboundPlayerInfoPacket {
    pub action: Action,
}

#[derive(Clone, Debug)]
pub enum Action {
    AddPlayer(Vec<AddPlayer>),
    UpdateGameMode(Vec<UpdateGameMode>),
    UpdateLatency(Vec<UpdateLatency>),
    UpdateDisplayName(Vec<UpdateDisplayName>),
    RemovePlayer(Vec<RemovePlayer>),
}

#[derive(Clone, Debug, McBuf)]
pub struct PlayerProperty {
    name: String,
    value: String,
    signature: Option<String>,
}

#[derive(Clone, Debug, McBuf)]
pub struct AddPlayer {
    uuid: Uuid,
    name: String,
    properties: Vec<PlayerProperty>,
    #[var]
    gamemode: u32,
    #[var]
    ping: i32,
    display_name: Option<Component>,
}

#[derive(Clone, Debug, McBuf)]
pub struct UpdateGameMode {
    uuid: Uuid,
    #[var]
    gamemode: u32,
}

#[derive(Clone, Debug, McBuf)]
pub struct UpdateLatency {
    uuid: Uuid,
    #[var]
    ping: i32,
}

#[derive(Clone, Debug, McBuf)]
pub struct UpdateDisplayName {
    uuid: Uuid,
    display_name: Option<Component>,
}
#[derive(Clone, Debug, McBuf)]
pub struct RemovePlayer {
    uuid: Uuid,
}

impl McBufReadable for Action {
    fn read_into(buf: &mut impl Read) -> Result<Self, String> {
        let id = buf.read_byte()?;
        Ok(match id {
            0 => Action::AddPlayer(Vec::<AddPlayer>::read_into(buf)?),
            1 => Action::UpdateGameMode(Vec::<UpdateGameMode>::read_into(buf)?),
            2 => Action::UpdateLatency(Vec::<UpdateLatency>::read_into(buf)?),
            3 => Action::UpdateDisplayName(Vec::<UpdateDisplayName>::read_into(buf)?),
            4 => Action::RemovePlayer(Vec::<RemovePlayer>::read_into(buf)?),
            _ => panic!("Unknown player info action id: {}", id),
        })
    }
}
impl McBufWritable for Action {
    fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
        buf.write_byte(match self {
            Action::AddPlayer(_) => 0,
            Action::UpdateGameMode(_) => 1,
            Action::UpdateLatency(_) => 2,
            Action::UpdateDisplayName(_) => 3,
            Action::RemovePlayer(_) => 4,
        })?;
        match self {
            Action::AddPlayer(players) => players.write_into(buf)?,
            Action::UpdateGameMode(players) => players.write_into(buf)?,
            Action::UpdateLatency(players) => players.write_into(buf)?,
            Action::UpdateDisplayName(players) => players.write_into(buf)?,
            Action::RemovePlayer(players) => players.write_into(buf)?,
        }
        Ok(())
    }
}