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 right clicks on blocks through `ghast_data::v1_16_5::place` (oracle-proven): the
//! rules before the packet ([`rules`]) and the blocks the client sets after it ([`sent`]).

use ghast_data::v1_16_5::BlockState;
use ghast_data::v1_16_5::place::{Click, Held, Mode, Outcome, Use, right_click};

use super::predict::{PlaceQuery, PlaceRules, PlaceSent};
use crate::common::GameMode;

fn mode(m: GameMode) -> Mode {
    match m {
        GameMode::Creative => Mode::Creative,
        GameMode::Adventure => Mode::Adventure,
        GameMode::Spectator => Mode::Spectator,
        _ => Mode::Survival,
    }
}

fn held(s: Option<&ghast_inventory::Stack>) -> Option<Held> {
    let s = s?;
    Some(Held {
        id: u16::try_from(s.id()).ok()?,
        count: s.count(),
        nbt: s.nbt().is_some(),
    })
}

/// `None`: outside the world border (`interactBlock` fails before the packet).
#[allow(clippy::too_many_arguments)]
fn outcome(
    world: &ghast_world::v1_16_5::World,
    player: &ghast_physics::math::Aabb,
    pos: ghast_protocol::BlockPos,
    face: crate::common::Face,
    cursor: [f32; 3],
    stack: Option<&ghast_inventory::Stack>,
    sneaking: bool,
    any_item: bool,
    game_mode: GameMode,
) -> Option<Outcome> {
    if !super::predict::border_contains(&ghast_physics::BlockView::world_border(world), pos) {
        return None;
    }
    let click = Click {
        pos: [pos.x, pos.y, pos.z],
        face: face.id() as u8,
        cursor,
        held: held(stack),
        any_item,
        sneaking,
        mode: mode(game_mode),
        player: ghast_data::Aabb {
            min_x: player.min_x,
            min_y: player.min_y,
            min_z: player.min_z,
            max_x: player.max_x,
            max_y: player.max_y,
            max_z: player.max_z,
        },
    };
    // an unloaded position reads as air, as an empty client chunk does
    Some(right_click(&click, &|[x, y, z]| {
        world.block_state(x, y, z).map_or(0, BlockState::id)
    }))
}

/// [`super::Sim::place_rules`] of 1.16.5 (the clicking hand).
pub(crate) fn rules(
    world: &ghast_world::v1_16_5::World,
    player: &ghast_physics::math::Aabb,
    q: &PlaceQuery<'_>,
) -> PlaceRules {
    let air_use_unknown = q.held.is_some_and(|s| {
        u16::try_from(s.id()).is_ok_and(ghast_data::v1_16_5::place::air_use_unknown)
    });
    let Some(o) = outcome(
        world,
        player,
        q.pos,
        q.face,
        q.cursor,
        q.held,
        q.sneaking,
        q.any_item,
        q.game_mode,
    ) else {
        // FAIL without a packet: the click ends
        return PlaceRules {
            air_use_unknown,
            block_packet: Some(false),
            used: Some(false),
            no_swing: false,
            ends: true,
            starts_use: false,
        };
    };
    // `BlockItem.useOnBlock`: a food block item (potatoes, carrots, sweet berries) whose
    // placement fails is eaten instead (`use`): CONSUME with the use started when the player can
    // eat it, else the failure
    let id = q.held.and_then(|s| u16::try_from(s.id()).ok());
    let eats = o.result == Some(Use::Fail)
        && id.is_some_and(|i| {
            ghast_data::v1_16_5::place::block_item(i) != 0
                && ghast_data::v1_16_5::item_use::is_food(i)
                && q.food >= 0
                && (q.food < 20 || ghast_data::v1_16_5::item_use::always_edible(i))
        });
    let (used, no_swing, ends) = match o.result {
        None => (None, false, false),
        _ if eats => (Some(true), true, false),
        Some(Use::Success) => (Some(true), false, false),
        Some(Use::Consume) => (Some(true), true, false),
        Some(Use::Fail) => (Some(false), false, true),
        Some(Use::Pass) => (Some(false), false, false),
    };
    PlaceRules {
        air_use_unknown,
        block_packet: Some(true),
        used,
        no_swing,
        ends,
        starts_use: eats,
    }
}

/// [`super::Sim::place_sent`] of 1.16.5 (the hand that placed): the blocks the client sets
/// and whether one item of the stack was used up there, when the rules know them (`None`:
/// they do not).
pub(crate) fn sent(
    world: &mut ghast_world::v1_16_5::World,
    player: &ghast_physics::math::Aabb,
    p: &PlaceSent<'_>,
) -> Option<bool> {
    let o = outcome(
        world,
        player,
        p.pos,
        p.face,
        p.cursor,
        p.held,
        p.sneaking,
        p.any_item,
        p.game_mode,
    )?;
    let placed = o.placed?;
    for ([x, y, z], id) in placed {
        if let Some(s) = BlockState::from_id(id) {
            world.set_block_state(x, y, z, s);
        }
    }
    let before = p.held.map_or(0, ghast_inventory::Stack::count);
    Some(o.count_after.is_some_and(|n| n < before))
}