ghast-client 0.5.5

Headless Minecraft Java Edition client for the ghast library: connection, login, fixed 50 ms tick loop and a version-agnostic bot API (1.8.9, 1.12.2, 1.16.5, 26.1, 26.2 and 26.3)
Documentation
//! 26.3 (protocol 777): the row of the modern world and movement (`modern/`, mounted below as
//! `imp`) — the version's crates, physics rules and tables. Everything else is shared with the
//! other modern versions.

pub(crate) use ghast_data::v26_3 as data;
pub(crate) use ghast_entity::v26_3 as entity;
pub(crate) use ghast_physics::modern::V26_3 as Rules;
pub(crate) use ghast_physics::modern::piston::v26_3 as pistons;
// The 26.1 collision and push rules hold for 26.3 by name: the new cushion overrides neither
// canBeCollidedWith nor isPushable, the poplar boats are boats by name.
pub(crate) use ghast_physics::modern::{collides_26_1 as collides, push_kind_26_1 as push_kind};
pub(crate) use ghast_physics::pick::v26_3 as pick;
pub(crate) use ghast_protocol::v26_3 as proto;
pub(crate) use ghast_world::v26_3 as world;

use crate::version::GameVersion;

/// The run-time tag of this version.
pub(crate) const GAME: GameVersion = GameVersion::V26_3;
/// The order of the team parameters in `set_player_team`.
pub(crate) const TEAM_LAYOUT: super::teams::TeamLayout = super::teams::TeamLayout::AffixesFirst;
/// `LivingEntity.setPosToBed`: only on a bed, at its sleep height + 0.125 (oracle
/// `sleep_no_bed`: without a bed the player is not moved).
pub(crate) const SLEEP_ON_BED_ONLY: bool = true;

/// Entity data indices (`SynchedEntityData` ids, read from the 26.3 classes, unchanged from 26.1).
pub(crate) mod meta {
    /// `Entity.DATA_SHARED_FLAGS_ID`.
    pub(crate) const SHARED_FLAGS: u8 = 0;
    /// `LivingEntity.DATA_LIVING_ENTITY_FLAGS` (bit value 4: the riptide spin).
    pub(crate) const LIVING_FLAGS: u8 = 8;
    /// `LivingEntity.SLEEPING_POS_ID` (an optional block position).
    pub(crate) const SLEEPING_POS: u8 = 14;
    /// `FireworkRocketEntity.DATA_ATTACHED_TO_TARGET`.
    pub(crate) const ATTACHED_TO_TARGET: u8 = 9;
    /// `Avatar.DATA_PLAYER_MAIN_HAND`.
    pub(crate) const PLAYER_MAIN_HAND: u8 = 15;
    /// `AgeableMob.DATA_BABY_ID`.
    pub(crate) const BABY: u8 = 16;
    /// `AbstractHorse.DATA_ID_FLAGS` (tamed = 2) / `TamableAnimal.DATA_FLAGS_ID` (tame = 4).
    pub(crate) const MOUNT_FLAGS: u8 = 18;
    /// `Pig.DATA_BOOST_TIME` / `Strider.DATA_BOOST_TIME` (after `AgeableMob`'s two).
    pub(crate) const BOOST_TIME: u8 = 18;
    /// `Entity.DATA_POSE`.
    pub(crate) const POSE: u8 = 6;
    /// `Camel.DASH` (after `AbstractHorse`'s flags).
    pub(crate) const CAMEL_DASH: u8 = 19;
    /// `Camel.LAST_POSE_CHANGE_TICK`.
    pub(crate) const CAMEL_LAST_POSE: u8 = 20;
    /// `HappyGhast.STAYS_STILL` (after its `IS_LEASH_HOLDER`).
    pub(crate) const STAYS_STILL: u8 = 19;
    /// `AbstractNautilus.DASH` (after `TamableAnimal`'s two).
    pub(crate) const NAUTILUS_DASH: u8 = 20;
}

/// The effects movement and digging read, as the engine's classic 1-based numbers (26.3
/// registries report: `minecraft:mob_effect` protocol id + 1).
pub(crate) mod effects {
    pub(crate) const JUMP_BOOST: i8 = 8;
    pub(crate) const BLINDNESS: i8 = 15;
    pub(crate) const LEVITATION: i8 = 25;
    pub(crate) const SLOW_FALLING: i8 = 28;
    pub(crate) const CONDUIT_POWER: i8 = 29;
    pub(crate) const DOLPHINS_GRACE: i8 = 30;
    pub(crate) const WEAVING: i8 = 37;
}

/// This version's modern item functions (`ghast_inventory::modern` with the version fixed).
pub(crate) mod inv {
    use ghast_inventory::Stack;
    use ghast_inventory::modern as m;
    pub(crate) use m::Equip;

    const V: ghast_inventory::Version = ghast_inventory::Version::V26_3;

    #[inline]
    pub(crate) fn item_name(id: i32) -> Option<&'static str> {
        m::item_name(V, id)
    }
    #[inline]
    pub(crate) fn item_use(s: &Stack) -> Option<m::ItemUse> {
        m::item_use(V, s)
    }
    #[inline]
    pub(crate) fn next_damage_will_break(s: &Stack) -> bool {
        m::next_damage_will_break(V, s)
    }
    #[inline]
    pub(crate) fn can_glide_using(s: &Stack, slot: Equip) -> bool {
        m::can_glide_using(V, s, slot)
    }
}

// One body, mounted once per modern version (see CONTRIBUTING.md).
#[allow(clippy::duplicate_mod)]
#[path = "modern/mod.rs"]
mod imp;

pub(crate) use imp::{ModernSim as Sim263, dig_progress, held_item_facts};

/// The exact destroy progress of this version.
pub(crate) fn exact_dig(m: crate::hands::ModernDig) -> crate::hands::ExactDig {
    crate::hands::ExactDig::V26_3(m)
}

/// `EnchantmentHelper.getTridentSpinAttackStrength` through the synced registry.
pub(crate) fn inv_trident_strength(
    inv: &mut ghast_inventory::Inventory,
    s: &ghast_inventory::Stack,
) -> Option<f32> {
    inv.trident_spin_attack_strength_modern(s)
}

#[cfg(test)]
mod tests {
    /// Every mount / firework entity type resolves by name in the 26.3 registry.
    #[test]
    fn entity_ids_by_name() {
        let (h, l, n, f) = super::imp::entity_ids_for_test();
        assert!(
            h.iter().chain(&l).chain(&n).all(|&i| i >= 0),
            "{h:?} {l:?} {n:?}"
        );
        let id = |s: &str| i32::from(super::data::entity_by_name(s).unwrap().id);
        assert_eq!(
            (h[0], l[0], n[0], f),
            (
                id("horse"),
                id("llama"),
                id("nautilus"),
                id("firework_rocket")
            )
        );
    }
}