ghast-client 0.4.8

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 and 26.2)
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Modern team collision rules for entity pushes: the part of the client scoreboard that
//! `EntitySelector.pushableBy` reads (each team's `collisionRule` and who is on which team),
//! kept from `set_player_team` packets only while entities are tracked.

use std::collections::HashMap;

use bytes::Bytes;
use ghast_protocol::codec::Reader;
use ghast_protocol::nbt::AnonNbt;

/// `Team.CollisionRule` network ids.
pub(crate) const ALWAYS: u8 = 0;
pub(crate) const NEVER: u8 = 1;
pub(crate) const PUSH_OTHER_TEAMS: u8 = 2;
pub(crate) const PUSH_OWN_TEAM: u8 = 3;

/// The order of a team's parameters in `set_player_team` (`Parameters.STREAM_CODEC`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)] // one variant per modern row; single-version builds use one
pub(crate) enum TeamLayout {
    /// 26.1: display name, options byte, name tag visibility, collision rule, colour
    /// (`ChatFormatting`), prefix, suffix.
    RuleBeforeAffixes,
    /// 26.2: display name, prefix, suffix, name tag visibility, collision rule, optional
    /// colour (`TeamColor`), options byte.
    AffixesFirst,
    /// 1.12.2 `teams`: display name, prefix and suffix as plain strings, options byte, name tag
    /// visibility and collision rule as names, colour byte.
    Legacy1_12,
    /// 1.13 – 1.16.5 `teams`: display name (chat JSON), options byte, name tag visibility and
    /// collision rule as names, colour (VarInt), prefix and suffix (chat JSON).
    Legacy1_16,
}

/// A collision rule by its network name (`always`, `never`, `pushOtherTeams`, `pushOwnTeam`),
/// as the 1.9 – 1.16 `teams` packet sends it. `None` for an unknown name: the client then
/// keeps the team's rule (a new team's is `always`).
pub(crate) fn rule_by_name(name: &str) -> Option<u8> {
    Some(match name {
        "always" => ALWAYS,
        "never" => NEVER,
        "pushOtherTeams" => PUSH_OTHER_TEAMS,
        "pushOwnTeam" => PUSH_OWN_TEAM,
        _ => return None,
    })
}

/// The client scoreboard's teams as far as collision rules go.
#[derive(Debug, Default)]
pub(crate) struct Teams {
    /// Team name → slot in `rules`.
    by_name: HashMap<Box<str>, u32>,
    /// Slot → collision rule (`None`: a free slot).
    rules: Vec<Option<u8>>,
    /// Scoreboard name (player name, or an entity's UUID string) → team slot.
    members: HashMap<Box<str>, u32>,
    /// Teams whose rule is not `always` (none: every push is allowed, nothing is looked up).
    strict: u32,
}

impl Teams {
    /// Forgets everything (a new play phase has a new scoreboard).
    pub(crate) fn clear(&mut self) {
        *self = Teams::default();
    }

    /// `true` when some team restricts pushing.
    #[inline]
    pub(crate) fn any_rule(&self) -> bool {
        self.strict > 0
    }

    /// The team slot and collision rule of a scoreboard name (`always` without a team).
    pub(crate) fn of(&self, name: &str) -> (Option<u32>, u8) {
        match self.members.get(name) {
            Some(&s) => (Some(s), self.rules[s as usize].unwrap_or(ALWAYS)),
            None => (None, ALWAYS),
        }
    }

    fn set_rule(&mut self, slot: u32, rule: u8) {
        let r = &mut self.rules[slot as usize];
        if r.is_some_and(|o| o != ALWAYS) {
            self.strict -= 1;
        }
        if rule != ALWAYS {
            self.strict += 1;
        }
        *r = Some(rule);
    }

    /// `ClientPacketListener.handleSetPlayerTeamPacket` for a `set_player_team` body. Only the
    /// name, the action, the collision rule and the member list are read (the components are
    /// skipped). Returns `false` for a body that does not parse (nothing more is applied).
    pub(crate) fn apply(&mut self, body: &Bytes, layout: TeamLayout) -> bool {
        self.apply_inner(body, layout).is_some()
    }

    fn apply_inner(&mut self, body: &Bytes, layout: TeamLayout) -> Option<()> {
        let mut r = Reader::new(body);
        let name = r.read_string().ok()?;
        let mode = r.read_i8().ok()?;
        // Action ADD (0): Scoreboard.addPlayerTeam (an existing team is kept); else the team
        // must exist (vanilla logs and ignores the packet).
        let slot = if mode == 0 {
            match self.by_name.get(&*name) {
                Some(&s) => s,
                None => {
                    let s = match self.rules.iter().position(Option::is_none) {
                        Some(i) => i as u32,
                        None => {
                            self.rules.push(None);
                            (self.rules.len() - 1) as u32
                        }
                    };
                    self.rules[s as usize] = Some(ALWAYS);
                    self.by_name.insert((&*name).into(), s);
                    s
                }
            }
        } else {
            *self.by_name.get(&*name)?
        };
        if (mode == 0 || mode == 2) && layout == TeamLayout::Legacy1_12 {
            r.read_string().ok()?; // display name
            r.read_string().ok()?; // prefix
            r.read_string().ok()?; // suffix
            r.read_u8().ok()?; // options
            r.read_string().ok()?; // name tag visibility
            let rule = r.read_string().ok()?;
            r.read_u8().ok()?; // colour
            if let Some(rule) = rule_by_name(&rule) {
                self.set_rule(slot, rule);
            }
        } else if (mode == 0 || mode == 2) && layout == TeamLayout::Legacy1_16 {
            r.read_string().ok()?; // display name
            r.read_u8().ok()?; // options
            r.read_string().ok()?; // name tag visibility
            let rule = r.read_string().ok()?;
            r.read_varint().ok()?; // colour
            r.read_string().ok()?; // prefix
            r.read_string().ok()?; // suffix
            if let Some(rule) = rule_by_name(&rule) {
                self.set_rule(slot, rule);
            }
        } else if mode == 0 || mode == 2 {
            AnonNbt::read(&mut r).ok()?; // display name
            let rule = match layout {
                TeamLayout::RuleBeforeAffixes => {
                    r.read_u8().ok()?; // options
                    r.read_varint().ok()?; // name tag visibility
                    let rule = r.read_varint().ok()?;
                    r.read_varint().ok()?; // color
                    AnonNbt::read(&mut r).ok()?; // prefix
                    AnonNbt::read(&mut r).ok()?; // suffix
                    rule
                }
                // Read above.
                TeamLayout::Legacy1_12 | TeamLayout::Legacy1_16 => return None,
                TeamLayout::AffixesFirst => {
                    AnonNbt::read(&mut r).ok()?; // prefix
                    AnonNbt::read(&mut r).ok()?; // suffix
                    r.read_varint().ok()?; // name tag visibility
                    let rule = r.read_varint().ok()?;
                    if r.read_bool().ok()? {
                        r.read_varint().ok()?; // color
                    }
                    r.read_u8().ok()?; // options
                    rule
                }
            };
            // ByIdMap.continuous with OutOfBoundsStrategy.ZERO: unknown ids are `always`.
            let rule = u8::try_from(rule)
                .ok()
                .filter(|&x| x <= PUSH_OWN_TEAM)
                .unwrap_or(ALWAYS);
            self.set_rule(slot, rule);
        }
        if mode == 0 || mode == 3 || mode == 4 {
            let n = r.read_varint().ok()?;
            for _ in 0..n.max(0) {
                let player = r.read_string().ok()?;
                if mode == 4 {
                    // removePlayerFromTeam(player, team): only a member of this team (vanilla
                    // throws otherwise; the membership stays).
                    if self.members.get(&*player) == Some(&slot) {
                        self.members.remove(&*player);
                    }
                } else {
                    // addPlayerToTeam: leaves its old team first.
                    self.members.insert((&*player).into(), slot);
                }
            }
        }
        if mode == 1 {
            // removePlayerTeam: its members have no team any more.
            self.members.retain(|_, s| *s != slot);
            if self.rules[slot as usize].is_some_and(|o| o != ALWAYS) {
                self.strict -= 1;
            }
            self.rules[slot as usize] = None;
            self.by_name.remove(&*name);
        }
        Some(())
    }

    /// `EntitySelector.pushableBy(pusher)` for the local player as the pushed entity (on the
    /// client only the local player passes the team test): the pusher's and the player's
    /// teams by scoreboard name.
    pub(crate) fn may_push(&self, pusher: &str, player: &str) -> bool {
        let (own, own_rule) = self.of(pusher);
        if own_rule == NEVER {
            return false;
        }
        let (theirs, their_rule) = self.of(player);
        if their_rule == NEVER {
            return false;
        }
        let same = own.is_some() && own == theirs;
        if (own_rule == PUSH_OWN_TEAM || their_rule == PUSH_OWN_TEAM) && same {
            return false;
        }
        (own_rule != PUSH_OTHER_TEAMS && their_rule != PUSH_OTHER_TEAMS) || same
    }
}

#[cfg(test)]
impl Teams {
    fn apply26(&mut self, body: &Bytes) -> bool {
        self.apply(body, TeamLayout::RuleBeforeAffixes)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bytes::BufMut;
    use ghast_protocol::McWrite;

    fn packet(team: &str, mode: i8, rule: Option<i32>, players: &[&str]) -> Bytes {
        let mut b = bytes::BytesMut::new();
        b.put_string(team).unwrap();
        b.put_i8(mode);
        if mode == 0 || mode == 2 {
            // Display name: an empty string tag; options, name tag visibility.
            b.put_u8(8);
            b.put_u16(0);
            b.put_u8(0);
            b.put_varint(0);
            b.put_varint(rule.unwrap_or(0));
            b.put_varint(15);
            b.put_u8(8);
            b.put_u16(0);
            b.put_u8(8);
            b.put_u16(0);
        }
        if mode == 0 || mode == 3 || mode == 4 {
            b.put_varint(players.len() as i32);
            for p in players {
                b.put_string(p).unwrap();
            }
        }
        b.freeze()
    }

    /// The vanilla predicate written out from `EntitySelector.pushableBy`, for the table test.
    fn vanilla(own: Option<(u32, u8)>, theirs: Option<(u32, u8)>) -> bool {
        let own_rule = own.map_or(ALWAYS, |t| t.1);
        if own_rule == NEVER {
            return false;
        }
        let their_rule = theirs.map_or(ALWAYS, |t| t.1);
        if their_rule == NEVER {
            return false;
        }
        let same = own.is_some() && own.map(|t| t.0) == theirs.map(|t| t.0);
        if (own_rule == PUSH_OWN_TEAM || their_rule == PUSH_OWN_TEAM) && same {
            false
        } else {
            own_rule != PUSH_OTHER_TEAMS && their_rule != PUSH_OTHER_TEAMS || same
        }
    }

    #[test]
    fn rules_follow_pushable_by() {
        // Every combination: pusher in team A / B / none, player in team A / none, any rules.
        for ra in 0..4u8 {
            for rb in 0..4u8 {
                let mut t = Teams::default();
                assert!(t.apply26(&packet("a", 0, Some(i32::from(ra)), &["me", "zombie-uuid"])));
                assert!(t.apply26(&packet("b", 0, Some(i32::from(rb)), &["cow-uuid"])));
                assert_eq!(t.any_rule(), ra != 0 || rb != 0);
                let a = Some((0, ra));
                let b = Some((1, rb));
                assert_eq!(t.may_push("zombie-uuid", "me"), vanilla(a, a), "{ra} {rb}");
                assert_eq!(t.may_push("cow-uuid", "me"), vanilla(b, a), "{ra} {rb}");
                assert_eq!(t.may_push("pig-uuid", "me"), vanilla(None, a), "{ra} {rb}");
                // The player leaves its team.
                assert!(t.apply26(&packet("a", 4, None, &["me"])));
                assert_eq!(t.may_push("zombie-uuid", "me"), vanilla(a, None));
                assert_eq!(t.may_push("cow-uuid", "me"), vanilla(b, None));
            }
        }
    }

    /// The 26.2 parameter order: display name, prefix, suffix, visibility, collision rule,
    /// optional colour, options (with and without a colour).
    #[test]
    fn layout_26_2() {
        for color in [None, Some(12)] {
            let mut b = bytes::BytesMut::new();
            b.put_string("a").unwrap();
            b.put_i8(0);
            for _ in 0..3 {
                b.put_u8(8);
                b.put_u16(0);
            }
            b.put_varint(1); // name tag visibility
            b.put_varint(i32::from(NEVER));
            match color {
                Some(c) => {
                    b.put_u8(1);
                    b.put_varint(c);
                }
                None => b.put_u8(0),
            }
            b.put_u8(3); // options
            b.put_varint(1);
            b.put_string("x").unwrap();
            let mut t = Teams::default();
            assert!(t.apply(&b.freeze(), TeamLayout::AffixesFirst), "{color:?}");
            assert_eq!(t.of("x"), (Some(0), NEVER));
        }
    }

    #[test]
    fn scoreboard_semantics() {
        let mut t = Teams::default();
        t.apply26(&packet("a", 0, Some(i32::from(NEVER)), &["x"]));
        assert!(!t.may_push("x", "me"));
        // Moving to another team leaves the first one.
        t.apply26(&packet("b", 0, Some(0), &[]));
        t.apply26(&packet("b", 3, None, &["x"]));
        assert!(t.may_push("x", "me"));
        // A remove for a team the member is not on keeps the membership.
        t.apply26(&packet("a", 4, None, &["x"]));
        assert_eq!(t.of("x"), (Some(1), ALWAYS));
        // Update info changes the rule; ADD on an existing team keeps it (and its members).
        t.apply26(&packet("b", 2, Some(i32::from(NEVER)), &[]));
        assert!(!t.may_push("x", "me"));
        t.apply26(&packet("b", 0, Some(i32::from(NEVER)), &["y"]));
        assert_eq!(t.of("x"), (Some(1), NEVER));
        // Removing a team frees its members and its slot.
        t.apply26(&packet("b", 1, None, &[]));
        assert_eq!(t.of("x"), (None, ALWAYS));
        assert!(t.may_push("y", "me"));
        assert_eq!(t.strict, 1);
        t.apply26(&packet("a", 1, None, &[]));
        assert!(!t.any_rule());
        // Unknown team, unknown rule id, a truncated body.
        assert!(!t.apply26(&packet("zzz", 3, None, &["x"])));
        t.apply26(&packet("c", 0, Some(9), &["x"]));
        assert_eq!(t.of("x"), (Some(0), ALWAYS));
        let p = packet("c", 3, None, &["y"]);
        assert!(!t.apply26(&p.slice(..p.len() - 1)));
    }
}

/// Cost probe: `GHAST_TEAMS_N` team packets (create with one member, add a member, update the
/// rule, remove) through [`Teams::apply`], counted with perfcount against `GHAST_TEAMS_N=0`.
#[cfg(test)]
#[test]
#[ignore = "cost probe"]
fn teams_cost() {
    let n: usize = std::env::var("GHAST_TEAMS_N")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(0);
    let pk = |team: &str, mode: i8, players: &[&str]| tests_packet(team, mode, Some(1), players);
    let names: Vec<String> = (0..64).map(|i| format!("team{i}")).collect();
    let packets: Vec<Bytes> = names
        .iter()
        .flat_map(|t| {
            [
                pk(t, 0, &["Steve_123"]),
                pk(t, 3, &["Alex_4567"]),
                pk(t, 2, &[]),
                pk(t, 1, &[]),
            ]
        })
        .collect();
    let mut t = Teams::default();
    let mut ok = 0;
    for i in 0..n {
        ok += usize::from(t.apply(&packets[i % packets.len()], TeamLayout::RuleBeforeAffixes));
    }
    println!("{ok}");
}

#[cfg(test)]
fn tests_packet(team: &str, mode: i8, rule: Option<i32>, players: &[&str]) -> Bytes {
    use bytes::BufMut;
    use ghast_protocol::McWrite;
    let mut b = bytes::BytesMut::new();
    b.put_string(team).unwrap();
    b.put_i8(mode);
    if mode == 0 || mode == 2 {
        // A display name component (a short text compound), then the options.
        b.put_u8(10);
        b.put_u8(8);
        b.put_u16(4);
        b.put_slice(b"text");
        b.put_u16(6);
        b.put_slice(b"Team A");
        b.put_u8(0);
        b.put_u8(0);
        b.put_varint(0);
        b.put_varint(rule.unwrap_or(0));
        b.put_varint(15);
        b.put_u8(8);
        b.put_u16(0);
        b.put_u8(8);
        b.put_u16(0);
    }
    if mode == 0 || mode == 3 || mode == 4 {
        b.put_varint(players.len() as i32);
        for p in players {
            b.put_string(p).unwrap();
        }
    }
    b.freeze()
}

/// The 1.12.2 `teams` layout: rules by name, an unknown name keeps the team's rule.
#[cfg(test)]
mod legacy_tests {
    use super::*;
    use bytes::BufMut;
    use ghast_protocol::McWrite;

    fn packet(team: &str, mode: i8, rule: &str, players: &[&str]) -> Bytes {
        let mut b = bytes::BytesMut::new();
        b.put_string(team).unwrap();
        b.put_i8(mode);
        if mode == 0 || mode == 2 {
            b.put_string(team).unwrap(); // display name
            b.put_string("").unwrap(); // prefix
            b.put_string("").unwrap(); // suffix
            b.put_u8(0); // options
            b.put_string("always").unwrap(); // name tag visibility
            b.put_string(rule).unwrap();
            b.put_i8(-1); // colour
        }
        if mode == 0 || mode == 3 || mode == 4 {
            b.put_varint(players.len() as i32);
            for p in players {
                b.put_string(p).unwrap();
            }
        }
        b.freeze()
    }

    #[test]
    fn legacy_1_12_layout() {
        let mut t = Teams::default();
        assert!(t.apply(
            &packet("a", 0, "never", &["bot", "zed"]),
            TeamLayout::Legacy1_12
        ));
        assert!(!t.may_push("zed", "bot"));
        // An unknown rule name keeps the rule (ClientPlayNetworkHandler.onTeam).
        assert!(t.apply(&packet("a", 2, "sometimes", &[]), TeamLayout::Legacy1_12));
        assert_eq!(t.of("bot").1, NEVER);
        // pushOtherTeams: only the same team pushes.
        assert!(t.apply(
            &packet("a", 2, "pushOtherTeams", &[]),
            TeamLayout::Legacy1_12
        ));
        assert!(t.may_push("zed", "bot"));
        assert!(!t.may_push("mob", "bot"));
        assert!(t.apply(&packet("a", 4, "", &["bot"]), TeamLayout::Legacy1_12));
        assert!(!t.may_push("zed", "bot"));
        assert!(t.apply(&packet("a", 1, "", &[]), TeamLayout::Legacy1_12));
        assert!(!t.any_rule() && t.may_push("zed", "bot"));
        assert_eq!(rule_by_name("pushOwnTeam"), Some(PUSH_OWN_TEAM));
        assert_eq!(rule_by_name("bogus"), None);
    }
}