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.16.5: the movement recording of a ride (vehicle packets, hands, ticks) and the hands that
//! steer a pig or strider.

use super::*;

impl Sim16 {
    /// [`Sim::hand_items`] after the item ids in the hands changed (out of the plain tick).
    #[cold]
    #[inline(never)]
    pub(super) fn hands_changed(&mut self, hands: [Option<i32>; 2]) {
        self.mounts.hands = hands;
        if self.rec.is_some() {
            self.record_hands();
        }
        use ghast_physics::v1_16_5::MountKind;
        let (carrot, fungus) = (
            self.mounts.holds_stick(MountKind::Pig),
            self.mounts.holds_stick(MountKind::Strider),
        );
        if let Some(t) = self.entities.as_mut() {
            t.set_steering_items(carrot, fungus);
        }
        if let Some(m) = mount_mut(&mut self.ride) {
            m.mount.rider_holds_stick = self.mounts.holds_stick(m.mount.kind);
        }
    }
}

/// 1.16.5 clientbound packets a recording reads ([`Sim16::record_vehicle_packet`]).
pub(super) const RECORDED_VEHICLE_PACKETS: [i32; 13] =
    recorded_vehicle_packets!(ghast_protocol::v1_16_5);

impl Sim16 {
    /// Recording: a clientbound packet of a living mount whose spawn the recording has (its
    /// minecraft-data name and wire body; the setup has our entity id), and the
    /// server's `vehicle_move` while we ride; the oracle's `entity_packet` event handles the
    /// same bytes.
    #[cold]
    #[inline(never)]
    pub(super) fn record_vehicle_packet(&mut self, raw: &RawPacket) {
        use ghast_protocol::v1_16_5::play::clientbound as cb;
        let Some(r) = self.rec.as_mut() else {
            return;
        };
        let name = match raw.id {
            cb::id::SPAWN_ENTITY_LIVING => "spawn_entity_living",
            cb::id::ENTITY_METADATA => "entity_metadata",
            cb::id::ENTITY_UPDATE_ATTRIBUTES => "entity_update_attributes",
            cb::id::ENTITY_VELOCITY => "entity_velocity",
            cb::id::REL_ENTITY_MOVE => "rel_entity_move",
            cb::id::ENTITY_MOVE_LOOK => "entity_move_look",
            cb::id::ENTITY_LOOK => "entity_look",
            cb::id::ENTITY_TELEPORT => "entity_teleport",
            cb::id::ENTITY_HEAD_ROTATION => "entity_head_rotation",
            cb::id::ENTITY_EQUIPMENT => "entity_equipment",
            cb::id::ENTITY_DESTROY => "entity_destroy",
            cb::id::SET_PASSENGERS => "set_passengers",
            cb::id::VEHICLE_MOVE => "vehicle_move",
            _ => return,
        };
        if raw.id == cb::id::VEHICLE_MOVE {
            if matches!(self.ride.as_deref(), Some(Ride16::Mount(_))) {
                r.entity_packet(name, &raw.body);
            }
            return;
        }
        let Some(body) = crate::record::vehicle_packet_body(
            raw,
            cb::id::SET_PASSENGERS,
            cb::id::ENTITY_DESTROY,
            &mut r.vehicles,
            |id| {
                raw.id == cb::id::SPAWN_ENTITY_LIVING
                    && self
                        .entities
                        .as_ref()
                        .and_then(|t| t.get(id))
                        .and_then(mount_kind)
                        .is_some()
            },
        ) else {
            return;
        };
        r.entity_packet(name, &body);
        if raw.id == cb::id::SPAWN_ENTITY_LIVING {
            self.record_hands();
        }
    }

    /// Recording: the items in the hands (what steers a pig or strider).
    #[cold]
    #[inline(never)]
    fn record_hands(&mut self) {
        let Some(r) = self.rec.as_mut() else {
            return;
        };
        let name = |id: Option<i32>| {
            id.and_then(|i| u16::try_from(i).ok())
                .and_then(ghast_data::v1_16_5::Item::from_id)
                .map(|i| i.info().name)
        };
        r.hands_legacy(name(self.mounts.hands[0]), name(self.mounts.hands[1]));
    }

    /// Recording: one movement step (a ride the recording cannot replay is noted).
    #[cold]
    #[inline(never)]
    pub(super) fn record_tick(&mut self, input: &ghast_physics::TickInput) {
        let Some(r) = self.rec.as_mut() else {
            return;
        };
        match self.ride.as_deref() {
            Some(Ride16::Boat(_) | Ride16::Cart(_)) => {
                r.note_once("riding a boat or minecart not representable");
            }
            Some(Ride16::Mount(m)) if !r.vehicles.contains(&m.id) => {
                r.note_once("riding a mount whose spawn was not recorded: not representable");
            }
            _ => {}
        }
        r.tick(
            input,
            &crate::record::state_1_16(&self.player),
            &self.packets,
        );
    }

    /// The hands as they are now, for a pig or strider we ride.
    #[cold]
    #[inline(never)]
    pub(super) fn stick_hands(&mut self, inv: &ghast_inventory::Inventory) {
        let hands = [inv.held().map(|s| s.id()), inv.offhand().map(|s| s.id())];
        if self.mounts.hands != hands {
            self.hands_changed(hands);
        }
    }
}