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    DigSpeed,
15    BlockHit, BlockInfo, EntityInfo, Face, PlacedBlock, PlayerInfo, Rotation, ScreenSlot,
16    SlotStack, Stack, entity_flags,
17};
18pub use state::{join_state, split_state};
19
20/// A block position in the world.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, yog_data::Schema)]
22#[data(view)]
23pub struct BlockPos {
24    pub x: i32,
25    pub y: i32,
26    pub z: i32,
27}
28
29/// A point in a world (blocks, fractional).
30#[derive(Debug, Clone, Copy, PartialEq, Default, yog_data::Schema)]
31#[data(view)]
32pub struct Point {
33    pub x: f64,
34    pub y: f64,
35    pub z: f64,
36}
37
38impl BlockPos {
39    pub const fn new(x: i32, y: i32, z: i32) -> Self {
40        Self { x, y, z }
41    }
42}
43
44impl From<(i32, i32, i32)> for BlockPos {
45    fn from((x, y, z): (i32, i32, i32)) -> Self {
46        Self { x, y, z }
47    }
48}
49
50/// `[x, y, z]`, as math on positions writes them.
51impl From<[i32; 3]> for BlockPos {
52    fn from([x, y, z]: [i32; 3]) -> Self {
53        Self { x, y, z }
54    }
55}
56
57impl From<BlockPos> for [i32; 3] {
58    fn from(p: BlockPos) -> Self {
59        [p.x, p.y, p.z]
60    }
61}
62
63impl From<[f64; 3]> for Point {
64    fn from([x, y, z]: [f64; 3]) -> Self {
65        Self { x, y, z }
66    }
67}
68
69impl From<Point> for [f64; 3] {
70    fn from(p: Point) -> Self {
71        [p.x, p.y, p.z]
72    }
73}
74
75impl From<(f64, f64, f64)> for Point {
76    fn from((x, y, z): (f64, f64, f64)) -> Self {
77        Self { x, y, z }
78    }
79}
80
81impl From<BlockPos> for Point {
82    /// The block's corner (its lowest x, y, z).
83    fn from(p: BlockPos) -> Self {
84        Self {
85            x: f64::from(p.x),
86            y: f64::from(p.y),
87            z: f64::from(p.z),
88        }
89    }
90}