Skip to content

Commit

Permalink
Improve Animation Packet
Browse files Browse the repository at this point in the history
  • Loading branch information
Oery committed Oct 25, 2024
1 parent 9663f0f commit 02c48ac
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/minecraft/packets/play/server/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,56 @@ use crate::minecraft::Packet;
use crate::serialization::{deserialize_varint, serialize_varint};
use kagami_macro::{packet, Deserialize, Packet, Serialize};

use byteorder::{ReadBytesExt, WriteBytesExt};

#[packet]
pub struct Animation {
#[encoding("varint")]
pub entity_id: i32,
pub animation: u8,
pub animation: AnimationKind,
}

#[derive(Debug, PartialEq)]
pub enum AnimationKind {
SwingArm,
TakeDamage,
LeaveBed,
Eat,
CriticalEffect,
MagicalCriticalEffect,
}

impl Deserialize for AnimationKind {
fn deserialize<R: std::io::Read>(reader: &mut R) -> std::io::Result<Self> {
use AnimationKind::*;

let action = reader.read_u8()?;
match action {
0 => Ok(SwingArm),
1 => Ok(TakeDamage),
2 => Ok(LeaveBed),
3 => Ok(Eat),
4 => Ok(CriticalEffect),
5 => Ok(MagicalCriticalEffect),
_ => Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("Invalid animation kind : {action}"),
)),
}
}
}

impl Serialize for AnimationKind {
fn serialize(&self, buf: &mut dyn std::io::Write) -> std::io::Result<()> {
use AnimationKind::*;

match self {
SwingArm => buf.write_u8(0),
TakeDamage => buf.write_u8(1),
LeaveBed => buf.write_u8(2),
Eat => buf.write_u8(3),
CriticalEffect => buf.write_u8(4),
MagicalCriticalEffect => buf.write_u8(5),
}
}
}

0 comments on commit 02c48ac

Please sign in to comment.