ghast-client 0.5.6

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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
//! 1.16.5: vehicles and mounts the player rides (boats, minecarts, living mounts): boarding,
//! their packets and their ticks.

use super::*;

impl Sim16 {
    /// A tick in the boat we control (`ghast_physics::v1_16_5::tick_boat`): the boat collides
    /// with the boxes the player collides with plus the living entities around (their boxes as
    /// the tracker holds them). On the client's own dismount (sneak) the player ticks in the
    /// same tick when it comes after the boat in the client world's order.
    #[cold]
    #[inline(never)]
    pub(super) fn ride_tick(&mut self, input: &ghast_physics::TickInput) {
        let Some(r) = boat_mut(&mut self.ride) else {
            return;
        };
        let mut boat_boxes = self.around.boxes.clone();
        if let Some(t) = &self.entities {
            let me = t.self_id();
            for e in t.iter() {
                let living = matches!(e.entity_type(), ghast_entity::EntityType::Player)
                    || e.class() == ghast_entity::EntityClass::Mob;
                // Not its own passengers (isConnectedThroughVehicle).
                if !living
                    || Some(e.id()) == me
                    || e.vehicle() == Some(r.id)
                    || t.is_spectator(e)
                    || !t.client_alive(e.id())
                {
                    continue;
                }
                if let Some(b) = t.client_box(e.id()) {
                    boat_boxes.push(aabb(b));
                }
            }
        }
        let around = ghast_physics::v1_16_5::Surroundings::new()
            .with_entities(&self.around.boxes)
            .with_others(&self.around.others);
        let drove = r.boat.controlled();
        let still = ghast_physics::v1_16_5::tick_boat(
            &mut self.player,
            &mut self.ext,
            &mut r.boat,
            &around,
            &boat_boxes,
            input,
            &self.world,
            &mut self.packets,
        );
        if !still {
            // Our own dismount: the passengers behind us tick after it and push us; the
            // client's list of the boat loses us (their seats change at once).
            let id = r.id;
            if let Some(t) = self.entities.as_mut() {
                let behind: Vec<i32> = t.get(id).map_or(Vec::new(), |e| {
                    let l = e.passengers();
                    let me = t.self_id();
                    l.iter()
                        .position(|&x| Some(x) == me)
                        .map_or(Vec::new(), |i| l[i + 1..].to_vec())
                });
                for pid in behind {
                    let Some(&(_, _, pos, b)) = self.around.riders.iter().find(|x| x.0 == pid)
                    else {
                        continue;
                    };
                    let Some(e) = t.get(pid) else { continue };
                    if !self.entity_push || t.is_spectator(e) || entity_bits(e) & PUSH_LIVING == 0 {
                        continue;
                    }
                    let pusher = ghast_physics::v1_16_5::Pusher::new(
                        pos,
                        aabb(b),
                        ghast_physics::v1_16_5::PushKind::Living,
                    );
                    ghast_physics::v1_16_5::push_by(
                        &mut self.player,
                        self.filed_pos,
                        &self.world,
                        &pusher,
                    );
                }
                t.self_dismounted(id);
            }
        }
        if drove {
            // The other passengers sit where our move left the boat.
            let (id, b) = (r.id, r.boat.bb);
            if let Some(t) = self.entities.as_mut() {
                t.client_vehicle_moved(
                    id,
                    r.boat.pos,
                    [b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z],
                    r.boat.yaw,
                    r.boat.tracked(),
                );
            }
        }
        if !still {
            let id = r.id;
            log::debug!("got off boat {id} (sneak)");
            let after = self
                .entities
                .as_ref()
                .is_some_and(|t| t.ticks_before_self(id));
            let bb = r.boat.bb;
            self.park(PARK_IN_TICK);
            if after {
                let mut boxes = self.around.boxes.clone();
                boxes.push(bb);
                let around = ghast_physics::v1_16_5::Surroundings::new()
                    .with_entities(&boxes)
                    .with_others(&self.around.others);
                let mut ti = *input;
                ti.yaw = self.player.yaw;
                ti.pitch = self.player.pitch;
                ghast_physics::v1_16_5::tick_ext(
                    &mut self.player,
                    &mut self.ext,
                    &around,
                    &ti,
                    &self.world,
                    &mut self.packets,
                );
            }
        }
    }

    /// The server's moves of the minecart we sit in (`onEntityPosition` / `onEntityUpdate`), and
    /// its removal (`ClientWorld.removeEntity` takes its passengers off).
    #[cold]
    #[inline(never)]
    pub(super) fn cart_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
        use ghast_protocol::v1_16_5::play::clientbound as cb;
        let Some(c) = cart_mut(&mut self.ride) else {
            return Ok(());
        };
        let m = &mut c.cart;
        match raw.id {
            cb::id::ENTITY_TELEPORT => {
                let p: cb::EntityTeleport = raw.decode_as()?;
                if p.entity_id == c.id {
                    m.server_teleport([p.x, p.y, p.z], p.yaw, p.pitch);
                }
            }
            cb::id::REL_ENTITY_MOVE => {
                let p: cb::RelEntityMove = raw.decode_as()?;
                if p.entity_id == c.id {
                    m.server_update(Some([p.d_x, p.d_y, p.d_z]), None);
                }
            }
            cb::id::ENTITY_MOVE_LOOK => {
                let p: cb::EntityMoveLook = raw.decode_as()?;
                if p.entity_id == c.id {
                    m.server_update(Some([p.d_x, p.d_y, p.d_z]), Some((p.yaw, p.pitch)));
                }
            }
            cb::id::ENTITY_LOOK => {
                let p: cb::EntityLook = raw.decode_as()?;
                if p.entity_id == c.id {
                    m.server_update(None, Some((p.yaw, p.pitch)));
                }
            }
            cb::id::ENTITY_DESTROY => {
                let p: cb::EntityDestroy = raw.decode_as()?;
                if p.entity_ids.contains(&c.id) {
                    self.ride = None;
                    self.update_slow();
                }
            }
            _ => {}
        }
        Ok(())
    }

    /// A tick in the minecart we sit in (`ghast_physics::v1_16_5::tick_minecart`). On the
    /// client's own dismount (sneak) the player ticks in the same tick when it comes after the
    /// cart in the client world's order.
    #[cold]
    #[inline(never)]
    pub(super) fn cart_tick(&mut self, input: &ghast_physics::TickInput) {
        let Some(c) = cart_mut(&mut self.ride) else {
            return;
        };
        let around = ghast_physics::v1_16_5::Surroundings::new()
            .with_entities(&self.around.boxes)
            .with_others(&self.around.others);
        let still = ghast_physics::v1_16_5::tick_minecart(
            &mut self.player,
            &mut self.ext,
            &mut c.cart,
            &around,
            input,
            &self.world,
            &mut self.packets,
        );
        if !still {
            let id = c.id;
            log::debug!("got off minecart {id} (sneak)");
            self.ride = None;
            let after = self
                .entities
                .as_ref()
                .is_some_and(|t| t.ticks_before_self(id));
            if after {
                ghast_physics::v1_16_5::tick_ext(
                    &mut self.player,
                    &mut self.ext,
                    &around,
                    input,
                    &self.world,
                    &mut self.packets,
                );
            }
        }
    }

    /// `set_passengers` naming us on a living mount the tracker knows: the mount from its client
    /// position, rotation, entity data and attributes. `false` for other entities.
    fn board_mount(&mut self, id: i32) -> bool {
        let Some(t) = self.entities.as_ref() else {
            return false;
        };
        let Some(e) = t.get(id) else {
            return false;
        };
        let Some(kind) = mount_kind(e) else {
            return false;
        };
        let shadow = self
            .mounts
            .seen
            .iter_mut()
            .find(|a| a.id == id)
            .and_then(|a| a.shadow.take())
            .filter(|m| m.kind == kind);
        let mut m = match shadow {
            // What the client held while it watched the mob: its interpolation, velocity, body,
            // walk animation, rearing.
            Some(mut m) => {
                m.board();
                m
            }
            None => {
                log::debug!("boarding {kind:?} {id} without its watched state (spawn not seen)");
                let pos = t.client_position(id).unwrap_or(e.position());
                let mut m =
                    ghast_physics::v1_16_5::Mount::new(kind, pos, e.yaw(), e.pitch(), e.head_yaw());
                m.tracked = e.position();
                if let Some(seen) = self.mounts.seen.iter().find(|a| a.id == id) {
                    m.speed_attribute = seen.speed;
                    m.jump_attribute = seen.jump;
                    m.saddled = seen.saddled;
                    if let Some(time) = seen.boost {
                        m.boost(time);
                    }
                }
                m
            }
        };
        m.rider_holds_stick = self.mounts.holds_stick(kind);
        // Entity.startRiding: the standing pose.
        let pl = &mut self.player;
        pl.pose = ghast_physics::v1_16_5::Pose::Standing;
        let [x, y, z] = pl.pos;
        pl.set_position(x, y, z);
        log::debug!(
            "riding {kind:?} {id} at {:?} (server {:?}, {} interpolation steps left), velocity {:?}, \
             on ground {}",
            m.pos,
            m.tracked,
            m.interpolation_steps(),
            m.vel,
            m.on_ground
        );
        self.parked = None;
        self.ride = Some(Box::new(Ride16::Mount(Mount16 { id, mount: m })));
        self.update_slow();
        true
    }

    /// Attributes, entity data and velocity packets of mount-type entities: what they say is kept
    /// for a later ride ([`MountSeen16`]); the mount we ride takes it at once.
    #[cold]
    #[inline(never)]
    pub(super) fn mount_data_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
        use ghast_physics::v1_16_5::MountKind;
        use ghast_protocol::defs::p754::types::EntityMetadataValue as V;
        use ghast_protocol::v1_16_5::play::clientbound as cb;
        let id = ghast_protocol::Reader::new(&raw.body).read_varint()?;
        let Some(t) = self.entities.as_ref() else {
            return Ok(());
        };
        let Some(kind) = t.get(id).and_then(mount_kind) else {
            return Ok(());
        };
        let riding = matches!(self.ride.as_deref(), Some(Ride16::Mount(m)) if m.id == id);
        if raw.id == cb::id::ENTITY_VELOCITY {
            let p: cb::EntityVelocity = raw.decode_as()?;
            let (x, y, z) = (p.velocity.x, p.velocity.y, p.velocity.z);
            if riding {
                if let Some(m) = mount_mut(&mut self.ride) {
                    m.mount.server_velocity(x, y, z);
                }
            } else if let Some(m) = self
                .mounts
                .seen
                .iter_mut()
                .find(|a| a.id == id)
                .and_then(|a| a.shadow.as_mut())
            {
                m.server_velocity(x, y, z);
            }
            return Ok(());
        }
        let i = match self.mounts.seen.iter().position(|a| a.id == id) {
            Some(i) => i,
            None => {
                // Forget the entities the tracker no longer has.
                self.mounts.seen.retain(|a| t.get(a.id).is_some());
                self.mounts.seen.push(MountSeen16 {
                    id,
                    speed: kind.default_speed(),
                    jump: kind.default_jump(),
                    saddled: false,
                    boost: None,
                    shadow: None,
                });
                self.mounts.seen.len() - 1
            }
        };
        let seen = &mut self.mounts.seen[i];
        let mut boosted = false;
        let mut horse_flags = None;
        if raw.id == cb::id::ENTITY_UPDATE_ATTRIBUTES {
            let p: cb::EntityUpdateAttributes = raw.decode_as()?;
            for a in &p.properties {
                let mut mods = ghast_physics::attribute::Modifiers::default();
                for m in &a.modifiers {
                    mods.insert(m.uuid.as_u128(), m.amount, m.operation as u8);
                }
                match &*a.key {
                    "minecraft:generic.movement_speed" => seen.speed = mods.value(a.value),
                    "minecraft:horse.jump_strength" => seen.jump = mods.value(a.value),
                    _ => {}
                }
            }
        } else {
            // Pig: saddle 16, boost time 17; strider: boost time 16, saddle 18; the horse
            // family: horse flags 16 (bit 4: saddled).
            let (saddle, boost) = match kind {
                MountKind::Pig => (16, 17),
                MountKind::Strider => (18, 16),
                _ => (16, u8::MAX),
            };
            let p: cb::EntityMetadata = raw.decode_as()?;
            for e in &p.metadata {
                match (&e.value, kind.is_horse_family()) {
                    (V::Byte(b), true) if e.key == saddle => {
                        seen.saddled = b & 4 != 0;
                        horse_flags = Some(*b as u8);
                    }
                    (V::Boolean(b), false) if e.key == saddle => seen.saddled = *b,
                    // onTrackedDataSet(BOOST_TIME): every entry starts a boost.
                    (V::VarInt(v), false) if e.key == boost => {
                        seen.boost = Some(*v);
                        boosted = true;
                    }
                    _ => {}
                }
            }
        }
        let (speed, jump, saddled, boost) = (seen.speed, seen.jump, seen.saddled, seen.boost);
        let target = if riding {
            mount_mut(&mut self.ride).map(|m| &mut m.mount)
        } else {
            self.mounts.seen[i].shadow.as_mut()
        };
        if let Some(m) = target {
            m.speed_attribute = speed;
            m.jump_attribute = jump;
            m.saddled = saddled;
            if let Some(f) = horse_flags {
                // The horse flags: the saddle and the rearing bit (32).
                m.server_horse_flags(f);
            }
            if boosted {
                m.boost(boost.unwrap_or(0));
            }
        }
        Ok(())
    }

    /// The server's moves of the living mount we ride (followed only when the server moves it)
    /// and its removal.
    #[cold]
    #[inline(never)]
    pub(super) fn mount_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
        use ghast_protocol::v1_16_5::play::clientbound as cb;
        let Some(r) = mount_mut(&mut self.ride) else {
            return Ok(());
        };
        let m = &mut r.mount;
        match raw.id {
            cb::id::ENTITY_TELEPORT => {
                let p: cb::EntityTeleport = raw.decode_as()?;
                if p.entity_id == r.id {
                    m.server_teleport([p.x, p.y, p.z], p.yaw, p.pitch, p.on_ground);
                }
            }
            cb::id::REL_ENTITY_MOVE => {
                let p: cb::RelEntityMove = raw.decode_as()?;
                if p.entity_id == r.id {
                    m.server_update(Some([p.d_x, p.d_y, p.d_z]), None, p.on_ground);
                }
            }
            cb::id::ENTITY_MOVE_LOOK => {
                let p: cb::EntityMoveLook = raw.decode_as()?;
                if p.entity_id == r.id {
                    m.server_update(
                        Some([p.d_x, p.d_y, p.d_z]),
                        Some((p.yaw, p.pitch)),
                        p.on_ground,
                    );
                }
            }
            cb::id::ENTITY_LOOK => {
                let p: cb::EntityLook = raw.decode_as()?;
                if p.entity_id == r.id {
                    m.server_update(None, Some((p.yaw, p.pitch)), p.on_ground);
                }
            }
            cb::id::ENTITY_HEAD_ROTATION => {
                let p: cb::EntityHeadRotation = raw.decode_as()?;
                if p.entity_id == r.id {
                    m.server_head_yaw(p.head_yaw);
                }
            }
            cb::id::ENTITY_DESTROY => {
                let p: cb::EntityDestroy = raw.decode_as()?;
                if p.entity_ids.contains(&r.id) {
                    self.ride = None;
                    self.update_slow();
                }
            }
            _ => {}
        }
        Ok(())
    }

    /// A mob spawn of a mount type: the mob as the client watches it from now on (its spawn
    /// position, angle bytes and velocity), replacing what an earlier entity of that id left.
    #[cold]
    #[inline(never)]
    pub(super) fn mount_spawn(&mut self, raw: &RawPacket) -> Result<(), SimError> {
        use ghast_physics::v1_16_5::Mount;
        use ghast_protocol::v1_16_5::play::clientbound as cb;
        let p: cb::SpawnEntityLiving = raw.decode_as()?;
        let Some(t) = self.entities.as_ref() else {
            return Ok(());
        };
        let id = p.entity_id;
        self.mounts
            .seen
            .retain(|a| a.id != id && t.get(a.id).is_some());
        let Some(kind) = t.get(id).and_then(mount_kind) else {
            return Ok(());
        };
        // onMobSpawn: `byte * 360 / 256.0F`; body and head from the head byte.
        let deg = |b: i8| f32::from(b) * 360.0 / 256.0;
        let mut m = Mount::watched(
            kind,
            [p.x, p.y, p.z],
            deg(p.yaw),
            deg(p.pitch),
            deg(p.head_pitch),
        );
        m.spawn_velocity(p.velocity.x, p.velocity.y, p.velocity.z);
        self.mounts.seen.push(MountSeen16 {
            id,
            speed: kind.default_speed(),
            jump: kind.default_jump(),
            saddled: false,
            boost: None,
            shadow: Some(m),
        });
        Ok(())
    }

    /// The server's moves, looks and head turns of the mounts the client watches (not the one we
    /// ride: [`Sim16::mount_packet`]), and their removal.
    #[cold]
    #[inline(never)]
    pub(super) fn watched_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
        use ghast_protocol::v1_16_5::play::clientbound as cb;
        let ridden = self.ride.as_ref().map(|r| r.id());
        if raw.id == cb::id::ENTITY_DESTROY {
            let p: cb::EntityDestroy = raw.decode_as()?;
            self.mounts.seen.retain(|a| !p.entity_ids.contains(&a.id));
            return Ok(());
        }
        let id = ghast_protocol::Reader::new(&raw.body).read_varint()?;
        if ridden == Some(id) {
            return Ok(());
        }
        let Some(m) = self
            .mounts
            .seen
            .iter_mut()
            .find(|a| a.id == id)
            .and_then(|a| a.shadow.as_mut())
        else {
            return Ok(());
        };
        match raw.id {
            cb::id::ENTITY_TELEPORT => {
                let p: cb::EntityTeleport = raw.decode_as()?;
                m.server_teleport([p.x, p.y, p.z], p.yaw, p.pitch, p.on_ground);
            }
            cb::id::REL_ENTITY_MOVE => {
                let p: cb::RelEntityMove = raw.decode_as()?;
                m.server_update(Some([p.d_x, p.d_y, p.d_z]), None, p.on_ground);
            }
            cb::id::ENTITY_MOVE_LOOK => {
                let p: cb::EntityMoveLook = raw.decode_as()?;
                m.server_update(
                    Some([p.d_x, p.d_y, p.d_z]),
                    Some((p.yaw, p.pitch)),
                    p.on_ground,
                );
            }
            cb::id::ENTITY_LOOK => {
                let p: cb::EntityLook = raw.decode_as()?;
                m.server_update(None, Some((p.yaw, p.pitch)), p.on_ground);
            }
            cb::id::ENTITY_HEAD_ROTATION => {
                let p: cb::EntityHeadRotation = raw.decode_as()?;
                m.server_head_yaw(p.head_yaw);
            }
            _ => {}
        }
        Ok(())
    }

    /// `onPlayerPositionLook`: a riding player leaves its vehicle before the teleport
    /// (`method_29239`; the server's set-passengers comes later).
    #[cold]
    #[inline(never)]
    pub(super) fn teleport_off(&mut self) {
        let Some(id) = self.ride.as_ref().map(|r| r.id()) else {
            return;
        };
        log::debug!("teleported off vehicle {id}");
        if let Some(t) = self.entities.as_mut() {
            t.self_dismounted(id);
        }
        if matches!(self.ride.as_deref(), Some(Ride16::Boat(_))) {
            self.park(PARK_BETWEEN_TICKS);
        } else {
            unmount(&mut self.ride, &mut self.mounts);
            self.ride = None;
        }
        self.update_slow();
    }

    /// The server's moves of the boat we ride (followed only while another passenger sits in
    /// front: the client then does not move it).
    #[cold]
    #[inline(never)]
    pub(super) fn boat_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
        use ghast_protocol::v1_16_5::play::clientbound as cb;
        let Some(r) = boat_mut(&mut self.ride) else {
            return Ok(());
        };
        let b = &mut r.boat;
        match raw.id {
            cb::id::ENTITY_TELEPORT => {
                let p: cb::EntityTeleport = raw.decode_as()?;
                if p.entity_id == r.id {
                    b.server_teleport([p.x, p.y, p.z], p.yaw, p.pitch, p.on_ground);
                }
            }
            cb::id::REL_ENTITY_MOVE => {
                let p: cb::RelEntityMove = raw.decode_as()?;
                if p.entity_id == r.id {
                    b.server_update(Some([p.d_x, p.d_y, p.d_z]), None, p.on_ground);
                }
            }
            cb::id::ENTITY_MOVE_LOOK => {
                let p: cb::EntityMoveLook = raw.decode_as()?;
                if p.entity_id == r.id {
                    b.server_update(
                        Some([p.d_x, p.d_y, p.d_z]),
                        Some((p.yaw, p.pitch)),
                        p.on_ground,
                    );
                }
            }
            cb::id::ENTITY_LOOK => {
                let p: cb::EntityLook = raw.decode_as()?;
                if p.entity_id == r.id {
                    b.server_update(None, Some((p.yaw, p.pitch)), p.on_ground);
                }
            }
            _ => {}
        }
        Ok(())
    }

    /// A tick on the living mount we ride (`ghast_physics::v1_16_5::tick_mount`). On the client's
    /// own dismount (sneak) the player ticks in the same tick when it comes after the mount in
    /// the client world's order.
    #[cold]
    #[inline(never)]
    pub(super) fn mount_tick(&mut self, input: &ghast_physics::TickInput) {
        let Some(r) = mount_mut(&mut self.ride) else {
            return;
        };
        let around = ghast_physics::v1_16_5::Surroundings::new()
            .with_entities(&self.around.boxes)
            .with_others(&self.around.others);
        let still = ghast_physics::v1_16_5::tick_mount(
            &mut self.player,
            &mut self.ext,
            &mut r.mount,
            &around,
            input,
            &self.world,
            &mut self.packets,
        );
        let id = r.id;
        // The tracker's client view of the mount is ours.
        if let Some(t) = self.entities.as_mut() {
            mount_view(t, id, &r.mount);
        }
        if !still {
            log::debug!("got off mount {id} (sneak)");
            unmount(&mut self.ride, &mut self.mounts);
            self.mounts.left = Some(id);
            // stopRiding: the client's copy of the mount no longer lists us (the server's
            // set-passengers comes later), so it follows the server's moves again.
            if let Some(t) = self.entities.as_mut() {
                t.self_dismounted(id);
            }
            let after = self
                .entities
                .as_ref()
                .is_some_and(|t| t.ticks_before_self(id));
            if after {
                ghast_physics::v1_16_5::tick_ext(
                    &mut self.player,
                    &mut self.ext,
                    &around,
                    input,
                    &self.world,
                    &mut self.packets,
                );
            }
        }
    }

    /// The player left the boat it drove: the boat stays where it is on the client.
    fn park(&mut self, in_tick: i64) {
        if let Some(Ride16::Boat(r)) = self.ride.take().map(|b| *b) {
            let b = r.boat.bb;
            self.parked = Some(Parked16 {
                id: r.id,
                pos: r.boat.pos,
                bb: [b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z],
                tracked: self.entities.as_ref().and_then(|t| t.client_position(r.id)),
            });
            let mut boat = r.boat;
            boat.leave();
            self.left_boat = Some(Box::new(LeftBoat16 {
                id: r.id,
                tick: self.piston_time.wrapping_add(in_tick),
                boat,
            }));
        }
    }

    /// `set_passengers` (boarding / leaving a boat) and `vehicle_move` (the server's correction
    /// of the boat we control).
    #[cold]
    #[inline(never)]
    pub(super) fn vehicle_packet(&mut self, raw: &RawPacket) -> Result<(), SimError> {
        use ghast_protocol::v1_16_5::play::clientbound as cb;
        if raw.id == cb::id::VEHICLE_MOVE {
            if let Some(m) = mount_mut(&mut self.ride) {
                let p: cb::VehicleMove = raw.decode_as()?;
                if let Some(ghast_physics::Packet::MoveVehicle {
                    pos,
                    yaw,
                    pitch,
                    on_ground,
                }) = m.mount.server_move([p.x, p.y, p.z], p.yaw, p.pitch)
                {
                    self.reply = Some(Outbound::VehicleMove {
                        pos: Vec3::new(pos[0], pos[1], pos[2]),
                        yaw,
                        pitch,
                        on_ground,
                    });
                    if let Some(r) = &mut self.rec {
                        r.vehicle_reply(pos, yaw, pitch, on_ground);
                    }
                    // The client view's copy of it moved with it.
                    let (id, b) = (m.id, m.mount.bb);
                    if let Some(t) = self.entities.as_mut() {
                        t.client_pushed(
                            id,
                            pos,
                            [b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z],
                            on_ground,
                        );
                    }
                }
                return Ok(());
            }
            if let Some(r) = boat_mut(&mut self.ride)
                && r.boat.controlled()
            {
                let p: cb::VehicleMove = raw.decode_as()?;
                if let ghast_physics::Packet::MoveVehicle {
                    pos,
                    yaw,
                    pitch,
                    on_ground,
                } = r.boat.server_move([p.x, p.y, p.z], p.yaw, p.pitch)
                {
                    self.reply = Some(Outbound::VehicleMove {
                        pos: Vec3::new(pos[0], pos[1], pos[2]),
                        yaw,
                        pitch,
                        on_ground,
                    });
                    // The client view's copy of it moved with it.
                    let (id, b) = (r.id, r.boat.bb);
                    if let Some(t) = self.entities.as_mut() {
                        t.client_pushed(
                            id,
                            pos,
                            [b.min_x, b.min_y, b.min_z, b.max_x, b.max_y, b.max_z],
                            on_ground,
                        );
                    }
                }
            }
            return Ok(());
        }
        let p: cb::SetPassengers = raw.decode_as()?;
        let me = self.rt.own_id;
        let on = p.passengers.contains(&me);
        let seat = p.passengers.iter().position(|&v| v == me);
        if on {
            // Entity.startRiding (every listing starts the ride again): the standing pose.
            self.player.start_riding();
        }
        let riding_it =
            matches!(self.ride.as_deref(), Some(Ride16::Boat(r)) if r.id == p.entity_id);
        let in_cart = matches!(self.ride.as_deref(), Some(Ride16::Cart(c)) if c.id == p.entity_id);
        let on_mount =
            matches!(self.ride.as_deref(), Some(Ride16::Mount(m)) if m.id == p.entity_id);
        if self.mounts.left == Some(p.entity_id) {
            self.mounts.left = None;
        }
        if !on && on_mount {
            log::debug!("left mount {}", p.entity_id);
            unmount(&mut self.ride, &mut self.mounts);
            self.update_slow();
            return Ok(());
        }
        if on && !on_mount && !riding_it && !in_cart && self.board_mount(p.entity_id) {
            return Ok(());
        }
        if on && !riding_it && !in_cart {
            // A minecart: where the server has it (the client's interpolation of a cart at rest
            // ends there), the rotation it last sent.
            let cart = self.entities.as_ref().and_then(|t| {
                let e = t.get(p.entity_id)?;
                (ghast_entity::v1_16_5::info(e.entity_type())?.name == "minecart").then(|| {
                    ghast_physics::v1_16_5::Minecart::new(e.position(), e.yaw(), e.pitch())
                })
            });
            if let Some(cart) = cart {
                let pl = &mut self.player;
                pl.pose = ghast_physics::v1_16_5::Pose::Standing;
                let [x, y, z] = pl.pos;
                pl.set_position(x, y, z);
                log::debug!("in minecart {}", p.entity_id);
                self.parked = None;
                self.ride = Some(Box::new(Ride16::Cart(Cart16 {
                    id: p.entity_id,
                    cart,
                })));
                self.update_slow();
                return Ok(());
            }
        }
        if !on && in_cart {
            log::debug!("left minecart {}", p.entity_id);
            self.ride = None;
            self.update_slow();
            return Ok(());
        }
        if on && riding_it {
            // The passengers changed: our seat, and whether we still drive it.
            if let (Some(r), Some(i)) = (boat_mut(&mut self.ride), seat) {
                r.boat.set_seat(i, p.passengers.len());
            }
        } else if on {
            // startRiding a boat the client knows: its client position, rotation and
            // interpolation; a boat we drove before keeps its turn. Other vehicles: not
            // simulated (the walking tick goes on).
            let parked = self.parked.filter(|k| k.id == p.entity_id);
            let left = self.left_boat.take().filter(|l| l.id == p.entity_id);
            let now = self.piston_time;
            let pushes = &self.around.boat_pushes;
            let boat = self.entities.as_ref().and_then(|t| {
                let e = t.get(p.entity_id)?;
                let is_boat = matches!(e.entity_type(), ghast_entity::EntityType::Registry(6));
                if !is_boat {
                    return None;
                }
                let mut b = match left {
                    // Left and boarded again with no tick between: as it was.
                    Some(l) if l.tick == now => l.boat,
                    _ => {
                        let (pos, bb) = view(t, parked, p.entity_id);
                        let pos = pos.unwrap_or(e.position());
                        let lerp = t.client_interpolation(p.entity_id);
                        let rot = lerp.map_or([e.yaw(), 0.0], |l| l.rot);
                        let mut b = ghast_physics::v1_16_5::Boat::new(pos, rot[0], rot[1]);
                        // Its box as the client holds it (moved as a whole by its last move).
                        if let Some(bb) = bb {
                            b.bb = aabb(bb);
                        }
                        // Its server position (ours while we drove it, see
                        // `client_vehicle_moved`) and its interpolation.
                        let tracked = e.position();
                        match lerp {
                            Some(l) => b.set_interpolation(
                                l.target,
                                l.target_rot[0],
                                l.target_rot[1],
                                i32::from(l.steps),
                                tracked,
                            ),
                            None => b.set_interpolation(pos, rot[0], rot[1], 0, tracked),
                        }
                        if let Some(l) = left {
                            b.set_turn(l.boat.yaw_velocity(), l.boat.paddles());
                        }
                        // Its last tick pushed us (an empty boat takes the opposite push).
                        if let Some(&(_, [dx, dz])) = pushes.iter().find(|q| q.0 == pos) {
                            b.vel = [0.0 + dx, 0.0, 0.0 + dz];
                        }
                        b
                    }
                };
                b.set_seat(seat.unwrap_or(0), p.passengers.len());
                Some(b)
            });
            match boat {
                Some(boat) => {
                    log::debug!("riding boat {}", p.entity_id);
                    self.parked = None;
                    self.ride = Some(Box::new(Ride16::Boat(BoatRide16 {
                        id: p.entity_id,
                        boat,
                    })));
                }
                None => log::debug!(
                    "riding {} (not simulated: no entity tracking or not a boat)",
                    p.entity_id
                ),
            }
        } else if !on && riding_it {
            log::debug!("left boat {}", p.entity_id);
            self.park(PARK_BETWEEN_TICKS);
        }
        self.update_slow();
        Ok(())
    }
}