minior position/rotation improvements

This commit is contained in:
Snowiiii
2024-08-09 14:29:51 +02:00
parent e96f072951
commit 770d73f080
11 changed files with 80 additions and 50 deletions

View File

@@ -3,7 +3,7 @@ use pumpkin_macros::packet;
use pumpkin_text::TextComponent;
use serde::Serialize;
use crate::{ VarInt};
use crate::VarInt;
#[derive(Serialize, Clone)]
#[packet(0x39)]

View File

@@ -12,6 +12,9 @@ pub struct CRemoveEntities {
impl CRemoveEntities {
pub fn new(entitiy_ids: Vec<VarInt>) -> Self {
Self { count: VarInt(entitiy_ids.len() as i32), entitiy_ids }
Self {
count: VarInt(entitiy_ids.len() as i32),
entitiy_ids,
}
}
}

View File

@@ -29,13 +29,13 @@ impl CSpawnEntity {
x: f64,
y: f64,
z: f64,
pitch: u8, // angle
yaw: u8, // angle
head_yaw: u8, // angle
pitch: f32, // angle
yaw: f32, // angle
head_yaw: f32, // angle
data: VarInt,
velocity_x: i16,
velocity_y: i16,
velocity_z: i16,
velocity_x: f32,
velocity_y: f32,
velocity_z: f32,
) -> Self {
Self {
entity_id,
@@ -44,13 +44,13 @@ impl CSpawnEntity {
x,
y,
z,
pitch,
yaw,
head_yaw,
pitch: (pitch * 256.0 / 360.0).floor() as u8,
yaw: (yaw * 256.0 / 360.0).floor() as u8,
head_yaw: (head_yaw * 256.0 / 360.0).floor() as u8,
data,
velocity_x,
velocity_y,
velocity_z,
velocity_x: (velocity_x.clamp(-3.9, 3.9) * 8000.0) as i16,
velocity_y: (velocity_y.clamp(-3.9, 3.9) * 8000.0) as i16,
velocity_z: (velocity_z.clamp(-3.9, 3.9) * 8000.0) as i16,
}
}
}

View File

@@ -16,4 +16,4 @@ pub enum ClickEvent {
ChangePage(i32),
/// Copies the given text to system clipboard
CopyToClipboard(String),
}
}

View File

@@ -33,4 +33,4 @@ pub enum NamedColor {
LightPurple,
Yellow,
White,
}
}

View File

@@ -6,8 +6,8 @@ use fastnbt::SerOpts;
use hover::HoverEvent;
use serde::{Deserialize, Serialize};
pub mod color;
pub mod click;
pub mod color;
pub mod hover;
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
@@ -163,9 +163,9 @@ impl TextComponent {
click_event: &self.click_event,
hover_event: &self.hover_event,
};
// dbg!(&serde_json::to_string(&astruct));
let nbt = fastnbt::to_bytes_with_opts(&astruct, SerOpts::network_nbt()).unwrap();
nbt
// dbg!(&serde_json::to_string(&astruct));
fastnbt::to_bytes_with_opts(&astruct, SerOpts::network_nbt()).unwrap()
}
}

View File

@@ -15,6 +15,7 @@ use crate::{
commands::{handle_command, CommandSender},
entity::player::Hand,
server::Server,
util::math::wrap_degrees,
};
use super::Client;
@@ -38,18 +39,27 @@ impl Client {
}
}
fn clamp_horizontal(pos: f64) -> f64 {
pos.clamp(-3.0E7, 3.0E7)
}
fn clamp_vertical(pos: f64) -> f64 {
pos.clamp(-2.0E7, 2.0E7)
}
pub fn handle_position(&mut self, server: &mut Server, position: SPlayerPosition) {
if position.x.is_nan() || position.feet_y.is_nan() || position.z.is_nan() {
self.kick("Invalid movement");
return;
}
let player = self.player.as_mut().unwrap();
let entity = &mut player.entity;
entity.lastx = entity.x;
entity.lasty = entity.y;
entity.lastz = entity.z;
entity.x = position.x;
entity.y = position.feet_y;
entity.z = position.z;
entity.x = Self::clamp_horizontal(position.x);
entity.y = Self::clamp_vertical(position.feet_y);
entity.z = Self::clamp_horizontal(position.z);
// TODO: teleport when moving > 8 block
// send new position to all other players
@@ -81,18 +91,20 @@ impl Client {
|| position_rotation.z.is_nan()
{
self.kick("Invalid movement");
return;
}
if !position_rotation.yaw.is_finite() || !position_rotation.pitch.is_finite() {
self.kick("Invalid rotation");
return;
}
let player = self.player.as_mut().unwrap();
let entity = &mut player.entity;
entity.x = position_rotation.x;
entity.y = position_rotation.feet_y;
entity.z = position_rotation.z;
entity.yaw = position_rotation.yaw % 360.0;
entity.pitch = position_rotation.pitch.clamp(-90.0, 90.0) % 360.0;
entity.x = Self::clamp_horizontal(position_rotation.x);
entity.y = Self::clamp_vertical(position_rotation.feet_y);
entity.z = Self::clamp_horizontal(position_rotation.z);
entity.yaw = wrap_degrees(position_rotation.yaw);
entity.pitch = wrap_degrees(position_rotation.pitch);
// send new position to all other players
let on_ground = player.on_ground;
@@ -122,22 +134,24 @@ impl Client {
pub fn handle_rotation(&mut self, server: &mut Server, rotation: SPlayerRotation) {
if !rotation.yaw.is_finite() || !rotation.pitch.is_finite() {
self.kick("Invalid rotation");
return;
}
let player = self.player.as_mut().unwrap();
let entity = &mut player.entity;
entity.yaw = rotation.yaw % 360.0;
entity.pitch = rotation.pitch.clamp(-90.0, 90.0) % 360.0;
entity.yaw = wrap_degrees(rotation.yaw);
entity.pitch = wrap_degrees(rotation.pitch);
// send new position to all other players
let on_ground = player.on_ground;
let entity_id = entity.entity_id;
let yaw = entity.yaw;
let pitch = entity.pitch;
let yaw = (entity.yaw * 256.0 / 360.0).floor();
let pitch = (entity.pitch * 256.0 / 360.0).floor();
let head_yaw = (entity.head_yaw * 256.0 / 360.0).floor();
server.broadcast_packet(
self,
CUpdateEntityRot::new(entity_id.into(), yaw as u8, pitch as u8, on_ground),
);
server.broadcast_packet(self, CHeadRot::new(entity_id.into(), yaw as u8));
server.broadcast_packet(self, CHeadRot::new(entity_id.into(), head_yaw as u8));
}
pub fn handle_chat_command(&mut self, _server: &mut Server, command: SChatCommand) {

View File

@@ -1,7 +1,5 @@
use pumpkin_protocol::{
CURRENT_MC_PROTOCOL,
};
use pumpkin_text::{click::ClickEvent, color::NamedColor, hover::HoverEvent, TextComponent};
use pumpkin_protocol::CURRENT_MC_PROTOCOL;
use pumpkin_text::{color::NamedColor, TextComponent};
use crate::server::CURRENT_MC_VERSION;

View File

@@ -165,6 +165,8 @@ impl Server {
let x = 10.0;
let y = 500.0;
let z = 10.0;
let yaw = 10.0;
let pitch = 10.0;
client.teleport(x, y, z, 10.0, 10.0);
let gameprofile = client.gameprofile.as_ref().unwrap();
// first send info update to our new player, So he can see his Skin
@@ -208,6 +210,7 @@ impl Server {
// spawn player for every client
self.broadcast_packet_expect(
client,
// TODO: add velo
CSpawnEntity::new(
entity_id.into(),
gameprofile.id,
@@ -215,13 +218,13 @@ impl Server {
x,
y,
z,
0,
0,
0,
pitch,
yaw,
yaw,
0.into(),
0,
0,
0,
0.0,
0.0,
0.0,
),
);
// spawn players for our client
@@ -238,13 +241,13 @@ impl Server {
entity.x,
entity.y,
entity.z,
entity.yaw as u8,
entity.pitch as u8,
0,
entity.yaw,
entity.pitch,
entity.pitch,
0.into(),
0,
0,
0,
0.0,
0.0,
0.0,
))
}
}

12
pumpkin/src/util/math.rs Normal file
View File

@@ -0,0 +1,12 @@
pub fn wrap_degrees(var: f32) -> f32 {
let mut var1 = var % 360.0;
if var1 >= 180.0 {
var1 -= 360.0;
}
if var1 < -180.0 {
var1 += 360.0;
}
var1
}

View File

@@ -1 +1 @@
pub mod math;