ghast-client 0.5.4

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: the bot's own fishing bobber ([`crate::fishing`]): the object spawn of type 90 whose
//! data is our entity id (`ClientPlayNetworkHandler.onEntitySpawn` makes it the owner's
//! `fishHook`), the synced hooked-entity id, the bite heuristic (the `entity.bobber.splash`
//! sound at the bobber: 1.12.2 servers send no bite flag) and the bobber's removal.

use super::*;
use ghast_entity::EntityType;

/// `FishingBobberEntity.HOOK_ENTITY_ID` (1.12.2: `Entity` defines 0 – 5).
const HOOKED: u8 = 6;
/// `entity.bobber.splash` in the 1.12.2 sound registry.
const SPLASH: i32 = 153;
/// The shortest time the fish stays on the hook after the splash (`hookCountdown`: 20 – 40).
const WINDOW: u8 = 20;
/// The bobber's object type.
const BOBBER: EntityType = EntityType::LegacyObject(90);

impl Sim12 {
    /// A spawn, (our bobber's) entity data or a sound packet, after the tracker applied it.
    #[cold]
    #[inline(never)]
    pub(super) fn fishing_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 Some(t) = &self.entities else {
            return Ok(());
        };
        if raw.id == cb::id::SOUND_EFFECT {
            let p: cb::SoundEffect = raw.decode_as()?;
            if p.sound_id == SPLASH {
                let pos = [
                    f64::from(p.x) / 8.0,
                    f64::from(p.y) / 8.0,
                    f64::from(p.z) / 8.0,
                ];
                let me = self.fishing.id();
                let others = t
                    .iter()
                    .filter(|e| e.entity_type() == BOBBER && Some(e.id()) != me)
                    .map(ghast_entity::Entity::position);
                self.fishing.splash(pos, others, WINDOW);
            }
            return Ok(());
        }
        let mut r = ghast_protocol::Reader::new(&raw.body);
        let id = r.read_varint()?;
        if raw.id == cb::id::SPAWN_ENTITY {
            let changed = match t.get(id) {
                Some(e) if e.entity_type() == BOBBER => {
                    self.fishing
                        .spawned(id, e.data(), self.glide.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.glide.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 {
            if let (HOOKED, V::VarInt(h)) = (m.key, &m.value) {
                self.fishing.hooked(*h);
            }
        }
        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| {
            ghast_physics::v1_12::projectile::bobber_liquid_height(world, p) > 0.0
        });
        if gone {
            self.glide.filter_dirty = true;
        }
    }
}