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