ghast-client 0.5.3

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

use ghast_data::v1_12::BlockState;
use ghast_data::v1_12::place::{Click, Held, Mode, Outcome, 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()?,
        damage: s.damage(),
        count: s.count(),
        nbt: s.nbt().is_some(),
    })
}

#[allow(clippy::too_many_arguments)]
fn outcome(
    world: &ghast_world::v1_12::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,
    yaw: f32,
) -> Outcome {
    // `onRightClick` first: a block outside the world border is not clicked (the item's use
    // in the air follows)
    if !super::predict::border_contains(&ghast_physics::BlockView::world_border(world), pos) {
        return Outcome {
            block_packet: Some(false),
            used: Some(false),
            placed: Some(Vec::new()),
            count_after: stack.map(ghast_inventory::Stack::count),
        };
    }
    let click = Click {
        pos: [pos.x, pos.y, pos.z],
        face: face.id() as u8,
        cursor,
        held: held(stack),
        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,
        },
        yaw,
    };
    // an unloaded position reads as air, as an empty client chunk does
    // the sneaking rule reads both hands: with the main hand empty, `any_item` is the off hand
    let off_hand_empty = !any_item;
    right_click(&click, off_hand_empty, &|[x, y, z]| {
        world.block_state(x, y, z).map_or(0, BlockState::raw)
    })
}

/// [`super::Sim::place_rules`] of 1.12.2 (the main hand).
pub(crate) fn rules(
    world: &ghast_world::v1_12::World,
    player: &ghast_physics::math::Aabb,
    q: &PlaceQuery<'_>,
) -> PlaceRules {
    let o = outcome(
        world,
        player,
        q.pos,
        q.face,
        q.cursor,
        q.held,
        q.sneaking,
        q.any_item,
        q.game_mode,
        q.yaw,
    );
    PlaceRules {
        block_packet: o.block_packet,
        used: o.used,
        air_use_unknown: q.held.is_some_and(|s| {
            u16::try_from(s.id()).is_ok_and(ghast_data::v1_12::place::air_use_unknown)
        }),
        no_swing: false,
        ends: false,
        starts_use: false,
    }
}

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