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
//! Modern versions: the bot's own fishing bobber ([`crate::fishing`]): the `fishing_bobber`
//! spawn whose data is our entity id (`FishingHook.recreateFromPacket` takes the owner from it
//! and `setOwner` makes it the player's `fishing`), the synced `DATA_HOOKED_ENTITY` and
//! `DATA_BITING` (the bite), and the bobber's removal.

use super::*;

/// `FishingHook` entity data (26.1 – 26.3: `Entity` defines 0 – 7).
mod data_index {
    /// `DATA_HOOKED_ENTITY` (entity id + 1).
    pub(super) const HOOKED: u8 = 8;
    /// `DATA_BITING`.
    pub(super) const BITING: u8 = 9;
}

impl ModernSim {
    /// The packet ids our bobber needs beyond the tracker's: its entity data (a lean tracker
    /// does not read it).
    pub(super) fn fishing_packet_ids(&self) -> &'static [i32] {
        use super::proto::play::clientbound as cb;
        if self.fishing.active() {
            &[cb::id::ENTITY_METADATA]
        } else {
            &[]
        }
    }

    /// A spawn or (our bobber's) entity data packet, after the tracker applied it.
    #[cold]
    #[inline(never)]
    pub(super) fn fishing_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
        use super::proto::play::clientbound as cb;
        use super::proto::types::EntityMetadataEntryValue as V;
        let Some(t) = &self.entities else {
            return Ok(());
        };
        let mut r = ghast_protocol::Reader::new(&raw.body);
        let id = r.read_varint()?;
        if raw.id == cb::id::SPAWN_ENTITY {
            r.skip(16)?; // uuid
            let ty = r.read_varint()?;
            let bobber = u16::try_from(ty)
                .ok()
                .and_then(super::data::entity_type)
                .is_some_and(|t| t.name == "fishing_bobber");
            let changed = match t.get(id) {
                Some(e) if bobber => self
                    .fishing
                    .spawned(id, e.data(), self.own.id, e.position()),
                // another entity under our bobber's id: ours is gone
                _ => self.fishing.id() == Some(id) && self.fishing.gone(),
            };
            if changed {
                self.filter_dirty = true;
            }
            return Ok(());
        }
        if self.fishing.id() != Some(id) {
            return Ok(());
        }
        let p: cb::EntityMetadata = raw.decode_as()?;
        for m in &p.metadata {
            match (m.key, &m.value) {
                (data_index::HOOKED, V::Int(h)) => self.fishing.hooked(*h),
                (data_index::BITING, V::Boolean(b)) => self.fishing.biting(*b),
                _ => {}
            }
        }
        Ok(())
    }

    /// One client tick with our bobber: its tracked position and state.
    #[cold]
    #[inline(never)]
    pub(super) fn tick_fishing(&mut self) {
        let pos = self
            .fishing
            .id()
            .and_then(|id| self.entities.as_ref()?.get(id))
            .map(ghast_entity::Entity::position);
        let world = &self.world;
        let gone = self.fishing.tick(pos, |p| {
            modern::projectile::bobber_liquid_height::<Rules, _>(world, p) > 0.0
        });
        if gone {
            self.filter_dirty = true;
        }
    }
}