ghast-client 0.5.6

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.8.9: the bot's own fishing bobber ([`crate::fishing`]): the object spawn of type 90 whose
//! data is our entity id (`ClientPlayNetworkHandler.onEntitySpawn` builds the bobber with its
//! thrower, which sets the thrower's `fishHook`), the bite heuristic (the `random.splash`
//! sound at the bobber: 1.8.9 servers send no bite flag, and no other vanilla code plays that
//! sound) and the bobber's removal. A 1.8.9 server never tells the client what the bobber
//! hooked.

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

/// The bite's splash sound.
const SPLASH: &str = "random.splash";
/// The shortest time the fish stays on the hook after the splash (`hookCountdown`: 10 – 30).
const WINDOW: u8 = 10;
/// The bobber's object type.
const BOBBER: EntityType = EntityType::LegacyObject(90);

impl Sim18 {
    /// A spawn or a named sound packet, after the tracker applied it.
    #[cold]
    #[inline(never)]
    pub(super) fn fishing_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
        use ghast_protocol::v1_8::play::clientbound as cb;
        let Some(t) = &self.entities else {
            return Ok(());
        };
        if raw.id == cb::id::NAMED_SOUND_EFFECT {
            let p: cb::NamedSoundEffect = raw.decode_as()?;
            if &*p.sound_name == 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()?;
        let Some(own) = self.own_id else {
            return Ok(());
        };
        let changed = match t.get(id) {
            Some(e) if e.entity_type() == BOBBER => {
                self.fishing.spawned(id, e.data(), own, 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;
        }
        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_8::projectile::water_at(world, p));
        if gone {
            self.filter_dirty = true;
        }
    }
}