Skip to main content

steel_registry/items/
item.rs

1//! Item-related types.
2//!
3//! Dynamic item behavior has been moved to `steel-core::behavior`.
4//! This file contains data structures that are needed by other crates.
5
6use std::io::{self, Cursor};
7
8use steel_utils::BlockPos;
9use steel_utils::serial::ReadFrom;
10
11use crate::blocks::properties::Direction;
12
13use glam::DVec3;
14
15/// Result of a ray cast hitting a block.
16///
17/// This is kept in steel-registry because it's used by steel-protocol
18/// for packet deserialization.
19#[derive(Debug, Clone)]
20pub struct BlockHitResult {
21    /// The exact location where the ray hit the block.
22    pub location: DVec3,
23    /// The face of the block that was hit.
24    pub direction: Direction,
25    /// The position of the block that was hit.
26    pub block_pos: BlockPos,
27    /// Whether this is a miss (no block hit).
28    pub miss: bool,
29    /// Whether the hit location is inside the block.
30    pub inside: bool,
31    /// Whether the world border was hit.
32    pub world_border_hit: bool,
33}
34
35impl ReadFrom for BlockHitResult {
36    fn read(data: &mut Cursor<&[u8]>) -> io::Result<Self> {
37        let block_pos = BlockPos::read(data)?;
38        let direction = Direction::read(data)?;
39        // Click coordinates are relative to the block position (0.0 to 1.0 range)
40        let click_x = f32::read(data)?;
41        let click_y = f32::read(data)?;
42        let click_z = f32::read(data)?;
43        let inside = bool::read(data)?;
44        let world_border_hit = bool::read(data)?;
45
46        // Convert to absolute world coordinates by adding block position
47        // (matching Java's FriendlyByteBuf.readBlockHitResult)
48        let location = DVec3::new(
49            f64::from(block_pos.x()) + f64::from(click_x),
50            f64::from(block_pos.y()) + f64::from(click_y),
51            f64::from(block_pos.z()) + f64::from(click_z),
52        );
53
54        Ok(BlockHitResult {
55            location,
56            direction,
57            block_pos,
58            miss: false,
59            inside,
60            world_border_hit,
61        })
62    }
63}