yog-event 0.9.4

Yog event system — pub/sub for game/server events.
Documentation
//! What mods keep on entities, and their sizes: services the runtime
//! provides (it keeps the data and syncs it to the clients that see them).

use yog_data::Schema;
use yog_data::with::Raw;

use crate::Arg;

/// Bytes a mod keeps on an entity under a key.
#[derive(Debug, Clone, PartialEq, Schema)]
pub struct DataValue {
    #[data(with = "Raw")]
    pub bytes: Vec<u8>,
}

/// What the entity keeps under `key`.
#[derive(Debug, Clone, Schema)]
pub struct Data {
    pub uuid: String,
    pub key: String,
}

/// Keeps `value` under `key` (`None` forgets it), synced to the clients
/// that see the entity; `persist` saves it with the entity.
#[derive(Debug, Clone, Schema)]
pub struct SetData {
    pub uuid: String,
    pub key: String,
    pub value: Option<DataValue>,
    pub persist: bool,
}

/// What entity `uuid` keeps under `key` (none: nothing, or no runtime).
pub fn data(uuid: impl Arg<String>, key: impl Arg<String>) -> Option<Vec<u8>> {
    crate::call(&Data {
        uuid: uuid.arg(),
        key: key.arg(),
    })
    .flatten()
    .map(|v| v.bytes)
}

/// Keeps `value` on entity `uuid` under `key` (`None` forgets it), synced
/// to the clients that see it; `persist` saves it with the entity.
pub fn set_data(
    uuid: impl Arg<String>,
    key: impl Arg<String>,
    value: Option<&[u8]>,
    persist: bool,
) -> bool {
    let value = value.map(|bytes| DataValue {
        bytes: bytes.to_vec(),
    });
    let set = SetData {
        uuid: uuid.arg(),
        key: key.arg(),
        value,
        persist,
    };
    crate::call(&set).unwrap_or_default()
}

/// Entity `uuid`'s size, `[width, height, eye height]` (`None`: vanilla's),
/// on the server and on every client that sees it.
pub fn set_dimensions(uuid: impl Arg<String>, size: Option<[f32; 3]>) -> bool {
    crate::call(&SetDimensions {
        uuid: uuid.arg(),
        size,
    })
    .unwrap_or_default()
}

/// What the host reads as "vanilla's size again": a negative width.
pub const VANILLA_SIZE: [f32; 3] = [-1.0, 0.0, 0.0];

/// The entity's size, `[width, height, eye height]` — `None`: vanilla's —
/// on the server and on every client that sees it.
#[derive(Debug, Clone, Schema)]
pub struct SetDimensions {
    pub uuid: String,
    pub size: Option<[f32; 3]>,
}

/// How a hanging entity hangs: on which side, how big, and the item it was
/// placed from (what it drops) — its entity data `yog:hanging`
/// (`yog_ids::yog::HANGING`), which the host keeps up to date.
#[derive(Debug, Clone, PartialEq, Eq, Schema)]
pub struct Hanging {
    /// The way it faces, out of its wall: `north`, `south`, `west`, `east`.
    pub facing: String,
    /// Whole blocks of wall, wide and high.
    pub width: u32,
    pub height: u32,
    /// The item it keeps: its id and custom data (SNBT, `{}` for none).
    pub item: String,
    pub nbt: String,
}