ghast-client 0.4.11

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 and 26.2)
Documentation
//! The cross-layer version row: what a protocol version can do, as facts the version-agnostic
//! core asks for instead of comparing version numbers.
//!
//! One constant row per [`GameVersion`]. Adding a version is adding a row (plus whatever is
//! really new); the core never names a version. Every field is a plain fact of the vanilla
//! client of that version.

use super::GameVersion;

/// The physics rule set a version runs on (`ghast-physics` module).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PhysicsRules {
    /// `ghast_physics::v1_8` (1.8 – 1.8.9).
    V1_8,
    /// `ghast_physics::v1_12` (1.12.1, 1.12.2).
    V1_12,
    /// `ghast_physics::v1_16_5` (1.16.2 – 1.16.5).
    V1_16_5,
    /// `ghast_physics::modern` with the `V26_1` rules (26.1 – 26.1.2).
    Modern26_1,
    /// `ghast_physics::modern` with the `V26_2` rules (26.2: collision restitution).
    Modern26_2,
}

/// When a change of the stack in the used hand ends an item use.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum UseStop {
    /// At once when the bot selects another slot (or the server does).
    AtSelect,
    /// In the player tick after the key handling, when the hand's stack object was replaced:
    /// another hotbar slot, or any server write of that slot (`set_slot`, `window_items`), even
    /// of an equal stack (1.8 `PlayerEntity.tick`, 1.12 `updateActiveHand`: identity).
    HeldReplaced,
    /// In the player tick after the key handling, when the hand holds another item (1.16.5
    /// `tickActiveItemStack`: `ItemStack.areItemsEqual`); a slot change to the same item or a
    /// resync keeps the use.
    HeldItemChanged,
}

/// When a change of the held item resets the 1.9+ attack cooldown.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CooldownReset {
    /// At once when the bot selects another slot.
    AtSelect,
    /// In the player tick after the key handling (`PlayerEntity.tick`, after the cooldown
    /// count), when the held stack is another item, or the same item without durability with
    /// another damage value (1.12 `ItemStack.equals`); an attack in the tick of a slot change
    /// still has the old strength.
    ItemAndDamage,
    /// As [`CooldownReset::ItemAndDamage`] comparing the item only.
    Item,
}

/// What an invulnerable player (creative, spectator) may eat: `PlayerEntity.canConsume`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum InvulnerableEating {
    /// Nothing (1.8, 1.12: `!abilities.invulnerable` is part of the test).
    Never,
    /// Anything, full or not (1.14+: `abilities.invulnerable || …`).
    Always,
    /// As any player (the hunger rule only; the versions not checked yet).
    Hunger,
}

/// The data tables of a version (`ghast-data` module).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DataSet {
    /// `ghast_data::v1_8`: numeric block / item ids with metadata.
    V1_8,
    /// `ghast_data::v1_12`: numeric block / item ids with metadata.
    V1_12,
    /// `ghast_data::v1_16_5`: flattened block states and item ids.
    V1_16_5,
    /// `ghast_data::v26_1`: flattened block states and item ids.
    V26_1,
    /// `ghast_data::v26_2`: flattened block states and item ids.
    V26_2,
}

/// How item stacks carry their extra data on the wire.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ItemModel {
    /// Numeric id, damage value and an optional NBT compound (1.8 – 1.12).
    LegacyNbt,
    /// Flattened id and an optional NBT compound (1.13 – 1.20.4).
    FlatNbt,
    /// Flattened id and data components (1.20.5+).
    Components,
}

/// What a protocol version can do: the facts the client core asks for.
///
/// Read one with [`GameVersion::caps`].
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct Capabilities {
    /// The version this row describes.
    pub version: GameVersion,
    /// Protocol number.
    pub protocol: i32,
    /// Physics rule set.
    pub physics: PhysicsRules,
    /// Data tables.
    pub data: DataSet,
    /// How item stacks carry extra data.
    pub item_model: ItemModel,
    /// 1.9 combat: attack cooldown, swing sent after the click packet, the off hand.
    pub combat_1_9: bool,
    /// The crosshair pick skips entities with the player's root vehicle (1.9+), not only the
    /// vehicle the player sits on (1.8).
    pub entity_pick_root_vehicle: bool,
    /// Eye height above the feet while standing.
    pub eye_height: f64,
    /// Eye height while sneaking (1.54 in 1.8 – 1.12, the crouching pose's 1.27 since 1.14).
    pub sneak_eye_height: f64,
    /// Longest chat line the server accepts, in UTF-16 units.
    pub chat_limit: usize,
    /// Game event 11 ("immediate respawn" game rule) exists (1.15+).
    pub immediate_respawn_rule: bool,
    /// The client uses a seed item at once when planting on its soil (the 1.8 `SeedItem.use`
    /// prediction; later versions wait for the server's slot update).
    pub seed_planting_prediction: bool,
    /// Server teleports carry an id the client confirms (1.9+).
    pub teleport_confirm: bool,
    /// Window clicks are confirmed with transactions (1.8 – 1.16).
    pub window_confirm: bool,
    /// A configuration state sits between login and play (1.20.2+).
    pub configuration_state: bool,
    /// Sneaking is sent as `player_input` key state, not as an entity action (1.21.2+).
    pub sneak_via_player_input: bool,
    /// The client sends `tick_end` after every tick (1.21.2+).
    pub sends_tick_end: bool,
    /// The player does not tick until the client sends `player_loaded` (1.21.4+).
    pub player_loaded_gate: bool,
    /// Chunk batches are acknowledged with the desired chunks per tick (1.20.2+).
    pub chunk_batch_ack: bool,
    /// Dig / place / use packets carry a sequence number the server acknowledges (1.19+).
    pub block_sequence: bool,
    /// Chat packets carry a timestamp, salt and message acknowledgements (1.19.3+).
    pub chat_acknowledgements: bool,
    /// The drop key takes the item off the held stack on the client at once and then swings
    /// the main arm when it took one (1.16.5 `ClientPlayerEntity.dropSelectedItem` and the
    /// key handler; 26.1 `LocalPlayer.drop`); 1.8.9 / 1.12.2 only send the packet and wait for
    /// the server's slot update.
    pub drop_prediction: bool,
    /// The client itself pushes the player away from other entities in its entity tick
    /// (26.1+; 1.12.2: `LivingEntity.tickCramming`, other players and boats push the local
    /// player on the client too; 1.16.5: `LivingEntity.tickCramming`,
    /// `OtherClientPlayerEntity.tickMovement` and `BoatEntity.tick`, in the client world's entity
    /// map order; 1.8.9 leaves it to the server). Judging those pushes needs
    /// each player entity's tab-list name (team collision rules) and game mode (spectators do
    /// not push), so entity tracking then also keeps a names-only tab list.
    pub client_entity_push: bool,
    /// A spectator's right click on a block (the block packet, then the click ends) swings the
    /// arm: 1.8 – 1.16 `interactBlock` returns true / SUCCESS for spectators; 26.1 / 26.2
    /// `MultiPlayerGameMode.performUseItemOn` returns CONSUME, which swings nothing.
    pub spectator_block_click_swings: bool,
    /// Switching to spectator ends an item use at once: 26.1 / 26.2
    /// `ServerPlayer.setGameMode(SPECTATOR)` calls `stopUsingItem` and the client's use ends
    /// with the living flags; 1.8 – 1.16.5 stop nothing (the use runs until release or finish).
    pub spectator_switch_stops_use: bool,
    /// The client's attack reads the held item's knockback enchantment level
    /// (`EnchantmentHelper.getKnockback`, 1.8 – 1.16.5): with knockback the attacker slows down
    /// on the client too. 26.1 / 26.2 `LivingEntity.getKnockback` runs the data-driven
    /// enchantment effects only on a `ServerLevel`: the client's knockback is the attribute
    /// alone (a sprint attack at full strength still slows).
    pub attack_knockback_enchantment: bool,
    /// Putting armor on with a right click swings the arm: 1.16.5 `ArmorItem.use` answers
    /// `success(…, isClient)` (SUCCESS swings) and 26.x `Equippable.swapWithEquipmentSlot`
    /// SUCCESS (a client swing); 1.12.2 `ArmorItem.use` SUCCESS swings nothing, 1.8
    /// `interactItem` neither.
    pub use_equip_swings: bool,
    /// The player's own inventory screen passes key presses through to the game while it is
    /// open (1.16.5 `InventoryScreen` / `CreativeInventoryScreen` set `passEvents`: GLFW key
    /// events the screen does not use reach the key bindings, and `handleInputEvents` runs):
    /// opening it releases every key (`KeyBinding.unpressAll`), a movement key pressed while
    /// it is open applies, closing it re-reads the held keys. 1.8.9 / 1.12.2 screens take
    /// every keyboard event (`handleInput` before `tickKeyboard`), 26.x not verified (off).
    pub own_inventory_passes_keys: bool,
    /// When a change of the used hand's stack ends an item use.
    pub use_stop: UseStop,
    /// Creative digging as 1.8 – 1.16.5 `attackBlock` / `updateBlockBreakingProgress` do it: a
    /// start packet and the block broken at once (not by a sword), then every 5 ticks while
    /// the button is held; nothing is aborted. Off: the survival path (26.x, not verified).
    pub creative_instant_dig: bool,
    /// What an invulnerable (creative) player may eat (`canConsume`).
    pub invulnerable_eating: InvulnerableEating,
    /// Our living flags from the server drive the client's item use (1.9+
    /// `ClientPlayerEntity.onTrackedDataSet(LIVING_FLAGS)`): bit 1 set while not using starts a
    /// use of the stack in the hand of bit 2, bit 1 clear while using ends it (no packet).
    pub living_flags_drive_use: bool,
    /// When a held item change resets the 1.9+ attack cooldown.
    pub cooldown_reset: CooldownReset,
    /// The key handling's crosshair pick turns with the player's head yaw (1.8
    /// `LivingEntity.getRotationVector(1.0F)`: the head yaw the last tick's `tickNewAi` set, so
    /// the yaw before this tick's turn) and this tick's pitch; the movement packet carries the
    /// new yaw. 1.9+ picks with this tick's yaw.
    pub pick_head_yaw: bool,
    /// The pick block key as 1.8.9 / 1.12.2 / 1.16.5 `MinecraftClient.doPick` ends (1.8
    /// `PlayerInventory.addPickBlock` of an item and damage, 1.12 `method_13250` / 1.16.5
    /// `addPickBlock` of a stack) and, in creative mode, a `creative_inventory_action` for the
    /// selected slot (oracle `clicks_creative`); 1.12.2 / 1.16.5 also load saved toolbars.
    /// Off: not modelled (refused).
    pub legacy_pick_block: bool,
}

const fn f(v: f32) -> f64 {
    v as f64
}

/// 1.12.1 (protocol 338): the 1.12.2 row with its own number (the same jar code apart from
/// the keep-alive width and GUI details; oracle records identical).
pub(crate) const V1_12_1: Capabilities = Capabilities {
    version: GameVersion::V1_12_1,
    protocol: 338,
    ..V1_12_2
};

/// 1.16.2 (protocol 751): the 1.16.5 row with its own number (client movement, block and
/// network code identical in the jars; oracle records identical).
pub(crate) const V1_16_2: Capabilities = Capabilities {
    version: GameVersion::V1_16_2,
    protocol: 751,
    ..V1_16_5
};

/// 1.16.3 (protocol 753): the 1.16.5 row with its own number, like [`V1_16_2`].
pub(crate) const V1_16_3: Capabilities = Capabilities {
    version: GameVersion::V1_16_3,
    protocol: 753,
    ..V1_16_5
};

/// 1.8 – 1.8.9 (protocol 47).
pub(crate) const V1_8_9: Capabilities = Capabilities {
    version: GameVersion::V1_8_9,
    protocol: 47,
    physics: PhysicsRules::V1_8,
    data: DataSet::V1_8,
    item_model: ItemModel::LegacyNbt,
    combat_1_9: false,
    entity_pick_root_vehicle: false,
    eye_height: f(1.62),
    // `1.62F - 0.08F` in float.
    sneak_eye_height: f(1.62 - 0.08),
    chat_limit: 100,
    own_inventory_passes_keys: false,
    immediate_respawn_rule: false,
    // the 1.8.9 right-click rules (`Sim::place_rules` / `place_sent`) predict planting now
    seed_planting_prediction: false,
    teleport_confirm: false,
    window_confirm: true,
    configuration_state: false,
    sneak_via_player_input: false,
    sends_tick_end: false,
    player_loaded_gate: false,
    chunk_batch_ack: false,
    block_sequence: false,
    chat_acknowledgements: false,
    client_entity_push: false,
    drop_prediction: false,
    spectator_block_click_swings: true,
    spectator_switch_stops_use: false,
    attack_knockback_enchantment: true,
    use_equip_swings: false,
    use_stop: UseStop::HeldReplaced,
    creative_instant_dig: true,
    invulnerable_eating: InvulnerableEating::Never,
    living_flags_drive_use: false,
    cooldown_reset: CooldownReset::AtSelect,
    pick_head_yaw: true,
    legacy_pick_block: true,
};

/// 1.12.2 (protocol 340).
pub(crate) const V1_12_2: Capabilities = Capabilities {
    version: GameVersion::V1_12_2,
    protocol: 340,
    physics: PhysicsRules::V1_12,
    data: DataSet::V1_12,
    combat_1_9: true,
    entity_pick_root_vehicle: true,
    chat_limit: 256,
    seed_planting_prediction: false,
    teleport_confirm: true,
    client_entity_push: true,
    living_flags_drive_use: true,
    cooldown_reset: CooldownReset::ItemAndDamage,
    pick_head_yaw: false,
    ..V1_8_9
};

/// 1.16.4 – 1.16.5 (protocol 754).
pub(crate) const V1_16_5: Capabilities = Capabilities {
    version: GameVersion::V1_16_5,
    protocol: 754,
    own_inventory_passes_keys: true,
    physics: PhysicsRules::V1_16_5,
    data: DataSet::V1_16_5,
    item_model: ItemModel::FlatNbt,
    sneak_eye_height: f(1.27),
    immediate_respawn_rule: true,
    client_entity_push: true,
    drop_prediction: true,
    use_equip_swings: true,
    invulnerable_eating: InvulnerableEating::Always,
    use_stop: UseStop::HeldItemChanged,
    cooldown_reset: CooldownReset::Item,
    pick_head_yaw: false,
    ..V1_12_2
};

/// 26.1 – 26.1.2 (protocol 775).
pub(crate) const V26_1: Capabilities = Capabilities {
    version: GameVersion::V26_1,
    protocol: 775,
    own_inventory_passes_keys: false,
    physics: PhysicsRules::Modern26_1,
    data: DataSet::V26_1,
    item_model: ItemModel::Components,
    window_confirm: false,
    configuration_state: true,
    sneak_via_player_input: true,
    sends_tick_end: true,
    player_loaded_gate: true,
    chunk_batch_ack: true,
    block_sequence: true,
    chat_acknowledgements: true,
    client_entity_push: true,
    spectator_block_click_swings: false,
    spectator_switch_stops_use: true,
    attack_knockback_enchantment: false,
    creative_instant_dig: false,
    invulnerable_eating: InvulnerableEating::Hunger,
    living_flags_drive_use: false,
    cooldown_reset: CooldownReset::AtSelect,
    pick_head_yaw: false,
    use_stop: UseStop::AtSelect,
    // 26.x picks with `pick_item_from_block` / `_entity` (not modelled)
    legacy_pick_block: false,
    ..V1_16_5
};

/// 26.2 (protocol 776): the 26.1 row with its own physics rules and data.
pub(crate) const V26_2: Capabilities = Capabilities {
    version: GameVersion::V26_2,
    protocol: 776,
    physics: PhysicsRules::Modern26_2,
    data: DataSet::V26_2,
    ..V26_1
};