Skip to main content

ghast_client/engine/
events.rs

1//! What happened since the behaviour's previous tick: [`TickEvents`] (with its rarely filled
2//! lists behind one pointer), the raw packet copies and teleport events it carries, and the
3//! caps of the pending lists.
4
5use super::*;
6
7/// A clientbound play packet copied for the behaviour ([`Behavior::raw_packets`]).
8#[derive(Debug, Clone, PartialEq, Eq)]
9#[non_exhaustive]
10pub struct RawInbound {
11    /// Packet id (version-specific).
12    pub id: i32,
13    /// The body after the id, undecoded (a cheap reference-counted copy).
14    pub body: bytes::Bytes,
15    /// When the frame was decoded from the socket (frames read together share it).
16    pub at: Instant,
17}
18
19/// A server teleport / correction (`position`) with the poses around it.
20#[derive(Debug, Clone, Copy, PartialEq)]
21#[non_exhaustive]
22pub struct TeleportEvent {
23    /// The packet's values as received: absolute or relative per
24    /// [`crate::common::teleport_flags`] in `flags`.
25    pub teleport: crate::common::Teleport,
26    /// What the server had from us just before ([`TickContext::sent`] before the overwrite):
27    /// compare with [`TeleportEvent::after`] to see what the server moved / turned us by.
28    pub sent_before: Pose,
29    /// The client pose just before (it may be ahead of `sent_before` within a tick).
30    pub pose_before: Pose,
31    /// The pose after applying it (relative fields resolved).
32    pub after: Pose,
33}
34
35/// Most teleports kept between two behaviour ticks.
36pub(super) const MAX_PENDING_TELEPORTS: usize = 64;
37/// Most knockback events kept between two behaviour ticks.
38pub(super) const MAX_PENDING_KNOCKBACK: usize = 64;
39
40/// Most raw packets kept between two behaviour ticks.
41pub(super) const MAX_PENDING_RAW: usize = 4096;
42/// Most entity events kept between two behaviour ticks.
43pub(super) const MAX_PENDING_ENTITY_EVENTS: usize = 4096;
44/// Most dialog events kept between two behaviour ticks (the open dialog is always current).
45pub(super) const MAX_PENDING_DIALOGS: usize = 64;
46/// Most HUD events kept between two behaviour ticks.
47pub(super) const MAX_PENDING_HUD: usize = 256;
48/// Most tab-completion answers kept between two behaviour ticks.
49pub(super) const MAX_PENDING_TABS: usize = 64;
50/// Most sign editor events kept between two behaviour ticks (the open editor is always
51/// current).
52pub(super) const MAX_PENDING_SIGNS: usize = 16;
53
54/// What happened since the behaviour's previous tick.
55#[derive(Debug, Clone, Default, PartialEq)]
56#[non_exhaustive]
57pub struct TickEvents {
58    /// Chat messages in arrival order (only with [`Interests::CHAT`]).
59    pub chat: Vec<ChatMessage>,
60    /// Chat messages dropped because too many piled up while the behaviour was not ticking.
61    pub chat_dropped: usize,
62    /// The server teleported / corrected the player.
63    pub teleported: bool,
64    /// Every teleport in arrival order, with the sent pose before it (at most 64 per tick).
65    pub teleports: Vec<TeleportEvent>,
66    /// A join game packet arrived (first join or a proxy server switch).
67    pub joined: bool,
68    /// The player respawned or changed dimension.
69    pub respawned: bool,
70    /// The player died (health reached 0).
71    pub died: bool,
72    /// Health, food or saturation changed.
73    pub health_changed: bool,
74    /// The game mode changed.
75    pub game_mode_changed: bool,
76    /// Inventory / window changes and click results (ghast-inventory).
77    pub inventory: ghast_inventory::Events,
78    /// Block changes in arrival order (only with [`Interests::BLOCK_UPDATES`]): position and
79    /// the version's raw block state (1.8 `id << 4 | meta`, 1.16 the global palette id).
80    /// The world already holds them when the behaviour ticks.
81    pub block_updates: Vec<(ghast_protocol::BlockPos, u32)>,
82    /// Block changes dropped because too many piled up between two behaviour ticks.
83    pub block_updates_dropped: usize,
84    /// The packets asked for with [`Behavior::raw_packets`], in arrival order (kept while the
85    /// behaviour is on hold, like chat).
86    pub raw: Vec<RawInbound>,
87    /// Raw packets dropped because more than 4096 piled up between two behaviour ticks.
88    pub raw_dropped: usize,
89    /// The rarely filled lists (effects, hurts, deaths, entity events), behind one pointer: a
90    /// tick moves and clears them only when something arrived.
91    more: Box<MoreEvents>,
92}
93
94impl TickEvents {
95    pub(super) fn clear(&mut self) {
96        self.chat.clear();
97        self.chat_dropped = 0;
98        self.teleported = false;
99        self.teleports.clear();
100        self.joined = false;
101        self.respawned = false;
102        self.died = false;
103        self.health_changed = false;
104        self.game_mode_changed = false;
105        if self.more.any {
106            *self.more = MoreEvents::default();
107        }
108        self.inventory = ghast_inventory::Events::default();
109        self.block_updates.clear();
110        self.block_updates_dropped = 0;
111        self.raw.clear();
112        self.raw_dropped = 0;
113    }
114
115    /// The extra lists for writing (marks them non-empty).
116    #[inline]
117    pub(super) fn more(&mut self) -> &mut MoreEvents {
118        self.more.any = true;
119        &mut self.more
120    }
121
122    /// Our status effects: every `entity_effect` / `remove_entity_effect` for us in arrival
123    /// order (a re-application with the same values is an `Applied` too), and `Cleared` when a
124    /// new player entity drops them (join, respawn). See [`TickContext::effects`].
125    #[must_use]
126    pub fn effects(&self) -> &[crate::common::EffectEvent] {
127        &self.more.effects
128    }
129
130    /// We were hurt (1.8 – 1.16.5 the hurt entity statuses for our entity, 26.x its
131    /// `damage_event`), in arrival order.
132    #[must_use]
133    pub fn hurt(&self) -> &[crate::common::Hurt] {
134        &self.more.hurt
135    }
136
137    /// The server's pushes on us, in arrival order: our own entity's velocity packets (an
138    /// attack's knockback among them) and explosion pushes (at most 64 between two behaviour
139    /// ticks). The movement already uses them ([`TickContext::velocity`]).
140    #[must_use]
141    pub fn knockback(&self) -> &[crate::common::Knockback] {
142        &self.more.knockback
143    }
144
145    /// Player deaths the server announced (ours: it sends them only to the dead player), with
146    /// the killer where the version sends one.
147    #[must_use]
148    pub fn deaths(&self) -> &[crate::common::Death] {
149        &self.more.deaths
150    }
151
152    /// Entity changes in arrival order (only with [`Interests::ENTITY_EVENTS`] and entity
153    /// tracking): spawns (a re-spawn with the same id is a `Removed` then a `Spawned`),
154    /// removals, moves, velocity, statuses, [`ghast_entity::Event::Hurt`] (the bot's own
155    /// entity included), metadata, riding, pickups. Kept while the behaviour is on hold, at
156    /// most 4096.
157    #[must_use]
158    pub fn entities(&self) -> &[ghast_entity::Event] {
159        &self.more.entities
160    }
161
162    /// Entity events dropped because more than 4096 piled up between two behaviour ticks.
163    #[must_use]
164    pub fn entities_dropped(&self) -> usize {
165        self.more.entities_dropped
166    }
167
168    /// Our item use's steps in order (at most 64 between two behaviour ticks): started by the
169    /// right button's key handling, finished by the server (entity status 9), released, or
170    /// interrupted (see [`crate::common::UseEvent`]). A use started and ended in the key
171    /// handling after the behaviour's tick shows up on the next tick, like
172    /// [`TickContext::using_item`].
173    #[must_use]
174    pub fn item_use(&self) -> &[crate::common::UseEvent] {
175        &self.more.item_use
176    }
177
178    /// Our item cooldowns' starts (the server's `set_cooldown` or our own item use) and ends,
179    /// in order (1.9+; at most 64 between two behaviour ticks). See [`crate::cooldown`];
180    /// [`TickContext::item_cooldowns`] holds them now.
181    #[must_use]
182    pub fn cooldowns(&self) -> &[crate::cooldown::CooldownEvent] {
183        &self.more.cooldowns
184    }
185
186    /// HUD changes in arrival order (only with [`Interests::HUD`]): boss bars, titles, the
187    /// action bar, the tab list's header and footer, and the countdowns running out (see
188    /// [`crate::hud`]). Kept while the behaviour is on hold, at most 256.
189    #[must_use]
190    pub fn hud(&self) -> &[crate::hud::HudEvent] {
191        &self.more.hud
192    }
193
194    /// HUD events dropped because more than 256 piled up between two behaviour ticks.
195    #[must_use]
196    pub fn hud_dropped(&self) -> usize {
197        self.more.hud_dropped
198    }
199
200    /// The server's answers to tab-completion requests ([`TickContext::tab_complete`]), in
201    /// arrival order (at most 64 between two behaviour ticks).
202    #[must_use]
203    pub fn tab_completions(&self) -> &[crate::hud::TabCompletion] {
204        &self.more.tabs
205    }
206
207    /// The sign editor's events, in order ([`crate::sign`]; the open editor is
208    /// [`crate::TickContext::sign_editor`]).
209    #[must_use]
210    pub fn signs(&self) -> &[crate::sign::SignEvent] {
211        &self.more.signs
212    }
213
214    /// The player fell asleep or woke up, in order ([`crate::TickContext::sleeping`] is the
215    /// state now).
216    #[must_use]
217    pub fn sleep(&self) -> &[crate::common::SleepEvent] {
218        &self.more.sleep
219    }
220
221    /// Our fishing bobber's changes, in arrival order: cast, bite, bite over, hooked, gone
222    /// (with entity tracking; see [`crate::fishing`]; [`crate::TickContext::fishing`] is the
223    /// bobber now). At most 64 between two behaviour ticks.
224    #[must_use]
225    pub fn fishing(&self) -> &[crate::fishing::FishingEvent] {
226        &self.more.fishing
227    }
228}
229
230/// The rarely filled event lists of [`TickEvents`].
231#[derive(Debug, Clone, Default, PartialEq)]
232pub(super) struct MoreEvents {
233    /// Something was written since the last clear.
234    pub(super) any: bool,
235    pub(super) effects: Vec<crate::common::EffectEvent>,
236    pub(super) hurt: Vec<crate::common::Hurt>,
237    pub(super) knockback: Vec<crate::common::Knockback>,
238    pub(super) deaths: Vec<crate::common::Death>,
239    pub(super) entities: Vec<ghast_entity::Event>,
240    pub(super) entities_dropped: usize,
241    pub(super) item_use: Vec<crate::common::UseEvent>,
242    pub(super) cooldowns: Vec<crate::cooldown::CooldownEvent>,
243    pub(super) hud: Vec<crate::hud::HudEvent>,
244    pub(super) hud_dropped: usize,
245    pub(super) tabs: Vec<crate::hud::TabCompletion>,
246    pub(super) signs: Vec<crate::sign::SignEvent>,
247    pub(super) sleep: Vec<crate::common::SleepEvent>,
248    pub(super) fishing: Vec<crate::fishing::FishingEvent>,
249}