yog_api/client/player.rs
1//! The local player (client): where it is, how it fares, what it holds.
2//! `None` (or empty) on a dedicated server and without a player.
3//!
4//! ```ignore
5//! use yog_api::client::player;
6//! if let Some(me) = player::state() {
7//! yog_api::info!("at {} {} {}", me.x, me.y, me.z);
8//! }
9//! ```
10
11use serde::Deserialize;
12use serde_json::Value;
13use yog_ids::client_op as op;
14
15use super::op::ask;
16
17/// The local player: where it is, where it looks, how it moves and fares.
18#[derive(Deserialize, Debug, Clone, PartialEq)]
19pub struct State {
20 pub x: f64,
21 pub y: f64,
22 pub z: f64,
23 /// Degrees: yaw 0 looks south (+z), 90 west; pitch 90 looks down.
24 pub yaw: f32,
25 pub pitch: f32,
26 pub vx: f64,
27 pub vy: f64,
28 pub vz: f64,
29 pub on_ground: bool,
30 pub in_water: bool,
31 pub eye_height: f64,
32 pub health: f32,
33 pub food: i32,
34 pub dimension: String,
35 /// The selected hotbar slot, 0–8.
36 pub slot: i32,
37 /// How charged the next attack is: 1 does full damage.
38 pub attack_strength: f32,
39}
40
41/// Where the local player is and how it fares; `None` without one.
42pub fn state() -> Option<State> {
43 ask(op::STATE, Value::Null)
44}
45
46/// A stack in the player's inventory.
47#[derive(Deserialize, Debug, Clone, PartialEq)]
48pub struct Stack {
49 /// 0–8 the hotbar, 9–35 the rest, 36–39 armor (feet first), 40 the off hand.
50 pub slot: i32,
51 pub item: String,
52 pub count: i32,
53}
54
55/// The player's inventory, stacks only.
56pub fn inventory() -> Vec<Stack> {
57 ask(op::INVENTORY, Value::Null).unwrap_or_default()
58}
59