Skip to main content

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 and 26.2).
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    )),
37    allow(dead_code)
38)]
39// A build with no version at all keeps only the shared store.
40#![cfg_attr(
41    not(any(
42        feature = "v1_8",
43        feature = "v1_12",
44        feature = "v1_16_5",
45        feature = "v26_1",
46        feature = "v26_2"
47    )),
48    allow(unused_imports, unused_variables)
49)]
50
51mod common;
52mod entity;
53#[cfg(any(feature = "v26_1", feature = "v26_2"))]
54mod modern_state;
55mod store;
56#[cfg(feature = "v1_12")]
57pub mod v1_12;
58#[cfg(feature = "v1_16_5")]
59pub mod v1_16_5;
60#[cfg(feature = "v1_8")]
61pub mod v1_8;
62
63/// The modern tracker source (`src/v26_1.rs`) compiled for protocol 775: `data` and `proto`
64/// name that protocol's `ghast-data` and `ghast-protocol` modules.
65#[cfg(feature = "v26_1")]
66#[path = "."]
67mod modern_775 {
68    pub(crate) use ghast_data::v26_1 as data;
69    use ghast_protocol::types::slot::v775::ItemStack;
70    pub(crate) use ghast_protocol::v26_1 as proto;
71
72    /// The entity era this instance stamps.
73    pub(crate) const ERA: crate::entity::Era = crate::entity::Era::V26;
74    /// Registry id of `minecraft:scale` in 26.1.
75    pub(crate) const SCALE_ATTRIBUTE_ID: i32 = 25;
76
77    /// A slot with 26.1's component codec.
78    #[inline]
79    pub(crate) fn read_stack(
80        r: &mut ghast_protocol::Reader<'_>,
81    ) -> crate::common::R<Option<ItemStack>> {
82        ItemStack::read(r)
83    }
84
85    #[path = "v26_1.rs"]
86    pub mod tracker;
87}
88#[cfg(feature = "v26_1")]
89#[doc(inline)]
90pub use modern_775::tracker as v26_1;
91
92/// The same tracker source for protocol 776 (26.2): ids, sizes and the slot codec come from
93/// `ghast_data::v26_2` / `ghast_protocol::v26_2`; the entity data serializers are unchanged.
94#[cfg(feature = "v26_2")]
95#[path = "."]
96mod modern_776 {
97    pub(crate) use ghast_data::v26_2 as data;
98    use ghast_protocol::types::slot::v775::ItemStack;
99    pub(crate) use ghast_protocol::v26_2 as proto;
100
101    /// The entity era this instance stamps.
102    pub(crate) const ERA: crate::entity::Era = crate::entity::Era::V26_2;
103    /// Registry id of `minecraft:scale` in 26.2 (`minecraft:attribute` gained entries before it).
104    pub(crate) const SCALE_ATTRIBUTE_ID: i32 = 30;
105
106    /// A slot with 26.2's component codec.
107    #[inline]
108    pub(crate) fn read_stack(
109        r: &mut ghast_protocol::Reader<'_>,
110    ) -> crate::common::R<Option<ItemStack>> {
111        ItemStack::read_776(r)
112    }
113
114    #[allow(clippy::duplicate_mod)]
115    #[path = "v26_1.rs"]
116    pub mod tracker;
117}
118#[cfg(feature = "v26_2")]
119#[doc(inline)]
120pub use modern_776::tracker as v26_2;
121
122pub use entity::{
123    EQUIPMENT_SLOTS, Effect, Entity, EntityClass, EntityType, Item, MetaValue, Metadata,
124};
125pub use store::{Config, Entities, Event, PlayerInfo};
126
127/// What applying a packet did.
128#[derive(Clone, Copy, Debug, PartialEq, Eq)]
129pub enum Applied {
130    /// Tracked state changed.
131    Changed,
132    /// Valid, but nothing changed (unknown entity id, unknown type, the bot's own position, ...).
133    Ignored,
134    /// Not a packet this tracker handles (or one its configuration does not want).
135    NotEntityPacket,
136}