Skip to content

Commit

Permalink
Use Entity teleport Packet instead of Player
Browse files Browse the repository at this point in the history
Should make fixing #363 easier
  • Loading branch information
Snowiiii committed Dec 7, 2024
1 parent a04b23f commit b2f627d
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 90 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ thiserror = "2"
num-traits = "0.2"
num-derive = "0.4"

bytes = "1.8"
bytes = "1.9"

# Concurrency/Parallelism and Synchronization
rayon = "1.10.0"
Expand Down
3 changes: 1 addition & 2 deletions pumpkin-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ log.workspace = true
tokio.workspace = true
num-traits.workspace = true
num-derive.workspace = true

bytes = "1.8"
bytes.workspace = true

# encryption
aes = "0.8.4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ use pumpkin_macros::client_packet;
use crate::{ClientPacket, PositionFlag, VarInt};

#[client_packet("play:player_position")]
pub struct CSyncPlayerPosition<'a> {
pub struct CPlayerPosition<'a> {
teleport_id: VarInt,
x: f64,
y: f64,
z: f64,
delta_x: f64,
delta_y: f64,
delta_z: f64,
position: Vector3<f64>,
delta: Vector3<f64>,
yaw: f32,
pitch: f32,
releatives: &'a [PositionFlag],
}

impl<'a> CSyncPlayerPosition<'a> {
impl<'a> CPlayerPosition<'a> {
pub fn new(
teleport_id: VarInt,
position: Vector3<f64>,
Expand All @@ -28,28 +24,24 @@ impl<'a> CSyncPlayerPosition<'a> {
) -> Self {
Self {
teleport_id,
x: position.x,
y: position.y,
z: position.z,
delta_x: delta.x,
delta_y: delta.y,
delta_z: delta.z,
position,
delta,
yaw,
pitch,
releatives,
}
}
}

impl ClientPacket for CSyncPlayerPosition<'_> {
impl ClientPacket for CPlayerPosition<'_> {
fn write(&self, bytebuf: &mut crate::bytebuf::ByteBuffer) {
bytebuf.put_var_int(&self.teleport_id);
bytebuf.put_f64(self.x);
bytebuf.put_f64(self.y);
bytebuf.put_f64(self.z);
bytebuf.put_f64(self.delta_x);
bytebuf.put_f64(self.delta_y);
bytebuf.put_f64(self.delta_z);
bytebuf.put_f64(self.position.x);
bytebuf.put_f64(self.position.y);
bytebuf.put_f64(self.position.z);
bytebuf.put_f64(self.delta.x);
bytebuf.put_f64(self.delta.y);
bytebuf.put_f64(self.delta.z);
bytebuf.put_f32(self.yaw);
bytebuf.put_f32(self.pitch);
// not sure about that
Expand Down
52 changes: 34 additions & 18 deletions pumpkin-protocol/src/client/play/c_teleport_entity.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
use pumpkin_core::math::vector3::Vector3;
use pumpkin_macros::client_packet;
use serde::Serialize;

use crate::VarInt;
use crate::{ClientPacket, PositionFlag, VarInt};

#[derive(Serialize)]
#[client_packet("play:teleport_entity")]
pub struct CTeleportEntitiy {
pub struct CTeleportEntitiy<'a> {
entity_id: VarInt,
x: f64,
y: f64,
z: f64,
yaw: u8,
pitch: u8,
position: Vector3<f64>,
delta: Vector3<f64>,
yaw: f32,
pitch: f32,
releatives: &'a [PositionFlag],
on_ground: bool,
}

impl CTeleportEntitiy {
impl<'a> CTeleportEntitiy<'a> {
pub fn new(
entity_id: VarInt,
x: f64,
y: f64,
z: f64,
yaw: u8,
pitch: u8,
position: Vector3<f64>,
delta: Vector3<f64>,
yaw: f32,
pitch: f32,
releatives: &'a [PositionFlag],
on_ground: bool,
) -> Self {
Self {
entity_id,
x,
y,
z,
position,
delta,
yaw,
pitch,
releatives,
on_ground,
}
}
}

impl ClientPacket for CTeleportEntitiy<'_> {
fn write(&self, bytebuf: &mut crate::bytebuf::ByteBuffer) {
bytebuf.put_var_int(&self.entity_id);
bytebuf.put_f64(self.position.x);
bytebuf.put_f64(self.position.y);
bytebuf.put_f64(self.position.z);
bytebuf.put_f64(self.delta.x);
bytebuf.put_f64(self.delta.y);
bytebuf.put_f64(self.delta.z);
bytebuf.put_f32(self.yaw);
bytebuf.put_f32(self.pitch);
// not sure about that
bytebuf.put_i32(PositionFlag::get_bitfield(self.releatives));
bytebuf.put_bool(self.on_ground);
}
}
4 changes: 2 additions & 2 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod c_play_disconnect;
mod c_player_abilities;
mod c_player_chat_message;
mod c_player_info_update;
mod c_player_position;
mod c_player_remove;
mod c_remove_entities;
mod c_reset_score;
Expand All @@ -55,7 +56,6 @@ mod c_sound_effect;
mod c_spawn_entity;
mod c_store_cookie;
mod c_subtitle;
mod c_sync_player_position;
mod c_system_chat_message;
mod c_teleport_entity;
mod c_transfer;
Expand Down Expand Up @@ -104,6 +104,7 @@ pub use c_play_disconnect::*;
pub use c_player_abilities::*;
pub use c_player_chat_message::*;
pub use c_player_info_update::*;
pub use c_player_position::*;
pub use c_player_remove::*;
pub use c_remove_entities::*;
pub use c_reset_score::*;
Expand All @@ -125,7 +126,6 @@ pub use c_sound_effect::*;
pub use c_spawn_entity::*;
pub use c_store_cookie::*;
pub use c_subtitle::*;
pub use c_sync_player_position::*;
pub use c_system_chat_message::*;
pub use c_teleport_entity::*;
pub use c_transfer::*;
Expand Down
2 changes: 1 addition & 1 deletion pumpkin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ parking_lot.workspace = true
serde.workspace = true
serde_json.workspace = true

bytes = "1.8"
bytes.workspace = true

rand = "0.8.5"

Expand Down
32 changes: 17 additions & 15 deletions pumpkin/src/client/player_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ impl Player {
if let Some((id, position)) = awaiting_teleport.as_ref() {
if id == &confirm_teleport.teleport_id {
// we should set the pos now to that we requested in the teleport packet, Is may fixed issues when the client sended position packets while being teleported
self.living_entity
.set_pos(position.x, position.y, position.z);
self.living_entity.set_pos(*position);

*awaiting_teleport = None;
} else {
Expand All @@ -115,25 +114,28 @@ impl Player {
pos.clamp(-2.0E7, 2.0E7)
}

pub async fn handle_position(self: &Arc<Self>, position: SPlayerPosition) {
if position.x.is_nan() || position.feet_y.is_nan() || position.z.is_nan() {
pub async fn handle_position(self: &Arc<Self>, position_packet: SPlayerPosition) {
if position_packet.x.is_nan()
|| position_packet.feet_y.is_nan()
|| position_packet.z.is_nan()
{
self.kick(TextComponent::text("Invalid movement")).await;
return;
}

let entity = &self.living_entity.entity;
self.living_entity.set_pos(
Self::clamp_horizontal(position.x),
Self::clamp_vertical(position.feet_y),
Self::clamp_horizontal(position.z),
let position = Vector3::new(
Self::clamp_horizontal(position_packet.x),
Self::clamp_vertical(position_packet.feet_y),
Self::clamp_horizontal(position_packet.z),
);
let entity = &self.living_entity.entity;
self.living_entity.set_pos(position);

let pos = entity.pos.load();
let last_pos = self.living_entity.last_pos.load();

entity
.on_ground
.store(position.ground, std::sync::atomic::Ordering::Relaxed);
.store(position_packet.ground, std::sync::atomic::Ordering::Relaxed);

let entity_id = entity.entity_id;
let Vector3 { x, y, z } = pos;
Expand Down Expand Up @@ -161,7 +163,7 @@ impl Player {
x.mul_add(4096.0, -(last_x * 4096.0)) as i16,
y.mul_add(4096.0, -(last_y * 4096.0)) as i16,
z.mul_add(4096.0, -(last_z * 4096.0)) as i16,
position.ground,
position_packet.ground,
),
)
.await;
Expand All @@ -184,13 +186,13 @@ impl Player {
self.kick(TextComponent::text("Invalid rotation")).await;
return;
}

let entity = &self.living_entity.entity;
self.living_entity.set_pos(
let position = Vector3::new(
Self::clamp_horizontal(position_rotation.x),
Self::clamp_vertical(position_rotation.feet_y),
Self::clamp_horizontal(position_rotation.z),
);
let entity = &self.living_entity.entity;
self.living_entity.set_pos(position);

let pos = entity.pos.load();
let last_pos = self.living_entity.last_pos.load();
Expand Down
14 changes: 7 additions & 7 deletions pumpkin/src/command/commands/cmd_teleport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl CommandExecutor for TpEntitiesToEntityExecutor {
for target in targets {
let yaw = target.living_entity.entity.yaw.load();
let pitch = target.living_entity.entity.pitch.load();
target.teleport(pos, yaw, pitch).await;
target.living_entity.entity.teleport(pos, yaw, pitch).await;
}

Ok(())
Expand All @@ -93,7 +93,7 @@ impl CommandExecutor for TpEntitiesToPosFacingPosExecutor {
let (yaw, pitch) = yaw_pitch_facing_position(&pos, &facing_pos);

for target in targets {
target.teleport(pos, yaw, pitch).await;
target.living_entity.entity.teleport(pos, yaw, pitch).await;
}

Ok(())
Expand All @@ -120,7 +120,7 @@ impl CommandExecutor for TpEntitiesToPosFacingEntityExecutor {
let (yaw, pitch) = yaw_pitch_facing_position(&pos, &facing_entity.pos.load());

for target in targets {
target.teleport(pos, yaw, pitch).await;
target.living_entity.entity.teleport(pos, yaw, pitch).await;
}

Ok(())
Expand All @@ -144,7 +144,7 @@ impl CommandExecutor for TpEntitiesToPosWithRotationExecutor {
let (yaw, pitch) = RotationArgumentConsumer::find_arg(args, ARG_ROTATION)?;

for target in targets {
target.teleport(pos, yaw, pitch).await;
target.living_entity.entity.teleport(pos, yaw, pitch).await;
}

Ok(())
Expand All @@ -168,7 +168,7 @@ impl CommandExecutor for TpEntitiesToPosExecutor {
for target in targets {
let yaw = target.living_entity.entity.yaw.load();
let pitch = target.living_entity.entity.pitch.load();
target.teleport(pos, yaw, pitch).await;
target.living_entity.entity.teleport(pos, yaw, pitch).await;
}

Ok(())
Expand All @@ -192,7 +192,7 @@ impl CommandExecutor for TpSelfToEntityExecutor {
CommandSender::Player(player) => {
let yaw = player.living_entity.entity.yaw.load();
let pitch = player.living_entity.entity.pitch.load();
player.teleport(pos, yaw, pitch).await;
player.living_entity.entity.teleport(pos, yaw, pitch).await;
}
_ => {
sender
Expand Down Expand Up @@ -222,7 +222,7 @@ impl CommandExecutor for TpSelfToPosExecutor {
let pos = Position3DArgumentConsumer::find_arg(args, ARG_LOCATION)?;
let yaw = player.living_entity.entity.yaw.load();
let pitch = player.living_entity.entity.pitch.load();
player.teleport(pos, yaw, pitch).await;
player.living_entity.entity.teleport(pos, yaw, pitch).await;
}
_ => {
sender
Expand Down
4 changes: 2 additions & 2 deletions pumpkin/src/entity/living.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ impl<C: Container> LivingEntity<C> {
}
}

pub fn set_pos(&self, x: f64, y: f64, z: f64) {
pub fn set_pos(&self, position: Vector3<f64>) {
self.last_pos.store(self.entity.pos.load());
self.entity.set_pos(x, y, z);
self.entity.set_pos(position);
}

pub async fn set_health(&self, health: f32) {
Expand Down
Loading

0 comments on commit b2f627d

Please sign in to comment.