ghast-client 0.5.4

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
//! Held item facts of the legacy versions (1.8 – 1.16.5): the blocks a right click activates,
//! usable items, food points and tool speeds by item id.

use super::*;

/// 1.8 blocks whose client `Block.onUse` answers a right click.
#[cfg(any(feature = "v1_8", feature = "v1_12_2"))]
pub(super) const ACTIVATED_1_8: &[u8] = &[
    23, 25, 26, 54, 58, 61, 62, 64, 69, 71, 77, 84, 92, 93, 94, 96, 107, 116, 117, 118, 120, 130,
    138, 143, 145, 146, 149, 150, 151, 154, 158, 167, 178, 183, 184, 185, 186, 187, 193, 194, 195,
    196, 197,
];

#[cfg(feature = "v1_12_2")]
/// 1.12 items used by holding right click besides food: potion, milk, bow, shield, splash and
/// lingering potions are thrown (not held).
const USABLE_1_12: &[u16] = &[373, 335, 261, 442];

/// 1.8 / 1.12 swords (iron, wood, stone, diamond, gold): in creative they break no block.
#[cfg(any(feature = "v1_8", feature = "v1_12_2"))]
const SWORDS_LEGACY: &[u16] = &[267, 268, 272, 276, 283];

/// 1.8 items used by holding right click besides food: potion, milk, bow, swords.
#[cfg(feature = "v1_8")]
const USABLE_1_8: &[u16] = &[373, 335, 261, 267, 268, 272, 276, 283];

/// The hunger points a food item restores (1.8 – 1.16.5 tables; `None` for other items and
/// versions).
pub(crate) fn food_points(data: DataSet, id: u16) -> Option<u8> {
    // Only read by the legacy tables (a 26.x-only build has none).
    let _ = id;
    match data {
        #[cfg(feature = "v1_8")]
        DataSet::V1_8 => ghast_data::v1_8::item_use::food_points(id),
        #[cfg(feature = "v1_12_2")]
        DataSet::V1_12 => ghast_data::v1_12::food(id).map(|f| f.food_points),
        #[cfg(feature = "v1_16_5")]
        DataSet::V1_16_5 => ghast_data::v1_16_5::food(id).map(|f| f.food_points),
        #[allow(unreachable_patterns)]
        _ => None,
    }
}

/// A held item's version facts (tool speeds, block item, usable) from its id.
pub(crate) fn held_item_facts(data: DataSet, id: u16, count: u8, efficiency: u8) -> HeldItem {
    match data {
        #[cfg(feature = "v1_8")]
        DataSet::V1_8 => HeldItem {
            modern_item: None,
            swing_on_use: false,
            equips: false,
            equip_swings: false,
            creative_keeps_blocks: SWORDS_LEGACY.contains(&id),
            use_succeeds: false,
            id,
            count,
            efficiency,
            places_block: id < 256,
            usable: ghast_data::v1_8::item_use::is_food(id) || USABLE_1_8.contains(&id),
            food: ghast_data::v1_8::item_use::is_food(id),
            always_edible: ghast_data::v1_8::item_use::always_edible(id),
            tool_speed: Some(ghast_data::v1_8::tool_multiplier),
        },
        #[cfg(feature = "v1_12_2")]
        DataSet::V1_12 => HeldItem {
            modern_item: None,
            swing_on_use: false,
            equips: false,
            equip_swings: false,
            creative_keeps_blocks: SWORDS_LEGACY.contains(&id),
            use_succeeds: matches!(id, 332 | 344 | 346 | 384 | 438 | 441),
            id,
            count,
            efficiency,
            places_block: id < 256,
            usable: ghast_data::v1_12::item_use::is_food(id) || USABLE_1_12.contains(&id),
            food: ghast_data::v1_12::item_use::is_food(id),
            always_edible: ghast_data::v1_12::item_use::always_edible(id),
            tool_speed: Some(ghast_data::v1_12::tool_multiplier),
        },
        #[cfg(feature = "v1_16_5")]
        DataSet::V1_16_5 => HeldItem {
            modern_item: None,
            // SUCCESS on the client with a swing (`TypedActionResult.success(stack,
            // world.isClient)`): thrown items, the fishing rod, the empty map.
            swing_on_use: ghast_data::v1_16_5::Item::from_id(id).is_some_and(|i| {
                matches!(
                    i.info().name,
                    "snowball"
                        | "egg"
                        | "ender_pearl"
                        | "experience_bottle"
                        | "splash_potion"
                        | "lingering_potion"
                        | "fishing_rod"
                        | "map"
                )
            }),
            equips: false,
            equip_swings: false,
            creative_keeps_blocks: ghast_data::v1_16_5::Item::from_id(id)
                .is_some_and(|i| i.info().name.ends_with("_sword") || i.info().name == "trident"),
            use_succeeds: false,
            id,
            count,
            efficiency,
            places_block: ghast_data::v1_16_5::Item::from_id(id)
                .is_some_and(|i| ghast_data::v1_16_5::Block::from_name(i.info().name).is_some()),
            usable: ghast_data::v1_16_5::item_use::is_food(id)
                || ghast_data::v1_16_5::Item::from_id(id).is_some_and(|i| {
                    matches!(
                        i.info().name,
                        "potion"
                            | "milk_bucket"
                            | "honey_bottle"
                            | "bow"
                            | "crossbow"
                            | "shield"
                            | "trident"
                    )
                }),
            food: ghast_data::v1_16_5::item_use::is_food(id),
            always_edible: ghast_data::v1_16_5::item_use::always_edible(id),
            tool_speed: Some(ghast_data::v1_16_5::tool_multiplier),
        },
        #[cfg(feature = "v26_1")]
        DataSet::V26_1 => v26_1::held_item_facts(id, count, efficiency),
        #[cfg(feature = "v26_2")]
        DataSet::V26_2 => v26_2::held_item_facts(id, count, efficiency),
        #[cfg(feature = "v26_3")]
        DataSet::V26_3 => v26_3::held_item_facts(id, count, efficiency),
        // A data set this build does not include: an item without known facts.
        #[allow(unreachable_patterns)]
        _ => HeldItem {
            modern_item: None,
            swing_on_use: false,
            equips: false,
            equip_swings: false,
            creative_keeps_blocks: false,
            use_succeeds: false,
            id,
            count,
            efficiency,
            places_block: false,
            usable: false,
            food: false,
            always_edible: false,
            tool_speed: None,
        },
    }
}