1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00
azalea/azalea-protocol/src/packets/game/clientbound_update_attributes_packet.rs
mat 5a9fca0ca9
Better errors (#14)
* make reading use thiserror

* finish implementing all the error things

* clippy warnings related to ok_or

* fix some errors in other places

* thiserror in more places

* don't use closures in a couple places

* errors in writing packet

* rip backtraces

* change some BufReadError::Custom to UnexpectedEnumVariant

* Errors say what packet is bad

* error on leftover data and fix

it wasn't reading the properties for gameprofile
2022-08-06 02:22:19 -05:00

52 lines
1.3 KiB
Rust

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(())
}
}