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
//! What happened since the behaviour's previous tick: [`TickEvents`] (with its rarely filled
//! lists behind one pointer), the raw packet copies and teleport events it carries, and the
//! caps of the pending lists.

use super::*;

/// A clientbound play packet copied for the behaviour ([`Behavior::raw_packets`]).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct RawInbound {
    /// Packet id (version-specific).
    pub id: i32,
    /// The body after the id, undecoded (a cheap reference-counted copy).
    pub body: bytes::Bytes,
    /// When the frame was decoded from the socket (frames read together share it).
    pub at: Instant,
}

/// A server teleport / correction (`position`) with the poses around it.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct TeleportEvent {
    /// The packet's values as received: absolute or relative per
    /// [`crate::common::teleport_flags`] in `flags`.
    pub teleport: crate::common::Teleport,
    /// What the server had from us just before ([`TickContext::sent`] before the overwrite):
    /// compare with [`TeleportEvent::after`] to see what the server moved / turned us by.
    pub sent_before: Pose,
    /// The client pose just before (it may be ahead of `sent_before` within a tick).
    pub pose_before: Pose,
    /// The pose after applying it (relative fields resolved).
    pub after: Pose,
}

/// Most teleports kept between two behaviour ticks.
pub(super) const MAX_PENDING_TELEPORTS: usize = 64;
/// Most knockback events kept between two behaviour ticks.
pub(super) const MAX_PENDING_KNOCKBACK: usize = 64;

/// Most raw packets kept between two behaviour ticks.
pub(super) const MAX_PENDING_RAW: usize = 4096;
/// Most entity events kept between two behaviour ticks.
pub(super) const MAX_PENDING_ENTITY_EVENTS: usize = 4096;
/// Most dialog events kept between two behaviour ticks (the open dialog is always current).
pub(super) const MAX_PENDING_DIALOGS: usize = 64;
/// Most HUD events kept between two behaviour ticks.
pub(super) const MAX_PENDING_HUD: usize = 256;
/// Most tab-completion answers kept between two behaviour ticks.
pub(super) const MAX_PENDING_TABS: usize = 64;
/// Most sign editor events kept between two behaviour ticks (the open editor is always
/// current).
pub(super) const MAX_PENDING_SIGNS: usize = 16;

/// What happened since the behaviour's previous tick.
#[derive(Debug, Clone, Default, PartialEq)]
#[non_exhaustive]
pub struct TickEvents {
    /// Chat messages in arrival order (only with [`Interests::CHAT`]).
    pub chat: Vec<ChatMessage>,
    /// Chat messages dropped because too many piled up while the behaviour was not ticking.
    pub chat_dropped: usize,
    /// The server teleported / corrected the player.
    pub teleported: bool,
    /// Every teleport in arrival order, with the sent pose before it (at most 64 per tick).
    pub teleports: Vec<TeleportEvent>,
    /// A join game packet arrived (first join or a proxy server switch).
    pub joined: bool,
    /// The player respawned or changed dimension.
    pub respawned: bool,
    /// The player died (health reached 0).
    pub died: bool,
    /// Health, food or saturation changed.
    pub health_changed: bool,
    /// The game mode changed.
    pub game_mode_changed: bool,
    /// Inventory / window changes and click results (ghast-inventory).
    pub inventory: ghast_inventory::Events,
    /// Block changes in arrival order (only with [`Interests::BLOCK_UPDATES`]): position and
    /// the version's raw block state (1.8 `id << 4 | meta`, 1.16 the global palette id).
    /// The world already holds them when the behaviour ticks.
    pub block_updates: Vec<(ghast_protocol::BlockPos, u32)>,
    /// Block changes dropped because too many piled up between two behaviour ticks.
    pub block_updates_dropped: usize,
    /// The packets asked for with [`Behavior::raw_packets`], in arrival order (kept while the
    /// behaviour is on hold, like chat).
    pub raw: Vec<RawInbound>,
    /// Raw packets dropped because more than 4096 piled up between two behaviour ticks.
    pub raw_dropped: usize,
    /// The rarely filled lists (effects, hurts, deaths, entity events), behind one pointer: a
    /// tick moves and clears them only when something arrived.
    more: Box<MoreEvents>,
}

impl TickEvents {
    pub(super) fn clear(&mut self) {
        self.chat.clear();
        self.chat_dropped = 0;
        self.teleported = false;
        self.teleports.clear();
        self.joined = false;
        self.respawned = false;
        self.died = false;
        self.health_changed = false;
        self.game_mode_changed = false;
        if self.more.any {
            *self.more = MoreEvents::default();
        }
        self.inventory = ghast_inventory::Events::default();
        self.block_updates.clear();
        self.block_updates_dropped = 0;
        self.raw.clear();
        self.raw_dropped = 0;
    }

    /// The extra lists for writing (marks them non-empty).
    #[inline]
    pub(super) fn more(&mut self) -> &mut MoreEvents {
        self.more.any = true;
        &mut self.more
    }

    /// Our status effects: every `entity_effect` / `remove_entity_effect` for us in arrival
    /// order (a re-application with the same values is an `Applied` too), and `Cleared` when a
    /// new player entity drops them (join, respawn). See [`TickContext::effects`].
    #[must_use]
    pub fn effects(&self) -> &[crate::common::EffectEvent] {
        &self.more.effects
    }

    /// We were hurt (1.8 – 1.16.5 the hurt entity statuses for our entity, 26.x its
    /// `damage_event`), in arrival order.
    #[must_use]
    pub fn hurt(&self) -> &[crate::common::Hurt] {
        &self.more.hurt
    }

    /// The server's pushes on us, in arrival order: our own entity's velocity packets (an
    /// attack's knockback among them) and explosion pushes (at most 64 between two behaviour
    /// ticks). The movement already uses them ([`TickContext::velocity`]).
    #[must_use]
    pub fn knockback(&self) -> &[crate::common::Knockback] {
        &self.more.knockback
    }

    /// Player deaths the server announced (ours: it sends them only to the dead player), with
    /// the killer where the version sends one.
    #[must_use]
    pub fn deaths(&self) -> &[crate::common::Death] {
        &self.more.deaths
    }

    /// Entity changes in arrival order (only with [`Interests::ENTITY_EVENTS`] and entity
    /// tracking): spawns (a re-spawn with the same id is a `Removed` then a `Spawned`),
    /// removals, moves, velocity, statuses, [`ghast_entity::Event::Hurt`] (the bot's own
    /// entity included), metadata, riding, pickups. Kept while the behaviour is on hold, at
    /// most 4096.
    #[must_use]
    pub fn entities(&self) -> &[ghast_entity::Event] {
        &self.more.entities
    }

    /// Entity events dropped because more than 4096 piled up between two behaviour ticks.
    #[must_use]
    pub fn entities_dropped(&self) -> usize {
        self.more.entities_dropped
    }

    /// Our item use's steps in order (at most 64 between two behaviour ticks): started by the
    /// right button's key handling, finished by the server (entity status 9), released, or
    /// interrupted (see [`crate::common::UseEvent`]). A use started and ended in the key
    /// handling after the behaviour's tick shows up on the next tick, like
    /// [`TickContext::using_item`].
    #[must_use]
    pub fn item_use(&self) -> &[crate::common::UseEvent] {
        &self.more.item_use
    }

    /// Our item cooldowns' starts (the server's `set_cooldown` or our own item use) and ends,
    /// in order (1.9+; at most 64 between two behaviour ticks). See [`crate::cooldown`];
    /// [`TickContext::item_cooldowns`] holds them now.
    #[must_use]
    pub fn cooldowns(&self) -> &[crate::cooldown::CooldownEvent] {
        &self.more.cooldowns
    }

    /// HUD changes in arrival order (only with [`Interests::HUD`]): boss bars, titles, the
    /// action bar, the tab list's header and footer, and the countdowns running out (see
    /// [`crate::hud`]). Kept while the behaviour is on hold, at most 256.
    #[must_use]
    pub fn hud(&self) -> &[crate::hud::HudEvent] {
        &self.more.hud
    }

    /// HUD events dropped because more than 256 piled up between two behaviour ticks.
    #[must_use]
    pub fn hud_dropped(&self) -> usize {
        self.more.hud_dropped
    }

    /// The server's answers to tab-completion requests ([`TickContext::tab_complete`]), in
    /// arrival order (at most 64 between two behaviour ticks).
    #[must_use]
    pub fn tab_completions(&self) -> &[crate::hud::TabCompletion] {
        &self.more.tabs
    }

    /// The sign editor's events, in order ([`crate::sign`]; the open editor is
    /// [`crate::TickContext::sign_editor`]).
    #[must_use]
    pub fn signs(&self) -> &[crate::sign::SignEvent] {
        &self.more.signs
    }

    /// The player fell asleep or woke up, in order ([`crate::TickContext::sleeping`] is the
    /// state now).
    #[must_use]
    pub fn sleep(&self) -> &[crate::common::SleepEvent] {
        &self.more.sleep
    }

    /// Our fishing bobber's changes, in arrival order: cast, bite, bite over, hooked, gone
    /// (with entity tracking; see [`crate::fishing`]; [`crate::TickContext::fishing`] is the
    /// bobber now). At most 64 between two behaviour ticks.
    #[must_use]
    pub fn fishing(&self) -> &[crate::fishing::FishingEvent] {
        &self.more.fishing
    }
}

/// The rarely filled event lists of [`TickEvents`].
#[derive(Debug, Clone, Default, PartialEq)]
pub(super) struct MoreEvents {
    /// Something was written since the last clear.
    pub(super) any: bool,
    pub(super) effects: Vec<crate::common::EffectEvent>,
    pub(super) hurt: Vec<crate::common::Hurt>,
    pub(super) knockback: Vec<crate::common::Knockback>,
    pub(super) deaths: Vec<crate::common::Death>,
    pub(super) entities: Vec<ghast_entity::Event>,
    pub(super) entities_dropped: usize,
    pub(super) item_use: Vec<crate::common::UseEvent>,
    pub(super) cooldowns: Vec<crate::cooldown::CooldownEvent>,
    pub(super) hud: Vec<crate::hud::HudEvent>,
    pub(super) hud_dropped: usize,
    pub(super) tabs: Vec<crate::hud::TabCompletion>,
    pub(super) signs: Vec<crate::sign::SignEvent>,
    pub(super) sleep: Vec<crate::common::SleepEvent>,
    pub(super) fishing: Vec<crate::fishing::FishingEvent>,
}