Skip to main content

yog_core/
lib.rs

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