ghast-client 0.4.1

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 and 26.1)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Per-version glue between the generated packets of `ghast-protocol` and the common layer.
//!
//! [`ClientVersion`] is implemented for the protocol marker types of the enabled versions
//! (`ghast_protocol::{V1_8, V1_12_2, V1_16_5, V26_1}`).
//! The connection and its tick loop are generic over it, so every hot path is monomorphised
//! per version (no trait objects); the bot API above it only sees [`crate::common`] types.

use std::fmt;
use std::str::FromStr;

use ghast_protocol::{DecodeError, EncodeError, FrameEncoder, PacketFilter, RawPacket, Version};

use crate::common::{
    ConfigInbound, ConfigOutbound, Inbound, LoginInbound, Outbound, Pose, Teleport,
};
use crate::interest::Interests;

mod caps;
#[cfg(feature = "v26_1")]
mod commands;
#[cfg(feature = "v1_12_2")]
mod v1_12_2;
#[cfg(feature = "v1_16_5")]
mod v1_16_5;
#[cfg(feature = "v1_8")]
mod v1_8;
#[cfg(feature = "v26_1")]
mod v26_1;

pub use caps::{Capabilities, DataSet, ItemModel, PhysicsRules};
#[cfg(feature = "v1_8")]
pub use ghast_protocol::v1_8::V1_8;
#[cfg(feature = "v1_12_2")]
pub use ghast_protocol::v1_12_2::V1_12_2;
#[cfg(feature = "v1_16_5")]
pub use ghast_protocol::v1_16_5::V1_16_5;
#[cfg(feature = "v26_1")]
pub use ghast_protocol::v26_1::V26_1;
#[cfg(feature = "v26_1")]
pub use v26_1::Wire26_1;

/// A protocol version the client supports, chosen at run time.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum GameVersion {
    /// 1.8 – 1.8.9 (protocol 47).
    V1_8_9,
    /// 1.12.2 (protocol 340).
    V1_12_2,
    /// 1.16.4 – 1.16.5 (protocol 754).
    V1_16_5,
    /// 26.1 – 26.1.2 (protocol 775).
    V26_1,
}

impl GameVersion {
    /// Every supported version, oldest first. A slice, so that adding a version is not a
    /// breaking change.
    pub const ALL: &'static [GameVersion] = &[
        GameVersion::V1_8_9,
        GameVersion::V1_12_2,
        GameVersion::V1_16_5,
        GameVersion::V26_1,
    ];

    /// What this version can do (the row the client core reads instead of comparing
    /// versions).
    #[must_use]
    pub const fn caps(self) -> &'static Capabilities {
        match self {
            Self::V1_8_9 => &caps::V1_8_9,
            Self::V1_12_2 => &caps::V1_12_2,
            Self::V1_16_5 => &caps::V1_16_5,
            Self::V26_1 => &caps::V26_1,
        }
    }

    /// Whether this build of the library includes the version (its cargo feature is on).
    #[must_use]
    pub const fn enabled(self) -> bool {
        match self {
            Self::V1_8_9 => cfg!(feature = "v1_8"),
            Self::V1_12_2 => cfg!(feature = "v1_12_2"),
            Self::V1_16_5 => cfg!(feature = "v1_16_5"),
            Self::V26_1 => cfg!(feature = "v26_1"),
        }
    }

    /// Protocol number.
    #[must_use]
    pub const fn protocol(self) -> i32 {
        self.caps().protocol
    }

    /// This version's row in the protocol table ([`ghast_protocol::versions`]): module,
    /// feature, every game version sharing the protocol number, wire facts (the row exists
    /// whether or not the version is compiled in; see its `enabled`).
    #[must_use]
    pub fn protocol_info(self) -> &'static ghast_protocol::ProtocolVersion {
        ghast_protocol::versions::by_protocol(self.protocol())
            .expect("every GameVersion has a row in the protocol table")
    }

    /// Display name (`"1.8.9"`, `"1.12.2"`, `"1.16.5"`).
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::V1_8_9 => "1.8.9",
            Self::V1_12_2 => "1.12.2",
            Self::V1_16_5 => "1.16.5",
            Self::V26_1 => "26.1",
        }
    }

    /// `true` for 1.9 and later: the attack cooldown, the off hand, swinging after the click,
    /// no 0.1 targeting border on entity boxes ([`Capabilities::combat_1_9`]).
    #[must_use]
    pub const fn combat_1_9(self) -> bool {
        self.caps().combat_1_9
    }

    /// Eye height above the feet while standing ([`Capabilities::eye_height`]).
    #[must_use]
    pub const fn eye_height(self) -> f64 {
        self.caps().eye_height
    }

    /// Eye height while sneaking: 1.54 in 1.8 – 1.12 (`getEyeHeight` − 0.08), 1.27 in 1.16
    /// (the crouching pose) ([`Capabilities::sneak_eye_height`]).
    #[must_use]
    pub const fn sneak_eye_height(self) -> f64 {
        self.caps().sneak_eye_height
    }
}

impl fmt::Display for GameVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.name())
    }
}

/// Error parsing a [`GameVersion`].
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("unsupported Minecraft version `{0}` (supported: 1.8.9, 1.12.2, 1.16.5, 26.1)")]
pub struct UnknownVersion(pub String);

impl FromStr for GameVersion {
    type Err = UnknownVersion;

    /// Accepts `1.8`, `1.8.x`, `47`, `1.12.2`, `340`, `1.16.4`, `1.16.5`, `754`, `26.1`,
    /// `26.1.1`, `26.1.2` and `775`.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.trim() {
            "1.8" | "1.8.0" | "1.8.1" | "1.8.2" | "1.8.3" | "1.8.4" | "1.8.5" | "1.8.6"
            | "1.8.7" | "1.8.8" | "1.8.9" | "47" => Ok(Self::V1_8_9),
            "1.12.2" | "340" => Ok(Self::V1_12_2),
            "1.16.4" | "1.16.5" | "754" => Ok(Self::V1_16_5),
            "26.1" | "26.1.0" | "26.1.1" | "26.1.2" | "775" => Ok(Self::V26_1),
            other => Err(UnknownVersion(other.to_owned())),
        }
    }
}

/// What a protocol version needs to provide to the client.
///
/// Sealed: implemented for the enabled versions' markers (`ghast_protocol::{V1_8, V1_12_2,
/// V1_16_5, V26_1}`) only.
pub trait ClientVersion: Version + sealed::Sealed {
    /// The run-time tag of this version.
    const GAME: GameVersion;
    /// Longest chat line the server accepts, in UTF-16 units (vanilla truncates to it).
    const CHAT_LIMIT: usize;

    /// Queues the handshake packet (next state = login).
    fn write_handshake(enc: &mut FrameEncoder, host: &str, port: u16) -> Result<(), EncodeError>;
    /// Per-connection state of the wire layer (block action sequence numbers, chat
    /// acknowledgements, cookies); `()` for versions without any.
    type Wire: Default + fmt::Debug + Send;

    /// Queues the login start packet (1.19+ also send the profile id).
    fn write_login_start(
        enc: &mut FrameEncoder,
        username: &str,
        uuid: uuid::Uuid,
    ) -> Result<(), EncodeError>;
    /// Queues the encryption response.
    fn write_encryption_response(
        enc: &mut FrameEncoder,
        shared_secret: &[u8],
        verify_token: &[u8],
    ) -> Result<(), EncodeError>;
    /// Queues a "not understood" answer to a login plugin request.
    fn write_plugin_response(enc: &mut FrameEncoder, message_id: i32) -> Result<(), EncodeError>;
    /// Decodes a login-state packet; `Ok(None)` for packets the client ignores.
    fn decode_login(raw: &RawPacket) -> Result<Option<LoginInbound>, DecodeError>;
    /// Queues the handshake of a connection that follows a server transfer (1.20.5+ intent 3,
    /// `transfer`); older versions have no transfers.
    fn write_transfer_handshake(
        enc: &mut FrameEncoder,
        host: &str,
        port: u16,
    ) -> Result<(), EncodeError> {
        Self::write_handshake(enc, host, port)
    }
    /// The cookies the server stored (carried to a transfer target, vanilla `TransferState`).
    fn take_cookies(
        wire: &mut Self::Wire,
    ) -> Vec<(ghast_protocol::types::ByteString, bytes::Bytes)> {
        let _ = wire;
        Vec::new()
    }
    /// Restores carried cookies into a new connection's wire state.
    fn put_cookies(
        wire: &mut Self::Wire,
        cookies: Vec<(ghast_protocol::types::ByteString, bytes::Bytes)>,
    ) {
        let _ = (wire, cookies);
    }
    /// A login-state cookie answer from the wire's cookies (a transferred connection has the
    /// previous server's); default: [`ClientVersion::write_login_cookie_response`].
    fn write_login_cookie_response_from(
        enc: &mut FrameEncoder,
        key: &str,
        wire: &mut Self::Wire,
    ) -> Result<(), EncodeError> {
        let _ = wire;
        Self::write_login_cookie_response(enc, key)
    }
    /// Queues a login-state cookie answer (1.20.5+; never called for older versions).
    fn write_login_cookie_response(enc: &mut FrameEncoder, key: &str) -> Result<(), EncodeError> {
        let _ = (enc, key);
        Ok(())
    }
    /// Queues `login_acknowledged` (1.20.2+; never called for older versions).
    fn write_login_acknowledged(enc: &mut FrameEncoder) -> Result<(), EncodeError> {
        let _ = enc;
        Ok(())
    }
    /// Decodes a configuration-state packet (1.20.2+); `Ok(None)` for packets the client
    /// ignores. Registry data is returned raw ([`ConfigInbound::RegistryData`]).
    fn decode_configuration(
        raw: &RawPacket,
        wire: &mut Self::Wire,
    ) -> Result<Option<ConfigInbound>, DecodeError> {
        let _ = (raw, wire);
        Ok(None)
    }
    /// Queues a configuration-state packet (1.20.2+).
    fn write_configuration(
        enc: &mut FrameEncoder,
        out: &ConfigOutbound,
        wire: &mut Self::Wire,
    ) -> Result<(), EncodeError> {
        let _ = (enc, out, wire);
        Ok(())
    }
    /// A `registry_data` packet of the configuration state, for the wire layer (chat types).
    fn note_registry(raw: &RawPacket, wire: &mut Self::Wire) {
        let _ = (raw, wire);
    }
    /// Queues `configuration_acknowledged` (play → configuration, 1.20.2+).
    fn write_configuration_acknowledged(enc: &mut FrameEncoder) -> Result<(), EncodeError> {
        let _ = enc;
        Ok(())
    }
    /// Gives the inventory what the configuration state carried for it (26.1: registry and tag
    /// bodies, kept raw by the inventory until a screen needs them), once per configuration.
    fn feed_inventory_config(inv: &mut ghast_inventory::Inventory, wire: &mut Self::Wire) {
        let _ = (inv, wire);
    }
    /// The entity a mount screen (`horse_screen_open`) belongs to; `None` for other packets
    /// and versions without one.
    #[inline(always)]
    fn mount_screen_entity(raw: &RawPacket) -> Option<i32> {
        let _ = raw;
        None
    }
    /// Opens the mount screen of [`ClientVersion::mount_screen_entity`] with what it reads off
    /// the entity (type id, `canUseSlot(SADDLE)`, `canUseSlot(BODY)`; `None`: unknown).
    fn open_mount_screen(
        inv: &mut ghast_inventory::Inventory,
        raw: &RawPacket,
        mount: Option<(i32, bool, bool)>,
    ) -> Result<(), DecodeError> {
        let _ = (inv, raw, mount);
        Ok(())
    }
    /// Sees a batch of outbound packets before they are written (26.1: the rotation of this
    /// tick's movement packet, which `use_item` carries).
    #[inline(always)]
    fn prepare_play(out: &[crate::common::Outbound], wire: &mut Self::Wire) {
        let _ = (out, wire);
    }
    /// Starts a chat session with these profile keys (1.19.3+ `setKeyPair`: a new random
    /// session id, a new signature chain); `false` when the version has no signed chat. The
    /// core then announces it ([`crate::common::Outbound::ChatSessionUpdate`]).
    fn begin_chat_session(
        wire: &mut Self::Wire,
        keys: &std::sync::Arc<ghast_auth::chat::ProfileKeys>,
        profile_id: uuid::Uuid,
    ) -> bool {
        let _ = (wire, keys, profile_id);
        false
    }
    /// What the client sends on `start_configuration` before `configuration_acknowledged`
    /// (26.1 `handleConfigurationStart`: the pending chat acknowledgement).
    fn write_configuration_start_replies(
        enc: &mut FrameEncoder,
        wire: &mut Self::Wire,
    ) -> Result<(), EncodeError> {
        let _ = (enc, wire);
        Ok(())
    }
    /// Packets the wire layer itself owes the server after decoding (chat acknowledgements).
    fn write_wire_replies(
        enc: &mut FrameEncoder,
        wire: &mut Self::Wire,
    ) -> Result<(), EncodeError> {
        let _ = (enc, wire);
        Ok(())
    }

    /// The play-state packet filter for the given interests: everything the core needs plus
    /// what the interests ask for. Everything else is dropped before it is decoded.
    fn play_filter(interests: Interests) -> PacketFilter;
    /// Decodes a play-state packet; `Ok(None)` for packets the core does not use.
    fn decode_play(raw: &RawPacket, wire: &mut Self::Wire) -> Result<Option<Inbound>, DecodeError>;
    /// Applies an inventory packet (windows, slots, confirms, held slot, login / respawn
    /// reset) to the inventory; returns the confirm answer to send now, if any. Other packets
    /// are ignored.
    fn inventory_packet(
        inv: &mut ghast_inventory::Inventory,
        raw: &RawPacket,
    ) -> Result<Option<ghast_inventory::Outgoing>, DecodeError>;
    /// The inventory version.
    const INVENTORY: ghast_inventory::Version;
    /// Hands the wire layer's registry facts to the inventory (1.20.5+ hashed stacks).
    fn configure_inventory(inv: &mut ghast_inventory::Inventory, wire: &Self::Wire) {
        let _ = (inv, wire);
    }
    /// Queues a play-state packet.
    fn write_play(
        enc: &mut FrameEncoder,
        out: &Outbound,
        wire: &mut Self::Wire,
    ) -> Result<(), EncodeError>;
    /// Applies a server teleport to the pose the way this version's client does
    /// (`onPlayerPositionLook` + `Entity.updatePositionAndAngles`).
    fn apply_teleport(pose: &mut Pose, t: &Teleport);
}

/// The world and movement implementation of a version (crate-internal).
pub(crate) trait HasSim {
    /// World + mover.
    type Sim: crate::sim::Sim;
    /// A fresh world and player.
    fn new_sim(opts: &crate::sim::SimOptions) -> Self::Sim;
}

mod sealed {
    // Deliberately unnameable: downstream crates cannot implement `ClientVersion`.
    #[allow(unnameable_types, reason = "sealed trait pattern")]
    pub trait Sealed {}
    #[cfg(feature = "v1_8")]
    impl Sealed for super::V1_8 {}
    #[cfg(feature = "v1_12_2")]
    impl Sealed for super::V1_12_2 {}
    #[cfg(feature = "v1_16_5")]
    impl Sealed for super::V1_16_5 {}
    #[cfg(feature = "v26_1")]
    impl Sealed for super::V26_1 {}
}

/// Truncates `s` to at most `max` UTF-16 units (Java's `String.length()`), at a character
/// boundary.
pub(crate) fn truncate_utf16(s: &str, max: usize) -> &str {
    let mut units = 0;
    for (i, c) in s.char_indices() {
        units += c.len_utf16();
        if units > max {
            return &s[..i];
        }
    }
    s
}

/// The common part of vanilla's teleport handling: relative axes are added, absolute axes
/// zero that axis' motion; rotations are added when relative. Returns the new yaw / pitch
/// before the version's `updatePositionAndAngles` normalisation.
pub(crate) fn teleport_target(pose: &mut Pose, t: &Teleport) -> (f32, f32) {
    use crate::common::teleport_flags as fl;
    let mut x = t.x;
    let mut y = t.y;
    let mut z = t.z;
    if t.flags & fl::X != 0 {
        x += pose.pos.x;
    } else {
        pose.motion.x = 0.0;
    }
    if t.flags & fl::Y != 0 {
        y += pose.pos.y;
    } else {
        pose.motion.y = 0.0;
    }
    if t.flags & fl::Z != 0 {
        z += pose.pos.z;
    } else {
        pose.motion.z = 0.0;
    }
    pose.pos = crate::common::Vec3::new(x, y, z);
    let mut yaw = t.yaw;
    let mut pitch = t.pitch;
    if t.flags & fl::PITCH != 0 {
        pitch += pose.pitch;
    }
    if t.flags & fl::YAW != 0 {
        yaw += pose.yaw;
    }
    (yaw, pitch)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn protocol_rows() {
        for &v in GameVersion::ALL {
            assert_eq!(v.caps().version, v);
            let row = v.protocol_info();
            assert_eq!(row.protocol, v.protocol());
            assert!(row.enabled || !v.enabled());
            assert!(row.game_versions.contains(&v.name()));
        }
    }

    #[test]
    fn parse_versions() {
        assert_eq!("1.8.8".parse::<GameVersion>().unwrap(), GameVersion::V1_8_9);
        assert_eq!("754".parse::<GameVersion>().unwrap(), GameVersion::V1_16_5);
        assert_eq!(
            "1.12.2".parse::<GameVersion>().unwrap(),
            GameVersion::V1_12_2
        );
        assert_eq!("340".parse::<GameVersion>().unwrap(), GameVersion::V1_12_2);
        assert!("1.12.1".parse::<GameVersion>().is_err());
        assert_eq!(GameVersion::V1_8_9.protocol(), 47);
        assert_eq!(GameVersion::V1_12_2.protocol(), 340);
        assert_eq!(GameVersion::V1_16_5.protocol(), 754);
    }

    #[test]
    fn utf16_truncation() {
        assert_eq!(truncate_utf16("hello", 3), "hel");
        assert_eq!(truncate_utf16("hello", 10), "hello");
        // U+1F600 is two UTF-16 units: never split.
        assert_eq!(truncate_utf16("a\u{1F600}b", 2), "a");
        assert_eq!(truncate_utf16("a\u{1F600}b", 3), "a\u{1F600}");
    }
}