azalea_protocol/packets/game/
c_damage_event.rs1use std::io::{Cursor, Write};
2
3use azalea_buf::{AzBuf, AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar};
4use azalea_core::position::Vec3;
5use azalea_protocol_macros::ClientboundGamePacket;
6use azalea_world::MinecraftEntityId;
7
8#[derive(Clone, Debug, AzBuf, ClientboundGamePacket)]
9pub struct ClientboundDamageEvent {
10 #[var]
11 pub entity_id: MinecraftEntityId,
12 #[var]
13 pub source_type_id: u32,
14 pub source_cause_id: OptionalEntityId,
15 pub source_direct_id: OptionalEntityId,
16 pub source_position: Option<Vec3>,
17}
18
19#[derive(Clone, Debug)]
20pub struct OptionalEntityId(pub Option<u32>);
21impl AzaleaRead for OptionalEntityId {
22 fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
23 match u32::azalea_read_var(buf)? {
24 0 => Ok(OptionalEntityId(None)),
25 id => Ok(OptionalEntityId(Some(id - 1))),
26 }
27 }
28}
29impl AzaleaWrite for OptionalEntityId {
30 fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
31 match self.0 {
32 Some(id) => (id + 1).azalea_write_var(buf),
33 None => 0u32.azalea_write_var(buf),
34 }
35 }
36}