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: the movement recording of a ride (vehicle packets, hands).

use super::*;

/// 1.12.2 clientbound packets a recording reads ([`Sim12::record_vehicle_packet`]).
pub(super) const RECORDED_VEHICLE_PACKETS_12: [i32; 13] =
    recorded_vehicle_packets!(ghast_protocol::v1_12_2);

impl Sim12 {
    /// 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 a mount; 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_12_2::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(Ride12 {
                    seat: Seat12::Mount(_),
                    ..
                })
            ) {
                r.entity_packet(name, &raw.body);
            }
            return;
        }
        let spawns_mount = |_| {
            raw.decode_as::<cb::SpawnEntityLiving>()
                .ok()
                .and_then(|p| u8::try_from(p.r#type).ok())
                .and_then(|t| mount_kind_12(ghast_entity::EntityType::LegacyMob(t)))
                .is_some()
        };
        let spawn = raw.id == cb::id::SPAWN_ENTITY_LIVING;
        let Some(body) = crate::record::vehicle_packet_body(
            raw,
            cb::id::SET_PASSENGERS,
            cb::id::ENTITY_DESTROY,
            &mut r.vehicles,
            |id| spawn && spawns_mount(id),
        ) else {
            return;
        };
        r.entity_packet(name, &body);
    }

    /// Recording: the items in the hands when the inventory may have changed them (numeric
    /// item ids: what steers a pig).
    #[cold]
    #[inline(never)]
    pub(super) fn record_hands(&mut self, inv: &mut ghast_inventory::Inventory) {
        let Some(r) = self.rec.as_mut() else {
            return;
        };
        if !inv.take_player_touched() && r.hands.is_some() {
            return;
        }
        let hands = [inv.held().map(|s| s.id()), inv.offhand().map(|s| s.id())];
        if r.hands == Some(hands) {
            return;
        }
        r.hands = Some(hands);
        let [main, off] = hands.map(|h| h.map(|i| i.to_string()));
        r.hands_legacy(main.as_deref(), off.as_deref());
    }
}