Skip to main content

steel_protocol/packets/game/
s_accept_teleportation.rs

1//! Serverbound accept teleportation packet - sent by client to acknowledge a teleport.
2
3use steel_macros::{ReadFrom, ServerPacket};
4
5/// Sent by the client to acknowledge a server-initiated teleport.
6///
7/// The client sends this after receiving a `CPlayerPosition` packet.
8/// The teleport ID must match the one from the `CPlayerPosition` packet.
9#[derive(ReadFrom, ServerPacket, Clone, Debug)]
10pub struct SAcceptTeleportation {
11    /// The teleport ID from the `CPlayerPosition` packet being acknowledged.
12    #[read(as = VarInt)]
13    pub teleport_id: i32,
14    /// The X position the client ended up at.
15    pub x: f64,
16    /// The Y position the client ended up at.
17    pub y: f64,
18    /// The Z position the client ended up at.
19    pub z: f64,
20    /// The yaw the client ended up at.
21    pub y_rot: f32,
22    /// The pitch the client ended up at.
23    pub x_rot: f32,
24}
25
26#[cfg(test)]
27mod tests {
28    use std::io::Cursor;
29
30    use steel_utils::serial::ReadFrom as _;
31
32    use super::*;
33
34    #[test]
35    fn accept_teleportation_reads_position_after_id() {
36        let mut bytes = vec![1]; // teleport id as a VarInt
37        for value in [1.5f64, 64.0, -2.5] {
38            bytes.extend_from_slice(&value.to_be_bytes());
39        }
40        bytes.extend_from_slice(&90.0f32.to_be_bytes());
41        bytes.extend_from_slice(&(-45.0f32).to_be_bytes());
42
43        let packet =
44            SAcceptTeleportation::read(&mut Cursor::new(&bytes[..])).expect("should decode");
45
46        assert_eq!(packet.teleport_id, 1);
47        assert_eq!((packet.x, packet.y, packet.z), (1.5, 64.0, -2.5));
48        assert_eq!((packet.y_rot, packet.x_rot), (90.0, -45.0));
49    }
50}