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