ghast_entity/lib.rs
1//! Entity tracking for the ghast Minecraft Java Edition client library (1.8.9, 1.12.2, 1.16.5
2//! 26.1, 26.2 and 26.3).
3//!
4//! [`v1_8::Tracker`] / [`v1_12::Tracker`] / [`v1_16_5::Tracker`] / `v26_1::Tracker` take raw
5//! clientbound play packets
6//! ([`ghast_protocol::RawPacket`]) and keep every entity the server announced, keyed by id:
7//! type, UUID, **server position** (exactly as the vanilla client tracks it from the packets,
8//! with each version's fixed-point rules), rotation, last velocity, on-ground, riding / leash
9//! links, and optionally metadata, equipment, effects and the tab list.
10//!
11//! - **Cheap.** Packets are parsed by hand straight from the packet body; movement, look,
12//! teleport, velocity, head-rotation and destroy packets do not allocate. Lookups are one hash
13//! of the id. Parts nobody needs are switched off in [`Config`], and `Tracker::wants(id)` /
14//! `packet_ids()` tell the client which packets it can drop before decoding.
15//! - **Vanilla semantics.** Spawns replace an existing id, unknown types spawn nothing, moves
16//! for unknown ids are ignored, `login` and world changes clear everything, pickups remove
17//! the item (1.16: when its stack runs out), riding follows each version's rules.
18//! - **Never panics on server data**: malformed bodies return a [`ghast_protocol::DecodeError`].
19//!
20//! ```
21//! use ghast_entity::{Config, v1_8::Tracker};
22//! let tracker = Tracker::new(Config::default());
23//! assert!(tracker.get(42).is_none());
24//! ```
25
26#![forbid(unsafe_code)]
27#![warn(missing_docs, unnameable_types)]
28// A build with only some versions leaves helpers of the others unused.
29#![cfg_attr(
30 not(all(
31 feature = "v1_8",
32 feature = "v1_12",
33 feature = "v1_16_5",
34 feature = "v26_1",
35 feature = "v26_2",
36 feature = "v26_3"
37 )),
38 allow(dead_code)
39)]
40// A build with no version at all keeps only the shared store.
41#![cfg_attr(
42 not(any(
43 feature = "v1_8",
44 feature = "v1_12",
45 feature = "v1_16_5",
46 feature = "v26_1",
47 feature = "v26_2",
48 feature = "v26_3"
49 )),
50 allow(unused_imports, unused_variables)
51)]
52
53#[cfg(feature = "v1_16_5")]
54mod client16;
55mod common;
56mod drawn;
57mod entity;
58#[cfg(any(feature = "v1_8", feature = "v1_12", feature = "v1_16_5"))]
59mod hanging;
60#[cfg(any(feature = "v26_1", feature = "v26_2", feature = "v26_3"))]
61mod modern_state;
62mod store;
63#[cfg(feature = "v1_12")]
64pub mod v1_12;
65#[cfg(feature = "v1_16_5")]
66pub mod v1_16_5;
67#[cfg(feature = "v1_8")]
68pub mod v1_8;
69
70/// The modern tracker source (`src/v26_1.rs`) compiled for protocol 775: `data` and `proto`
71/// name that protocol's `ghast-data` and `ghast-protocol` modules.
72#[cfg(feature = "v26_1")]
73#[path = "."]
74mod modern_775 {
75 pub(crate) use ghast_data::v26_1 as data;
76 use ghast_protocol::types::slot::v775::ItemStack;
77 pub(crate) use ghast_protocol::v26_1 as proto;
78
79 /// The entity era this instance stamps.
80 pub(crate) const ERA: crate::entity::Era = crate::entity::Era::V26;
81 /// Registry id of `minecraft:scale` in 26.1.
82 pub(crate) const SCALE_ATTRIBUTE_ID: i32 = 25;
83 /// 26.3's movement model (see `modern_777`): off.
84 pub(crate) const PATHS: bool = false;
85
86 /// A slot with 26.1's component codec.
87 #[inline]
88 pub(crate) fn read_stack(
89 r: &mut ghast_protocol::Reader<'_>,
90 ) -> crate::common::R<Option<ItemStack>> {
91 ItemStack::read(r)
92 }
93
94 #[path = "v26_1.rs"]
95 pub mod tracker;
96}
97#[cfg(feature = "v26_1")]
98#[doc(inline)]
99pub use modern_775::tracker as v26_1;
100
101/// The same tracker source for protocol 776 (26.2): ids, sizes and the slot codec come from
102/// `ghast_data::v26_2` / `ghast_protocol::v26_2`; the entity data serializers are unchanged.
103#[cfg(feature = "v26_2")]
104#[path = "."]
105mod modern_776 {
106 pub(crate) use ghast_data::v26_2 as data;
107 use ghast_protocol::types::slot::v775::ItemStack;
108 pub(crate) use ghast_protocol::v26_2 as proto;
109
110 /// The entity era this instance stamps.
111 pub(crate) const ERA: crate::entity::Era = crate::entity::Era::V26_2;
112 /// Registry id of `minecraft:scale` in 26.2 (`minecraft:attribute` gained entries before it).
113 pub(crate) const SCALE_ATTRIBUTE_ID: i32 = 30;
114 /// 26.3's movement model (see `modern_777`): off.
115 pub(crate) const PATHS: bool = false;
116
117 /// A slot with 26.2's component codec.
118 #[inline]
119 pub(crate) fn read_stack(
120 r: &mut ghast_protocol::Reader<'_>,
121 ) -> crate::common::R<Option<ItemStack>> {
122 ItemStack::read_776(r)
123 }
124
125 #[allow(clippy::duplicate_mod)]
126 #[path = "v26_1.rs"]
127 pub mod tracker;
128}
129#[cfg(feature = "v26_2")]
130#[doc(inline)]
131pub use modern_776::tracker as v26_2;
132
133/// The same tracker source for protocol 777 (26.3), with 26.3's movement model switched on
134/// ([`PATHS`](modern_777::PATHS)): relative moves and position syncs carry position paths,
135/// living entities interpolate along a queue of steps (`SteppedInterpolationHandler`), and the
136/// interpolation handlers compare rotations with `==`. Entity data gains the `dye_color`
137/// serializer (id 43).
138#[cfg(feature = "v26_3")]
139#[path = "."]
140mod modern_777 {
141 pub(crate) use ghast_data::v26_3 as data;
142 use ghast_protocol::types::slot::v775::ItemStack;
143 pub(crate) use ghast_protocol::v26_3 as proto;
144
145 /// The entity era this instance stamps.
146 pub(crate) const ERA: crate::entity::Era = crate::entity::Era::V26_3;
147 /// Registry id of `minecraft:scale` in 26.3.
148 pub(crate) const SCALE_ATTRIBUTE_ID: i32 = 30;
149 /// 26.3's movement model: `VecDelta` / `PositionPath` packets, `SteppedInterpolationHandler`
150 /// for living entities, `LinearInterpolationHandler` for the rest, `dye_color` metadata.
151 pub(crate) const PATHS: bool = true;
152
153 /// A slot with 26.3's component codec.
154 #[inline]
155 pub(crate) fn read_stack(
156 r: &mut ghast_protocol::Reader<'_>,
157 ) -> crate::common::R<Option<ItemStack>> {
158 ItemStack::read_777(r)
159 }
160
161 #[allow(clippy::duplicate_mod)]
162 #[path = "v26_1.rs"]
163 pub mod tracker;
164}
165#[cfg(feature = "v26_3")]
166#[doc(inline)]
167pub use modern_777::tracker as v26_3;
168
169pub use entity::{
170 EQUIPMENT_SLOTS, Effect, Entity, EntityClass, EntityType, Item, MetaValue, Metadata,
171};
172pub use store::{Config, Entities, Event, PlayerInfo};
173
174/// What applying a packet did.
175#[derive(Clone, Copy, Debug, PartialEq, Eq)]
176pub enum Applied {
177 /// Tracked state changed.
178 Changed,
179 /// Valid, but nothing changed (unknown entity id, unknown type, the bot's own position, ...).
180 Ignored,
181 /// Not a packet this tracker handles (or one its configuration does not want).
182 NotEntityPacket,
183}