Skip to main content

yog_event/entity/
data.rs

1//! What mods keep on entities, and their sizes: services the runtime
2//! provides (it keeps the data and syncs it to the clients that see them).
3
4use yog_data::Schema;
5use yog_data::with::Raw;
6
7use crate::Arg;
8
9/// Bytes a mod keeps on an entity under a key.
10#[derive(Debug, Clone, PartialEq, Schema)]
11pub struct DataValue {
12    #[data(with = "Raw")]
13    pub bytes: Vec<u8>,
14}
15
16/// What the entity keeps under `key`.
17#[derive(Debug, Clone, Schema)]
18pub struct Data {
19    pub uuid: String,
20    pub key: String,
21}
22
23/// Keeps `value` under `key` (`None` forgets it), synced to the clients
24/// that see the entity; `persist` saves it with the entity.
25#[derive(Debug, Clone, Schema)]
26pub struct SetData {
27    pub uuid: String,
28    pub key: String,
29    pub value: Option<DataValue>,
30    pub persist: bool,
31}
32
33/// What entity `uuid` keeps under `key` (none: nothing, or no runtime).
34pub fn data(uuid: impl Arg<String>, key: impl Arg<String>) -> Option<Vec<u8>> {
35    crate::call(&Data {
36        uuid: uuid.arg(),
37        key: key.arg(),
38    })
39    .flatten()
40    .map(|v| v.bytes)
41}
42
43/// Keeps `value` on entity `uuid` under `key` (`None` forgets it), synced
44/// to the clients that see it; `persist` saves it with the entity.
45pub fn set_data(
46    uuid: impl Arg<String>,
47    key: impl Arg<String>,
48    value: Option<&[u8]>,
49    persist: bool,
50) -> bool {
51    let value = value.map(|bytes| DataValue {
52        bytes: bytes.to_vec(),
53    });
54    let set = SetData {
55        uuid: uuid.arg(),
56        key: key.arg(),
57        value,
58        persist,
59    };
60    crate::call(&set).unwrap_or_default()
61}
62
63/// Entity `uuid`'s size, `[width, height, eye height]` (`None`: vanilla's),
64/// on the server and on every client that sees it.
65pub fn set_dimensions(uuid: impl Arg<String>, size: Option<[f32; 3]>) -> bool {
66    crate::call(&SetDimensions {
67        uuid: uuid.arg(),
68        size,
69    })
70    .unwrap_or_default()
71}
72
73/// What the host reads as "vanilla's size again": a negative width.
74pub const VANILLA_SIZE: [f32; 3] = [-1.0, 0.0, 0.0];
75
76/// The entity's size, `[width, height, eye height]` — `None`: vanilla's —
77/// on the server and on every client that sees it.
78#[derive(Debug, Clone, Schema)]
79pub struct SetDimensions {
80    pub uuid: String,
81    pub size: Option<[f32; 3]>,
82}
83
84/// How a hanging entity hangs: on which side, how big, and the item it was
85/// placed from (what it drops) — its entity data `yog:hanging`
86/// (`yog_ids::yog::HANGING`), which the host keeps up to date.
87#[derive(Debug, Clone, PartialEq, Eq, Schema)]
88pub struct Hanging {
89    /// The way it faces, out of its wall: `north`, `south`, `west`, `east`.
90    pub facing: String,
91    /// Whole blocks of wall, wide and high.
92    pub width: u32,
93    pub height: u32,
94    /// The item it keeps: its id and custom data (SNBT, `{}` for none).
95    pub item: String,
96    pub nbt: String,
97}