ghast-client 0.5.3

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, 26.2 and 26.3)
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! Inbound packets applied to the core on arrival: `Engine::handle` (join, respawn, teleports,
//! chat, health, effects, abilities, slots, game state, attributes, statuses, configuration,
//! resource packs, rotation, dialogs) and the HUD's cold paths.

use super::*;

/// Queues a HUD event for the behaviour's next tick.
pub(super) fn push_hud(ev: &mut TickEvents, e: crate::hud::HudEvent) {
    let m = ev.more();
    if m.hud.len() < MAX_PENDING_HUD {
        m.hud.push(e);
    } else {
        m.hud_dropped += 1;
    }
}

// The HUD's paths are out of line and cold, so the packet handler and the tick stay as small
// (and as inlinable) as without the HUD when it is off and no tab completion is pending.

/// A chat message with the HUD on: an action bar message (1.8 – 1.16.5 chat position 2, 26.x
/// overlay system messages: `setOverlayMessage`) goes to the HUD; returns the message when the
/// behaviour wants chat too.
#[cold]
#[inline(never)]
fn hud_chat(c: &mut Core, m: ChatMessage) -> Option<ChatMessage> {
    if let Some(h) = &mut c.hud {
        if m.position == crate::common::ChatPosition::ActionBar {
            let t = match &m.rendered {
                Some(r) => crate::hud::Text::from_text(r.clone()),
                None => crate::hud::Text::from_json(m.json.clone()),
            };
            let ev = &mut c.pending;
            h.action_bar_op(t, |e| push_hud(ev, e));
        }
        if !c.chat_events {
            return None;
        }
    }
    Some(m)
}

/// The HUD packets (boss bars, titles, action bar, tab list) and tab-completion answers.
#[cold]
#[inline(never)]
fn hud_packet(c: &mut Core, p: Inbound) {
    match p {
        Inbound::BossBar(op) => {
            if let Some(h) = &mut c.hud {
                let ev = &mut c.pending;
                if !h.boss_bar_op(op, |e| push_hud(ev, e)) {
                    log::debug!(
                        "[{}] t{} update of an unknown boss bar",
                        c.opts.name,
                        c.tick
                    );
                }
            }
        }
        Inbound::Title(op) => {
            if let Some(h) = &mut c.hud {
                let ev = &mut c.pending;
                h.title_op(op, |e| push_hud(ev, e));
            }
        }
        Inbound::ActionBar(t) => {
            if let Some(h) = &mut c.hud {
                let ev = &mut c.pending;
                h.action_bar_op(t, |e| push_hud(ev, e));
            }
        }
        Inbound::TabList { header, footer } => {
            if let Some(h) = &mut c.hud {
                let ev = &mut c.pending;
                h.tab_list(header, footer, |e| push_hud(ev, e));
            }
        }
        Inbound::TabComplete(r) => {
            let t = c.tabs.answer(r);
            let m = c.pending.more();
            if m.tabs.len() < MAX_PENDING_TABS {
                m.tabs.push(t);
            }
        }
        _ => {}
    }
}

/// `open_sign_entity` (`onSignEditorOpen` / `handleOpenSignEditor` → `openTextEdit`).
#[cold]
#[inline(never)]
pub(super) fn open_sign(c: &mut Core, sim: &(impl Sim + ?Sized), e: crate::sign::SignEditor) {
    use crate::sign::{OpenSign, SignCloseReason, SignEvent, SignRules};
    let rules = c.opts.caps.sign_editor;
    let here = sim.sign_at(e.pos);
    if rules == SignRules::Sided && here != Some(true) {
        // 26.x: no SignBlockEntity there → "Ignoring openTextEdit on an invalid entity"
        log::debug!(
            "[{}] t{} sign editor at {:?} ignored: no sign there",
            c.opts.name,
            c.tick,
            e.pos
        );
        push_sign(c, SignEvent::Ignored(e));
        return;
    }
    if c.sign.is_some() {
        // setScreen(new): the old screen's removed() sends its lines
        close_sign_screen(c, Some(SignCloseReason::Replaced));
    }
    log::debug!(
        "[{}] t{} sign editor at {:?} (front {})",
        c.opts.name,
        c.tick,
        e.pos,
        e.front
    );
    c.sign = Some(OpenSign {
        editor: e,
        // 1.16.5: the made-up block entity reads the block there on the first tick
        check_first_tick: rules == SignRules::FirstTickCheck && here != Some(true),
    });
    c.rare = true;
    push_sign(c, SignEvent::Opened(e));
}

/// The sign screen goes away (`removed()`): its lines go out (the behaviour's, or four empty
/// ones). `reason`: the client closed it by itself (reported as an event).
pub(super) fn close_sign_screen(c: &mut Core, reason: Option<crate::sign::SignCloseReason>) {
    if let Some(s) = c.sign.take() {
        c.update_rare();
        c.out.push(Outbound::UpdateSign {
            pos: s.editor.pos,
            front: s.editor.front,
            lines: Box::default(),
        });
        if let Some(r) = reason {
            push_sign(c, crate::sign::SignEvent::Closed(s.editor, r));
        }
    }
}

fn push_sign(c: &mut Core, e: crate::sign::SignEvent) {
    let m = c.pending.more();
    if m.signs.len() < MAX_PENDING_SIGNS {
        m.signs.push(e);
    }
}

/// The sign screen's tick (`Screen.tick` after the packets, before the key handling): 1.16.5
/// closes an editor opened where the client had no sign; 26.x closes it when the sign is gone
/// or the player too far (`AbstractSignEditScreen.isValid`).
#[cold]
#[inline(never)]
pub(super) fn sign_screen_tick(c: &mut Core, sim: &(impl Sim + ?Sized)) {
    use crate::sign::{SignCloseReason, SignRules};
    let Some(s) = c.sign else { return };
    let reason = if s.check_first_tick {
        // 1.16.5 sends the editor before the block (the vanilla client has its own placement
        // prediction there): air may be a sign ghast did not predict, so only another block
        // closes the editor
        let p = s.editor.pos;
        let other =
            sim.sign_at(p) == Some(false) && sim.block_query(p.x, p.y, p.z).is_some_and(|q| !q.air);
        if let Some(o) = &mut c.sign {
            o.check_first_tick = false;
        }
        other.then_some(SignCloseReason::BlockGone)
    } else if c.opts.caps.sign_editor == SignRules::Sided {
        if sim.sign_at(s.editor.pos) != Some(true) {
            Some(SignCloseReason::BlockGone)
        } else {
            // Player.isWithinBlockInteractionRange(pos, 4.0): the block's box to the eyes
            let eye = c.pose.pos.y + pick_eye_height(sim, c.opts.caps, c.pose.sneaking);
            let p = s.editor.pos;
            let d = |v: f64, lo: i32| (f64::from(lo) - v).max(v - f64::from(lo + 1)).max(0.0);
            let (dx, dy, dz) = (d(c.pose.pos.x, p.x), d(eye, p.y), d(c.pose.pos.z, p.z));
            let max = sim.block_interaction_range() + 4.0;
            (dx * dx + dy * dy + dz * dz >= max * max).then_some(SignCloseReason::TooFar)
        }
    } else {
        None
    };
    if reason.is_some() {
        close_sign_screen(c, reason);
    }
}

/// The player is asleep in `bed` (already put on it).
#[cold]
#[inline(never)]
pub(super) fn fell_asleep(c: &mut Core, bed: ghast_protocol::BlockPos) {
    log::debug!(
        "[{}] t{} asleep in the bed at {:?}",
        c.opts.name,
        c.tick,
        bed
    );
    c.sleeping = Some(bed);
    c.rare = true;
    c.sleep_ticks = 0;
    let m = c.pending.more();
    m.sleep.push(crate::common::SleepEvent::FellAsleep(bed));
}

/// The player woke up.
#[cold]
#[inline(never)]
pub(super) fn woke_up(c: &mut Core) {
    if c.sleeping.take().is_some() {
        c.update_rare();
        log::debug!("[{}] t{} woke up", c.opts.name, c.tick);
        let m = c.pending.more();
        m.sleep.push(crate::common::SleepEvent::WokeUp);
    }
}

/// Once per tick while a sign editor is open, the player sleeps or is dead ([`Core::rare`]),
/// or when the sim saw our sleeping position change: the sign screen's tick, then the sleep
/// timer (`sleepTimer++`, at most 100) and the sim's news.
#[cold]
#[inline(never)]
pub(super) fn rare_screens_tick(
    c: &mut Core,
    sim: &(impl Sim + ?Sized),
    change: Option<Option<ghast_protocol::BlockPos>>,
) {
    if c.sign.is_some() {
        sign_screen_tick(c, sim);
    }
    if change.is_some() || c.sleeping.is_some() {
        sleep_tick(c, change);
    }
}

fn sleep_tick(c: &mut Core, change: Option<Option<ghast_protocol::BlockPos>>) {
    match change {
        Some(Some(bed)) => fell_asleep(c, bed),
        Some(None) => woke_up(c),
        None => {}
    }
    if c.sleeping.is_some() {
        c.sleep_ticks = (c.sleep_ticks + 1).min(100);
        // The sleep screen holds the mouse: the tick's look step takes the held-look path
        // (`tick::held_look`), so the plain tick tests no sleep flag there.
        c.gate_tick = true;
    }
}

/// `Gui.tick` while a title or the action bar counts down.
#[cold]
#[inline(never)]
pub(super) fn hud_countdown(h: &mut crate::hud::Hud, ev: &mut TickEvents) {
    h.tick(|e| push_hud(ev, e));
}

impl<S: Sim> Engine<S> {
    /// Applies one inbound packet (except disconnect and compression, which the connection
    /// handles).
    pub(crate) fn handle(&mut self, p: Inbound) {
        let c = &mut self.core;
        let sim = &mut self.sim;
        match p {
            Inbound::KeepAlive(id) => c.out.push(Outbound::KeepAlive(id)),
            Inbound::JoinGame(j) => {
                c.entity_id = j.entity_id;
                // a new client player: not asleep
                c.sleeping = None;
                c.update_rare();
                // LocalPlayer's main arm comes from the client option.
                sim.set_main_arm_left(c.opts.settings.main_hand == 0);
                c.game_mode = j.game_mode;
                c.respawn_screen = j.respawn_screen;
                // A join always creates a new client world (the world itself was cleared by
                // the world packet routing).
                c.dimension = j.dimension;
                c.joined = true;
                // A new client level has an empty scoreboard.
                if let Some(t) = &mut c.teams {
                    **t = crate::teams::Teams::default();
                }
                c.pending.joined = true;
                // The level's loading screen replaces any dialog.
                c.dialog = None;
                c.health = Health::default();
                c.abilities = Abilities::default();
                c.reset_player("join game");
                sim.reset_player();
                sim.spectator(c.game_mode == GameMode::Spectator);
                // ClientPlayNetworkHandler.onGameJoin: settings, then the brand (1.20.2+ sends
                // both in the configuration state instead).
                if !c.opts.caps.configuration_state {
                    c.out.push(Outbound::Settings(c.opts.settings.clone()));
                    if let Some(brand) = &c.opts.brand {
                        c.out.push(Outbound::Brand(brand.clone()));
                    }
                }
                log::info!(
                    "[{}] joined: entity {} {:?} dimension {}",
                    c.opts.name,
                    c.entity_id,
                    c.game_mode,
                    c.dimension
                );
            }
            Inbound::Respawn {
                dimension,
                game_mode,
                keep,
            } => {
                // a new client player: not asleep
                c.sleeping = None;
                c.update_rare();
                if dimension != c.dimension {
                    c.dimension = dimension;
                }
                c.game_mode = game_mode;
                c.pending.respawned = true;
                c.reset_player("respawn");
                sim.respawn_player(keep);
                sim.spectator(c.game_mode == GameMode::Spectator);
            }
            Inbound::Teleport(t) => {
                if c.teleported_once {
                    log::debug!(
                        "[{}] t{} teleport {:?} {:?} {:?} yaw {:?} pitch {:?} flags {}; we had sent \
                         {:?} {:?} {:?} yaw {:?} pitch {:?} ground {}",
                        c.opts.name,
                        c.tick,
                        t.x,
                        t.y,
                        t.z,
                        t.yaw,
                        t.pitch,
                        t.flags,
                        c.sent.pos.x,
                        c.sent.pos.y,
                        c.sent.pos.z,
                        c.sent.yaw,
                        c.sent.pitch,
                        c.sent.on_ground
                    );
                }
                let (sent_before, pose_before) = (c.sent, c.pose);
                sim.teleport(&mut c.pose, &t);
                sim.teleport_sequence(c.sequence);
                if c.pending.teleports.len() < MAX_PENDING_TELEPORTS {
                    c.pending.teleports.push(TeleportEvent {
                        teleport: t,
                        sent_before,
                        pose_before,
                        after: c.pose,
                    });
                }
                if c.opts.caps.teleport_confirm_pose {
                    // 26.3 handleMovePlayer: one accept_teleportation with the pose the player
                    // took (the server runs it through its movement checks), and
                    // `gameMode.stopDestroyBlock()`.
                    c.out.push(Outbound::TeleportConfirmPose {
                        id: t.teleport_id.unwrap_or(0),
                        pos: c.pose.pos,
                        yaw: c.pose.yaw,
                        pitch: c.pose.pitch,
                    });
                    c.hands.stop_destroy_block(&mut c.out);
                } else {
                    if let Some(id) = t.teleport_id {
                        c.out.push(Outbound::TeleportConfirm(id));
                    }
                    c.out.push(Outbound::Move(MovePacket::PositionLook {
                        horizontal_collision: false,
                        pos: c.pose.pos,
                        yaw: c.pose.yaw,
                        pitch: c.pose.pitch,
                        on_ground: false,
                    }));
                }
                // The server now has this pose; vanilla's lastReported* are not touched.
                c.sent = Pose {
                    on_ground: false,
                    ..c.pose
                };
                if !c.teleported_once {
                    log::debug!(
                        "[{}] t{} spawn position {:.3} {:.3} {:.3}",
                        c.opts.name,
                        c.tick,
                        c.pose.pos.x,
                        c.pose.pos.y,
                        c.pose.pos.z
                    );
                }
                c.teleported_once = true;
                c.pending.teleported = true;
            }
            Inbound::Chat(m) => {
                // the HUD's share is out of line (rare: only with `Interests::HUD`)
                let m = if c.hud.is_some() {
                    match hud_chat(c, m) {
                        Some(m) => m,
                        // decoded for the HUD only
                        None => return,
                    }
                } else {
                    m
                };
                if c.pending.chat.len() < MAX_PENDING_CHAT {
                    c.pending.chat.push(m);
                } else {
                    c.pending.chat_dropped += 1;
                }
            }
            p @ (Inbound::BossBar(_)
            | Inbound::Title(_)
            | Inbound::ActionBar(_)
            | Inbound::TabList { .. }
            | Inbound::TabComplete(_)) => hud_packet(c, p),
            Inbound::OpenSign(e) => open_sign(c, &*sim, e),
            Inbound::UseBed { entity_id, bed } if entity_id == c.entity_id => {
                // trySleep on the client: the player goes on the bed
                sim.sleep_at(&mut c.pose, bed);
                fell_asleep(c, bed);
            }
            Inbound::WakeUp(id) if id == c.entity_id => {
                // awaken on the client: the standing box again
                sim.wake_up();
                woke_up(c);
            }
            Inbound::UseBed { .. } | Inbound::WakeUp(_) => {}
            Inbound::Time(t) => {
                c.clocks.apply(&t);
                // the dimension's day clock (26.x: by its world clock registry id)
                c.day_clock = sim.day_clock();
            }
            Inbound::Health(h) => {
                let was_dead = c.dead;
                // The absorption comes from our entity data, not from update_health.
                c.health = Health {
                    absorption: c.health.absorption,
                    ..h
                };
                sim.food(h.food);
                // isAlive(): a dead player does not wait for its section (26.1 load gate).
                sim.player_alive(h.health > 0.0);
                c.pending.health_changed = true;
                if h.health <= 0.0 && !was_dead {
                    c.dead = true;
                    c.rare = true;
                    c.pending.died = true;
                    log::info!("[{}] t{} died", c.opts.name, c.tick);
                }
            }
            Inbound::Died { entity_id } => {
                if entity_id == c.entity_id && !c.respawn_screen {
                    // 1.16 immediate respawn: the client respawns without a death screen.
                    c.out.push(Outbound::Respawn);
                }
            }
            Inbound::Death(d) => {
                if d.entity_id == c.entity_id && !c.respawn_screen {
                    // 1.16 immediate respawn: the client respawns without a death screen.
                    c.out.push(Outbound::Respawn);
                }
                c.pending.more().deaths.push(d);
            }
            Inbound::SpawnPoint(p) => c.spawn_point = Some(p),
            Inbound::Teams(p) => {
                if let Some(t) = &mut c.teams
                    && !t.apply(&p)
                {
                    log::debug!("[{}] t{} team packet not applied", c.opts.name, c.tick);
                }
            }
            Inbound::Damage(h) => {
                if h.entity_id == c.entity_id {
                    c.last_hurt = Some(c.tick + 1);
                    c.pending.more().hurt.push(h);
                }
            }
            Inbound::EffectApplied(e) => {
                if e.entity_id == c.entity_id {
                    c.effects.insert(e.id, e.amplifier);
                    sim.status_effects(&c.effects);
                    let a = crate::common::ActiveEffect {
                        id: e.id,
                        amplifier: e.amplifier,
                        duration: e.duration,
                        flags: e.flags,
                        received_tick: c.tick + 1,
                    };
                    match c.active_effects.iter_mut().find(|x| x.id == e.id) {
                        Some(x) => *x = a,
                        None => c.active_effects.push(a),
                    }
                    c.pending
                        .more()
                        .effects
                        .push(crate::common::EffectEvent::Applied(a));
                }
            }
            Inbound::Abilities(a) => {
                c.abilities = a;
                c.pose.flying = a.flying();
                sim.abilities(a);
            }
            Inbound::HeldSlot(s) => {
                if (0..9).contains(&s) {
                    // Only the inventory's selection changes; the controller's copy does not,
                    // so the next sync echoes it (as vanilla does).
                    if c.held_slot != s as u8
                        && c.opts.caps.use_stop == crate::version::UseStop::AtSelect
                    {
                        c.hands.stop_using();
                    }
                    c.held_slot = s as u8;
                    c.slot_from_server = true;
                }
            }
            Inbound::GameState { reason, value } => match reason {
                // Weather (the 26.1 riptide reads the rain).
                1 | 2 | 7 => sim.weather(reason, value),
                3 => {
                    c.game_mode = GameMode::from_wire(value as u8);
                    sim.spectator(c.game_mode == GameMode::Spectator);
                    // 26.x ServerPlayer.setGameMode(SPECTATOR) calls stopUsingItem and the living
                    // flags clear the client's use: stopping at once stands in for that. 1.8.9 –
                    // 1.16.5 stop nothing (the use runs until release or finish).
                    if c.game_mode == GameMode::Spectator && c.opts.caps.spectator_switch_stops_use
                    {
                        c.hands.stop_using();
                    }
                    c.pending.game_mode_changed = true;
                }
                // 1.15+: "immediate respawn" gamerule (1 = no respawn screen).
                11 if c.opts.caps.immediate_respawn_rule => c.respawn_screen = value == 0.0,
                // 1.21.4+ LEVEL_CHUNKS_LOAD_START: the level's chunks are on their way.
                13 if c.opts.caps.player_loaded_gate => {
                    c.level_load_started = true;
                    sim.level_load_start();
                }
                _ => {}
            },
            // Window confirms are answered by the inventory (exactly once).
            Inbound::Transaction { .. } => {}
            Inbound::OpenWindow(id) => c.open_window = id,
            Inbound::CloseWindow(_) => c.open_window = 0,
            Inbound::Velocity { entity_id, motion } => {
                if entity_id == c.entity_id {
                    sim.velocity(motion, false);
                }
            }
            Inbound::ExplosionPush(motion) => sim.velocity(motion, true),
            Inbound::MovementSpeed {
                entity_id,
                base,
                modifiers,
            } => {
                if entity_id == c.entity_id {
                    movement_speed(c, sim, base, &modifiers);
                }
            }
            Inbound::LegacyAttributes {
                entity_id,
                movement_speed: speed,
                attack_speed,
            } => {
                if entity_id == c.entity_id {
                    if let Some((base, modifiers)) = speed {
                        movement_speed(c, sim, base, &modifiers);
                    }
                    if let Some((base, modifiers)) = attack_speed {
                        let mut m = ghast_physics::attribute::Modifiers::NONE;
                        for &(id, amount, op) in &modifiers {
                            m.insert(id.as_u128(), amount, op as u8);
                        }
                        c.attack_speed = m.value(base);
                    }
                }
            }
            Inbound::Effect {
                entity_id,
                id,
                amplifier,
            } => {
                if entity_id == c.entity_id {
                    c.effects.insert(id, amplifier);
                    sim.status_effects(&c.effects);
                }
            }
            Inbound::EffectRemoved { entity_id, id } => {
                if entity_id == c.entity_id {
                    c.effects.remove(&id);
                    sim.status_effects(&c.effects);
                    c.active_effects.retain(|x| x.id != id);
                    c.pending
                        .more()
                        .effects
                        .push(crate::common::EffectEvent::Removed(id));
                }
            }
            Inbound::EntityStatus { entity_id, status } => {
                if entity_id != c.entity_id {
                    sim.entity_event(entity_id, status);
                }
                // 9: the server finished our item use (eating, drinking).
                if entity_id == c.entity_id && status == 9 {
                    finish_food(c);
                    finish_consumes(c);
                    c.hands.finish_using();
                }
                // LivingEntity.handleStatus: 2 hurt, 33 thorns, 36 drowning, 37 burning (1.9+),
                // 44 sweet berry bush (1.14+); 26.x sends damage events instead.
                if entity_id == c.entity_id && matches!(status, 2 | 33 | 36 | 37 | 44) {
                    c.last_hurt = Some(c.tick + 1);
                    c.pending
                        .more()
                        .hurt
                        .push(crate::common::Hurt::from_status(entity_id, status));
                }
            }
            Inbound::Attributes {
                entity_id,
                attributes,
            } => {
                if entity_id == c.entity_id {
                    if let Some(a) = attributes.iter().find(|a| a.name == "attack_speed") {
                        c.attack_speed = modern_attribute_value(a);
                    }
                    sim.attributes(&attributes);
                } else {
                    sim.entity_attributes(entity_id, &attributes);
                }
            }
            Inbound::Ping(id) => c.out.push(Outbound::Pong(id)),
            Inbound::ChunkBatchStart => sim.chunk_batch_start(c.now_nanos()),
            Inbound::ChunkBatchFinished(size) => {
                let per_tick = sim.chunk_batch_finished(size, c.now_nanos());
                c.out.push(Outbound::ChunkBatchReceived(per_tick));
            }
            Inbound::StartConfiguration => {
                // Vanilla drops the level and the player until the next join.
                c.joined = false;
                // clearClientLevel → Hud.onDisconnected.
                if let Some(h) = &mut c.hud {
                    let ev = &mut c.pending;
                    h.reset(|e| push_hud(ev, e));
                }
                c.dialog = None;
                c.reset_player("reconfiguration");
                sim.drop_level();
            }
            Inbound::ResourcePackPush { id, required } => {
                // A client that never shows the prompt: optional packs are declined; required
                // ones are accepted (and reported loaded) when allowed, as the server would
                // otherwise disconnect.
                if required && c.opts.accept_required_packs {
                    for result in [3, 4, 0] {
                        c.out.push(Outbound::ResourcePackStatus { id, result });
                    }
                } else {
                    c.out.push(Outbound::ResourcePackStatus { id, result: 1 });
                }
            }
            Inbound::CookieRequest(key) => {
                // The wire layer fills in a stored value.
                c.out.push(Outbound::CookieResponse { key, value: None });
            }
            Inbound::StoreCookie { .. } => {}
            // The session driver ends the session (or follows the transfer).
            Inbound::Transfer { .. } => {}
            Inbound::Rotate {
                yaw,
                pitch,
                relative_yaw,
                relative_pitch,
            } => {
                // ClientPacketListener.handleRotatePlayer: set (or add), answer with a look.
                let yaw = if relative_yaw { c.pose.yaw + yaw } else { yaw };
                let pitch = if relative_pitch {
                    c.pose.pitch + pitch
                } else {
                    pitch
                };
                c.pose.yaw = yaw;
                c.pose.pitch = pitch.clamp(-90.0, 90.0);
                c.rotation.reset(c.pose.yaw, c.pose.pitch);
                c.out.push(Outbound::LookAnswer {
                    yaw: c.pose.yaw,
                    pitch: c.pose.pitch,
                });
                c.sent.yaw = c.pose.yaw;
                c.sent.pitch = c.pose.pitch;
            }
            Inbound::ShowDialog(d) => {
                log::debug!("[{}] t{} dialog \"{}\"", c.opts.name, c.tick, d.title);
                c.dialog = Some(d.clone());
                if c.dialogs_pending.len() < MAX_PENDING_DIALOGS {
                    c.dialogs_pending.push(crate::dialog::DialogEvent::Shown(d));
                }
            }
            Inbound::ClearDialog => {
                if c.dialog.take().is_some() && c.dialogs_pending.len() < MAX_PENDING_DIALOGS {
                    c.dialogs_pending.push(crate::dialog::DialogEvent::Cleared);
                }
            }
            Inbound::Disconnect(_) | Inbound::SetCompression(_) => {}
        }
    }
}