aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs
blob: 6bb41a8165c8206a2abd7719d2d10b1eeb0adca0 (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
use azalea_buf::{BufReadError, McBuf};
use azalea_buf::{McBufReadable, McBufWritable, Readable, Writable};
use azalea_core::ResourceLocation;
use packet_macros::ClientboundGamePacket;
use std::io::{Read, Write};
use uuid::Uuid;

#[derive(Clone, Debug, McBuf, ClientboundGamePacket)]
pub struct ClientboundUpdateAttributesPacket {
    #[var]
    pub entity_id: u32,
    pub attributes: Vec<AttributeSnapshot>,
}

#[derive(Clone, Debug, McBuf)]
pub struct AttributeSnapshot {
    pub attribute: ResourceLocation,
    pub base: f64,
    pub modifiers: Vec<Modifier>,
}

#[derive(Clone, Debug, McBuf)]
pub struct Modifier {
    pub uuid: Uuid,
    pub amount: f64,
    pub operation: u8,
}

#[derive(Clone, Debug, Copy)]
enum Operation {
    Addition = 0,
    MultiplyBase = 1,
    MultiplyTotal = 2,
}

impl McBufReadable for Operation {
    fn read_from(buf: &mut impl Read) -> Result<Self, BufReadError> {
        match buf.read_byte()? {
            0 => Ok(Operation::Addition),
            1 => Ok(Operation::MultiplyBase),
            2 => Ok(Operation::MultiplyTotal),
            id => Err(BufReadError::UnexpectedEnumVariant { id: id.into() }),
        }
    }
}

impl McBufWritable for Operation {
    fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
        buf.write_byte(*self as u8)?;
        Ok(())
    }
}