azalea_protocol/packets/game/
clientbound_set_objective_packet.rs

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
use std::io::{Cursor, Write};

use azalea_buf::{McBuf, McBufReadable, McBufWritable};
use azalea_chat::{numbers::NumberFormat, FormattedText};
use azalea_core::objectives::ObjectiveCriteria;
use azalea_protocol_macros::ClientboundGamePacket;

#[derive(Clone, Debug, McBuf, ClientboundGamePacket)]
pub struct ClientboundSetObjectivePacket {
    pub objective_name: String,
    pub method: Method,
}

#[derive(Clone, Copy, Debug, McBuf)]
pub enum MethodKind {
    Add,
    Remove,
    Change,
}

#[derive(Clone, Debug)]
pub enum Method {
    Add {
        display_name: FormattedText,
        render_type: ObjectiveCriteria,
        number_format: NumberFormat,
    },
    Remove,
    Change {
        display_name: FormattedText,
        render_type: ObjectiveCriteria,
        number_format: NumberFormat,
    },
}

impl McBufReadable for Method {
    fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
        let kind = MethodKind::read_from(buf)?;
        match kind {
            MethodKind::Add => Ok(Method::Add {
                display_name: FormattedText::read_from(buf)?,
                render_type: ObjectiveCriteria::read_from(buf)?,
                number_format: NumberFormat::read_from(buf)?,
            }),
            MethodKind::Remove => Ok(Method::Remove),
            MethodKind::Change => Ok(Method::Change {
                display_name: FormattedText::read_from(buf)?,
                render_type: ObjectiveCriteria::read_from(buf)?,
                number_format: NumberFormat::read_from(buf)?,
            }),
        }
    }
}

impl McBufWritable for Method {
    fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
        match self {
            Method::Add {
                display_name,
                render_type,
                number_format,
            } => {
                MethodKind::Add.write_into(buf)?;
                display_name.write_into(buf)?;
                render_type.write_into(buf)?;
                number_format.write_into(buf)?;
            }
            Method::Remove => MethodKind::Remove.write_into(buf)?,
            Method::Change {
                display_name,
                render_type,
                number_format,
            } => {
                MethodKind::Change.write_into(buf)?;
                display_name.write_into(buf)?;
                render_type.write_into(buf)?;
                number_format.write_into(buf)?;
            }
        }
        Ok(())
    }
}