Skip to main content

steel_protocol/packets/game/
c_teleport_entity.rs

1use glam::DVec3;
2use steel_macros::{ClientPacket, WriteTo};
3use steel_registry::packets::play::C_TELEPORT_ENTITY;
4
5use super::c_player_position::RelativeMovement;
6
7/// Teleports an entity with optional relative position, rotation, and velocity flags.
8#[derive(ClientPacket, WriteTo, Clone, Debug)]
9#[packet_id(Play = C_TELEPORT_ENTITY)]
10pub struct CTeleportEntity {
11    #[write(as = VarInt)]
12    pub entity_id: i32,
13    pub pos: DVec3,
14    pub vel: DVec3,
15    pub yaw: f32,
16    pub pitch: f32,
17    pub relatives: RelativeMovement,
18    pub on_ground: bool,
19}
20
21impl CTeleportEntity {
22    #[must_use]
23    pub const fn new(
24        entity_id: i32,
25        pos: DVec3,
26        vel: DVec3,
27        yaw: f32,
28        pitch: f32,
29        relatives: RelativeMovement,
30        on_ground: bool,
31    ) -> Self {
32        Self {
33            entity_id,
34            pos,
35            vel,
36            yaw,
37            pitch,
38            relatives,
39            on_ground,
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use glam::DVec3;
47
48    use super::{CTeleportEntity, RelativeMovement};
49
50    #[test]
51    fn teleport_entity_preserves_relative_flags_and_motion() {
52        let packet = CTeleportEntity::new(
53            42,
54            DVec3::new(1.0, 2.0, 3.0),
55            DVec3::new(0.1, 0.2, 0.3),
56            90.0,
57            -15.0,
58            RelativeMovement::DELTA.union(RelativeMovement::ROTATION),
59            true,
60        );
61
62        assert_eq!(packet.entity_id, 42);
63        assert_eq!(packet.pos, DVec3::new(1.0, 2.0, 3.0));
64        assert_eq!(packet.vel, DVec3::new(0.1, 0.2, 0.3));
65        assert_eq!(packet.yaw, 90.0);
66        assert_eq!(packet.pitch, -15.0);
67        assert_eq!(
68            packet.relatives,
69            RelativeMovement::DELTA.union(RelativeMovement::ROTATION)
70        );
71        assert!(packet.on_ground);
72    }
73}