summaryrefslogtreecommitdiff
path: root/src/player.rs
blob: 7b99c16ff183be2e838457fe0ee790c2abbae850 (plain)
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
use anyhow::Result;
use mcproto_rs::types::Vec3;
use mcproto_rs::v1_19_3::{Packet761, SynchronizePlayerPositionSpec};

use crate::connect::MinecraftClient;

pub struct Player {
    pub client: MinecraftClient,
    pub name: String,
    pub position: Vec3<f64>,
    pub yaw: f32,
    pub pitch: f32,
}


impl Player {
    pub fn create_teleport_packet(&self) -> Packet761 {
        Packet761::SynchronizePlayerPosition(SynchronizePlayerPositionSpec {
            position: self.position.clone(),
            yaw: self.yaw,
            pitch: self.pitch,
            flags: Default::default(),
            teleport_id: 0.into(), // TODO: teleport id ack
            dismount_vehicle: false,
        })
    }

    pub async fn teleport(&mut self, position: Vec3<f64>, yaw: f32, pitch: f32) -> Result<()> {
        self.position = position;
        self.yaw = yaw;
        self.pitch = pitch;
        self.send_packet(self.create_teleport_packet())?;
        Ok(())
    }

    pub fn send_packet(&self, packet: Packet761) -> Result<()> {
        self.client.send_packet(packet)
    }
    pub fn send_all_packets(&self, packets: Vec<Packet761>) -> Result<(), anyhow::Error> {
        self.client.send_all_packets(packets)
    }
}