azalea_protocol/packets/game/
s_interact.rs1use std::io::{Cursor, Write};
2
3use azalea_buf::{AzBuf, AzaleaRead, AzaleaReadVar, AzaleaWrite, AzaleaWriteVar};
4use azalea_core::position::Vec3;
5use azalea_protocol_macros::ServerboundGamePacket;
6use azalea_world::MinecraftEntityId;
7
8use crate::packets::BufReadError;
9
10#[derive(Clone, Debug, AzBuf, ServerboundGamePacket)]
11pub struct ServerboundInteract {
12 #[var]
13 pub entity_id: MinecraftEntityId,
14 pub action: ActionType,
15 pub using_secondary_action: bool,
17}
18
19#[derive(Clone, Copy, Debug)]
20pub enum ActionType {
21 Interact {
22 hand: InteractionHand,
23 },
24 Attack,
25 InteractAt {
26 location: Vec3,
27 hand: InteractionHand,
28 },
29}
30
31impl AzaleaWrite for ActionType {
32 fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
33 match self {
34 ActionType::Interact { hand } => {
35 0u32.azalea_write_var(buf)?;
36 hand.azalea_write(buf)?;
37 }
38 ActionType::Attack => {
39 1u32.azalea_write_var(buf)?;
40 }
41 ActionType::InteractAt { location, hand } => {
42 2u32.azalea_write_var(buf)?;
43 (location.x as f32).azalea_write(buf)?;
44 (location.y as f32).azalea_write(buf)?;
45 (location.z as f32).azalea_write(buf)?;
46 hand.azalea_write(buf)?;
47 }
48 }
49 Ok(())
50 }
51}
52
53impl AzaleaRead for ActionType {
54 fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
55 let action_type = u32::azalea_read_var(buf)?;
56 match action_type {
57 0 => {
58 let hand = InteractionHand::azalea_read(buf)?;
59 Ok(ActionType::Interact { hand })
60 }
61 1 => Ok(ActionType::Attack),
62 2 => {
63 let x = f32::azalea_read(buf)?;
64 let y = f32::azalea_read(buf)?;
65 let z = f32::azalea_read(buf)?;
66 let hand = InteractionHand::azalea_read(buf)?;
67 Ok(ActionType::InteractAt {
68 location: Vec3 {
69 x: f64::from(x),
70 y: f64::from(y),
71 z: f64::from(z),
72 },
73 hand,
74 })
75 }
76 _ => Err(BufReadError::UnexpectedEnumVariant {
77 id: action_type as i32,
78 }),
79 }
80 }
81}
82
83#[derive(AzBuf, Clone, Copy, Debug)]
84pub enum InteractionHand {
85 MainHand = 0,
86 OffHand = 1,
87}