ghast-world 0.5.3

Chunk storage for the ghast Minecraft Java Edition client library: compact, O(1) block lookup, explicit loaded/unloaded columns, decoding of 1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3 chunk and block-change packets
Documentation
//! Small helpers shared by the per-version worlds (each one is the same code in every version
//! that uses it).

#[cfg(any(
    feature = "v1_8",
    feature = "v1_12",
    feature = "v1_16_5",
    feature = "v26_1",
    feature = "v26_2",
    feature = "v26_3"
))]
use crate::Applied;

/// [`Applied::Changed`] when `b`, else [`Applied::Ignored`].
#[cfg(any(
    feature = "v1_8",
    feature = "v1_12",
    feature = "v1_16_5",
    feature = "v26_1",
    feature = "v26_2",
    feature = "v26_3"
))]
#[inline]
pub(crate) fn changed(b: bool) -> Applied {
    if b {
        Applied::Changed
    } else {
        Applied::Ignored
    }
}

/// Bytes of one light array per section (1.8 and 1.12 send light inside the chunk packet).
#[cfg(any(feature = "v1_8", feature = "v1_12"))]
pub(crate) const LIGHT_BYTES: usize = 2048;

/// Same block id, any meta (1.8 and 1.12): the condition under which vanilla keeps a block
/// entity across a block change.
#[cfg(any(feature = "v1_8", feature = "v1_12"))]
pub(crate) fn same_block_id(a: u16, b: u16) -> bool {
    a >> 4 == b >> 4
}

/// Copies one light array out of a chunk packet (`s` is exactly [`LIGHT_BYTES`] long).
#[cfg(any(feature = "v1_8", feature = "v1_12"))]
pub(crate) fn nibbles(s: &[u8]) -> Box<[u8; LIGHT_BYTES]> {
    let mut b = Box::new([0u8; LIGHT_BYTES]);
    b.copy_from_slice(s);
    b
}

/// A wire block-state id as a raw state, with ids outside `0..STATE_COUNT` as air (the
/// flattened versions, where the raw state is the global id).
#[cfg(any(
    feature = "v1_16_5",
    feature = "v26_1",
    feature = "v26_2",
    feature = "v26_3"
))]
#[inline]
pub(crate) fn raw_state_or_air<const STATE_COUNT: usize>(v: i64) -> u16 {
    u16::try_from(v)
        .ok()
        .filter(|&r| usize::from(r) < STATE_COUNT)
        .unwrap_or(0)
}

/// The border that the physics' collision reads (1.8, 1.12 and 1.16.5: the current size).
#[cfg(all(
    feature = "physics",
    any(feature = "v1_8", feature = "v1_12", feature = "v1_16_5")
))]
pub(crate) fn physics_border(b: &crate::Border) -> ghast_physics::WorldBorder {
    let (center_x, center_z) = b.center();
    ghast_physics::WorldBorder {
        center_x,
        center_z,
        size: b.size(),
    }
}