Skip to main content

yog_core/
lib.rs

1//! Core Yog types and handles shared across all API modules.
2//!
3//! Kept tiny — its one dependency is Yog Data, so its records travel the
4//! bus: every other `yog-*` crate builds on this, and the facade
5//! [`yog-api`] re-exports it.
6
7mod local;
8mod query;
9mod state;
10
11pub use local::{BlockArea, BlockKind, ClickMode, LocalState, NearbyEntity};
12pub use query::{
13    BlockHit, BlockInfo, EntityInfo, Face, PlacedBlock, PlayerInfo, Rotation, ScreenSlot,
14    SlotStack, Stack, entity_flags,
15};
16pub use state::{join_state, split_state};
17
18/// A block position in the world.
19#[derive(Debug, Clone, Copy, PartialEq, Eq, yog_data::Schema)]
20#[data(view)]
21pub struct BlockPos {
22    pub x: i32,
23    pub y: i32,
24    pub z: i32,
25}
26
27/// A point in a world (blocks, fractional).
28#[derive(Debug, Clone, Copy, PartialEq, Default, yog_data::Schema)]
29#[data(view)]
30pub struct Point {
31    pub x: f64,
32    pub y: f64,
33    pub z: f64,
34}
35
36impl BlockPos {
37    pub const fn new(x: i32, y: i32, z: i32) -> Self {
38        Self { x, y, z }
39    }
40}
41
42impl From<(i32, i32, i32)> for BlockPos {
43    fn from((x, y, z): (i32, i32, i32)) -> Self {
44        Self { x, y, z }
45    }
46}
47
48/// `[x, y, z]`, as math on positions writes them.
49impl From<[i32; 3]> for BlockPos {
50    fn from([x, y, z]: [i32; 3]) -> Self {
51        Self { x, y, z }
52    }
53}
54
55impl From<BlockPos> for [i32; 3] {
56    fn from(p: BlockPos) -> Self {
57        [p.x, p.y, p.z]
58    }
59}
60
61impl From<[f64; 3]> for Point {
62    fn from([x, y, z]: [f64; 3]) -> Self {
63        Self { x, y, z }
64    }
65}
66
67impl From<Point> for [f64; 3] {
68    fn from(p: Point) -> Self {
69        [p.x, p.y, p.z]
70    }
71}
72
73impl From<(f64, f64, f64)> for Point {
74    fn from((x, y, z): (f64, f64, f64)) -> Self {
75        Self { x, y, z }
76    }
77}
78
79impl From<BlockPos> for Point {
80    /// The block's corner (its lowest x, y, z).
81    fn from(p: BlockPos) -> Self {
82        Self {
83            x: f64::from(p.x),
84            y: f64::from(p.y),
85            z: f64::from(p.z),
86        }
87    }
88}