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
//! 1.12.2: elytra gliding and the firework rockets that boost it.

use super::*;

impl Sim12 {
    /// Recomputes [`Sim12::glide_on`].
    pub(super) fn update_glide(&mut self) {
        self.glide_on = !self.ext.is_inert() || !self.glide.rockets.is_empty();
    }

    /// The tick with the extension state (gliding, rockets): out of line, so the plain tick
    /// stays as it was.
    #[cold]
    #[inline(never)]
    pub(super) fn step_ext(&mut self, pose: &Pose, controls: Controls, using_item: bool) {
        let input = ghast_physics::TickInput {
            keys: keys(controls),
            yaw: pose.yaw,
            pitch: pose.pitch,
            using_item,
        };
        let solids: &[ghast_physics::math::Aabb] =
            match (self.near.as_deref_mut(), self.entities.as_ref()) {
                (Some(n), Some(t)) if !n.idle() => {
                    n.solids(t, &self.world, &self.player);
                    &n.solids
                }
                _ => &[],
            };
        ghast_physics::v1_12::tick_ext(
            &mut self.player,
            &mut self.ext,
            &input,
            &self.world,
            &ghast_physics::v1_12::Options::default(),
            solids,
            &mut self.packets,
        );
    }

    /// The attached rockets' ticks after ours (in the world's entity list after the entities
    /// spawned before them; the pushes of other entities come first here).
    #[cold]
    #[inline(never)]
    pub(super) fn tick_rockets(&mut self, pose: &mut Pose) {
        for r in &mut self.glide.rockets {
            ghast_physics::v1_12::rocket_boost(&mut self.player, &self.ext);
            // Entity.baseTick: below y = -64 the rocket is removed, after this tick.
            if r.1 < -64.0 {
                r.0 = i32::MIN;
            }
            r.1 = self.player.pos[1];
        }
        self.glide.rockets.retain(|r| r.0 != i32::MIN);
        self.update_glide();
        let p = &self.player;
        pose.motion = Vec3::new(p.vel[0], p.vel[1], p.vel[2]);
    }

    /// A rocket spawn, removal or entity data, or our own entity data: decoded only when it
    /// can concern us.
    #[cold]
    #[inline(never)]
    pub(super) fn glide_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
        use ghast_protocol::defs::p340::types::EntityMetadataValue as V;
        use ghast_protocol::v1_12_2::play::clientbound as cb;
        let g = &mut self.glide;
        match raw.id {
            cb::id::SPAWN_ENTITY => {
                let p: cb::SpawnEntity = raw.decode_as()?;
                g.pending.retain(|r| r.0 != p.entity_id);
                g.rockets.retain(|r| r.0 != p.entity_id);
                if p.r#type == FIREWORK_ROCKET_12 {
                    if g.pending.len() >= 32 {
                        g.pending.remove(0);
                    }
                    g.pending.push((p.entity_id, p.y));
                }
            }
            cb::id::ENTITY_DESTROY => {
                let p: cb::EntityDestroy = raw.decode_as()?;
                for id in &p.entity_ids {
                    g.pending.retain(|r| r.0 != *id);
                    g.rockets.retain(|r| r.0 != *id);
                }
            }
            cb::id::ENTITY_METADATA => {
                let mut r = ghast_protocol::Reader::new(&raw.body);
                let id = r.read_varint()?;
                let own = id == g.own_id;
                let pending = g.pending.iter().position(|r| r.0 == id);
                if !own && pending.is_none() {
                    return Ok(());
                }
                let p: cb::EntityMetadata = raw.decode_as()?;
                for e in &p.metadata {
                    match (e.key, &e.value) {
                        // The shared flags: sprinting, fall flying (sneaking is the input's:
                        // `ClientPlayerEntity.isSneaking`).
                        (0, V::Byte(b)) if own => {
                            self.server_flags = Some(*b as u8);
                            self.ext.apply_shared_flags(&mut self.player, *b as u8);
                            if let Some(r) = &mut self.rec {
                                r.note("own entity flags not representable");
                            }
                        }
                        // LivingEntity's hand flags: the server's view of our item use.
                        (6, V::Byte(b)) if own => self.living_flags = Some(*b as u8),
                        // PlayerEntity's absorption amount (its first entry: 11).
                        (11, V::Float(a)) if own => {
                            self.absorption = *a;
                            self.absorption_dirty = true;
                        }
                        // A rocket's shooter (an entity id, 0 for none).
                        (7, V::VarInt(v)) if !own => {
                            if let Some(k) = pending {
                                let (rid, y) = g.pending.remove(k);
                                if *v == g.own_id {
                                    g.rockets.push((rid, y));
                                    log::debug!("firework rocket {rid} attached to us");
                                }
                            }
                        }
                        _ => {}
                    }
                }
            }
            _ => {}
        }
        self.update_glide();
        Ok(())
    }
}