azalea_protocol/packets/game/
c_set_player_team.rs

1use std::io::{Cursor, Write};
2
3use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError};
4use azalea_chat::{style::ChatFormatting, FormattedText};
5use azalea_protocol_macros::ClientboundGamePacket;
6
7#[derive(Clone, Debug, AzBuf, ClientboundGamePacket)]
8pub struct ClientboundSetPlayerTeam {
9    pub name: String,
10    pub method: Method,
11}
12
13#[derive(Clone, Debug)]
14pub enum Method {
15    Add((Parameters, PlayerList)),
16    Remove,
17    Change(Parameters),
18    Join(PlayerList),
19    Leave(PlayerList),
20}
21
22impl AzaleaRead for Method {
23    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
24        Ok(match u8::azalea_read(buf)? {
25            0 => Method::Add((Parameters::azalea_read(buf)?, PlayerList::azalea_read(buf)?)),
26            1 => Method::Remove,
27            2 => Method::Change(Parameters::azalea_read(buf)?),
28            3 => Method::Join(PlayerList::azalea_read(buf)?),
29            4 => Method::Leave(PlayerList::azalea_read(buf)?),
30            id => return Err(BufReadError::UnexpectedEnumVariant { id: i32::from(id) }),
31        })
32    }
33}
34
35impl AzaleaWrite for Method {
36    fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
37        match self {
38            Method::Add((parameters, playerlist)) => {
39                0u8.azalea_write(buf)?;
40                parameters.azalea_write(buf)?;
41                playerlist.azalea_write(buf)?;
42            }
43            Method::Remove => {
44                1u8.azalea_write(buf)?;
45            }
46            Method::Change(parameters) => {
47                2u8.azalea_write(buf)?;
48                parameters.azalea_write(buf)?;
49            }
50            Method::Join(playerlist) => {
51                3u8.azalea_write(buf)?;
52                playerlist.azalea_write(buf)?;
53            }
54            Method::Leave(playerlist) => {
55                4u8.azalea_write(buf)?;
56                playerlist.azalea_write(buf)?;
57            }
58        }
59        Ok(())
60    }
61}
62
63#[derive(AzBuf, Clone, Debug)]
64pub struct Parameters {
65    pub display_name: FormattedText,
66    pub options: u8,
67    pub nametag_visibility: String,
68    pub collision_rule: String,
69    pub color: ChatFormatting,
70    pub player_prefix: FormattedText,
71    pub player_suffix: FormattedText,
72}
73
74type PlayerList = Vec<String>;