azalea_protocol/packets/game/
s_use_item_on.rs

1use std::io::{Cursor, Write};
2
3use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError};
4use azalea_core::{
5    direction::Direction,
6    position::{BlockPos, Vec3},
7};
8use azalea_protocol_macros::ServerboundGamePacket;
9
10use crate::packets::game::s_interact::InteractionHand;
11
12#[derive(Clone, Debug, AzBuf, ServerboundGamePacket)]
13pub struct ServerboundUseItemOn {
14    pub hand: InteractionHand,
15    pub block_hit: BlockHit,
16    #[var]
17    pub sequence: u32,
18}
19
20#[derive(Clone, Debug)]
21pub struct BlockHit {
22    /// The block that we clicked.
23    pub block_pos: BlockPos,
24    /// The face of the block that was clicked.
25    pub direction: Direction,
26    /// The exact coordinates of the world where the block was clicked. In the
27    /// network, this is transmitted as the difference between the location and
28    /// block position.
29    pub location: Vec3,
30    /// Whether the player's head is inside a block.
31    pub inside: bool,
32    /// Whether the player's hitting the world border.
33    pub world_border: bool,
34}
35
36impl AzaleaWrite for BlockHit {
37    fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
38        self.block_pos.azalea_write(buf)?;
39        self.direction.azalea_write(buf)?;
40        f32::azalea_write(
41            &((self.location.x - f64::from(self.block_pos.x)) as f32),
42            buf,
43        )?;
44        f32::azalea_write(
45            &((self.location.y - f64::from(self.block_pos.y)) as f32),
46            buf,
47        )?;
48        f32::azalea_write(
49            &((self.location.z - f64::from(self.block_pos.z)) as f32),
50            buf,
51        )?;
52        self.inside.azalea_write(buf)?;
53        self.world_border.azalea_write(buf)?;
54        Ok(())
55    }
56}
57
58impl AzaleaRead for BlockHit {
59    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
60        let block_pos = BlockPos::azalea_read(buf)?;
61        let direction = Direction::azalea_read(buf)?;
62        let cursor_x = f32::azalea_read(buf)?;
63        let cursor_y = f32::azalea_read(buf)?;
64        let cursor_z = f32::azalea_read(buf)?;
65        let inside = bool::azalea_read(buf)?;
66        let world_border = bool::azalea_read(buf)?;
67        Ok(Self {
68            block_pos,
69            direction,
70            location: Vec3 {
71                x: f64::from(block_pos.x) + f64::from(cursor_x),
72                y: f64::from(block_pos.y) + f64::from(cursor_y),
73                z: f64::from(block_pos.z) + f64::from(cursor_z),
74            },
75            inside,
76            world_border,
77        })
78    }
79}