1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 06:16:04 +00:00

fix container_set_content, player_position, and recipe_book_remove packets

This commit is contained in:
mat 2024-12-05 06:30:47 +00:00
parent 6379035b85
commit 39f4d68e1f
11 changed files with 151 additions and 90 deletions

View file

@ -50,7 +50,6 @@ env_logger = "0.11.5"
flate2 = "1.0.35" flate2 = "1.0.35"
futures = "0.3.31" futures = "0.3.31"
futures-lite = "2.5.0" futures-lite = "2.5.0"
#futures-util = "0.3.31"
log = "0.4.22" log = "0.4.22"
md-5 = "0.10.6" md-5 = "0.10.6"
minecraft_folder_path = "0.1.2" minecraft_folder_path = "0.1.2"
@ -72,7 +71,6 @@ serde_json = "1.0.133"
sha-1 = "0.10.1" sha-1 = "0.10.1"
sha2 = "0.10.8" sha2 = "0.10.8"
simdnbt = "0.6" simdnbt = "0.6"
#smallvec = "1.13.2"
socks5-impl = "0.5.17" socks5-impl = "0.5.17"
syn = "2.0.90" syn = "2.0.90"
thiserror = "2.0.3" thiserror = "2.0.3"

View file

@ -192,9 +192,7 @@ pub fn send_position(
let packet = if sending_position && sending_direction { let packet = if sending_position && sending_direction {
Some( Some(
ServerboundMovePlayerPosRot { ServerboundMovePlayerPosRot {
x: position.x, pos: **position,
y: position.y,
z: position.z,
x_rot: direction.x_rot, x_rot: direction.x_rot,
y_rot: direction.y_rot, y_rot: direction.y_rot,
on_ground: physics.on_ground, on_ground: physics.on_ground,

View file

@ -1,12 +1,14 @@
use std::{ use std::{
collections::HashSet, collections::HashSet,
io::Cursor, io::Cursor,
ops::Add,
sync::{Arc, Weak}, sync::{Arc, Weak},
}; };
use azalea_chat::FormattedText; use azalea_chat::FormattedText;
use azalea_core::{ use azalea_core::{
game_type::GameMode, game_type::GameMode,
math,
position::{ChunkPos, Vec3}, position::{ChunkPos, Vec3},
resource_location::ResourceLocation, resource_location::ResourceLocation,
}; };
@ -435,64 +437,63 @@ pub fn process_packet_events(ecs: &mut World) {
continue; continue;
}; };
let delta_movement = physics.velocity; **last_sent_position = **position;
let is_x_relative = p.relative_arguments.x; fn apply_change<T: Add<Output = T>>(base: T, condition: bool, change: T) -> T {
let is_y_relative = p.relative_arguments.y; if condition {
let is_z_relative = p.relative_arguments.z; base + change
} else {
let (delta_x, new_pos_x) = if is_x_relative { change
last_sent_position.x += p.pos.x; }
(delta_movement.x, position.x + p.pos.x)
} else {
last_sent_position.x = p.pos.x;
(0.0, p.pos.x)
};
let (delta_y, new_pos_y) = if is_y_relative {
last_sent_position.y += p.pos.y;
(delta_movement.y, position.y + p.pos.y)
} else {
last_sent_position.y = p.pos.y;
(0.0, p.pos.y)
};
let (delta_z, new_pos_z) = if is_z_relative {
last_sent_position.z += p.pos.z;
(delta_movement.z, position.z + p.pos.z)
} else {
last_sent_position.z = p.pos.z;
(0.0, p.pos.z)
};
let mut y_rot = p.y_rot;
let mut x_rot = p.x_rot;
if p.relative_arguments.x_rot {
x_rot += direction.x_rot;
}
if p.relative_arguments.y_rot {
y_rot += direction.y_rot;
} }
physics.velocity = Vec3 { let new_x = apply_change(position.x, p.relative.x, p.change.pos.x);
x: delta_x, let new_y = apply_change(position.y, p.relative.y, p.change.pos.y);
y: delta_y, let new_z = apply_change(position.z, p.relative.z, p.change.pos.z);
z: delta_z,
};
// we call a function instead of setting the fields ourself since the
// function makes sure the rotations stay in their
// ranges
(direction.y_rot, direction.x_rot) = (y_rot, x_rot);
// TODO: minecraft sets "xo", "yo", and "zo" here but idk what that means
// so investigate that ig
let new_pos = Vec3 {
x: new_pos_x,
y: new_pos_y,
z: new_pos_z,
};
let new_y_rot = apply_change(direction.y_rot, p.relative.y_rot, p.change.y_rot);
let new_x_rot = apply_change(direction.x_rot, p.relative.x_rot, p.change.x_rot);
let mut new_delta_from_rotations = physics.velocity;
if p.relative.rotate_delta {
let y_rot_delta = direction.y_rot - new_y_rot;
let x_rot_delta = direction.x_rot - new_x_rot;
new_delta_from_rotations = new_delta_from_rotations
.x_rot(math::to_radians(x_rot_delta as f64) as f32)
.y_rot(math::to_radians(y_rot_delta as f64) as f32);
}
let new_delta = Vec3::new(
apply_change(
new_delta_from_rotations.x,
p.relative.delta_x,
p.change.delta.x,
),
apply_change(
new_delta_from_rotations.y,
p.relative.delta_y,
p.change.delta.y,
),
apply_change(
new_delta_from_rotations.z,
p.relative.delta_z,
p.change.delta.z,
),
);
// apply the updates
physics.velocity = new_delta;
(direction.y_rot, direction.x_rot) = (new_y_rot, new_x_rot);
let new_pos = Vec3::new(new_x, new_y, new_z);
if new_pos != **position { if new_pos != **position {
**position = new_pos; **position = new_pos;
} }
// send the relevant packets
send_packet_events.send(SendPacketEvent::new( send_packet_events.send(SendPacketEvent::new(
player_entity, player_entity,
ServerboundAcceptTeleportation { id: p.id }, ServerboundAcceptTeleportation { id: p.id },
@ -500,11 +501,9 @@ pub fn process_packet_events(ecs: &mut World) {
send_packet_events.send(SendPacketEvent::new( send_packet_events.send(SendPacketEvent::new(
player_entity, player_entity,
ServerboundMovePlayerPosRot { ServerboundMovePlayerPosRot {
x: new_pos.x, pos: new_pos,
y: new_pos.y, y_rot: new_y_rot,
z: new_pos.z, x_rot: new_x_rot,
y_rot,
x_rot,
// this is always false // this is always false
on_ground: false, on_ground: false,
}, },

View file

@ -68,6 +68,15 @@ pub fn fract(x: f64) -> f64 {
x - floor x - floor
} }
// these are copied from the java standard library, we don't calculate the
// consts ourself to make sure it's the same as java
pub fn to_radians(degrees: f64) -> f64 {
degrees * 0.017453292519943295
}
pub fn to_degrees(radians: f64) -> f64 {
radians * 57.29577951308232
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View file

@ -13,6 +13,7 @@ use std::{
use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError}; use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError};
use crate::math;
use crate::resource_location::ResourceLocation; use crate::resource_location::ResourceLocation;
macro_rules! vec3_impl { macro_rules! vec3_impl {
@ -233,6 +234,23 @@ impl Vec3 {
pub fn distance_to(&self, other: &Self) -> f64 { pub fn distance_to(&self, other: &Self) -> f64 {
(self - other).length() (self - other).length()
} }
pub fn x_rot(self, radians: f32) -> Vec3 {
let x_delta = math::cos(radians);
let y_delta = math::sin(radians);
let x = self.x;
let y = self.y * (x_delta as f64) + self.z * (y_delta as f64);
let z = self.z * (x_delta as f64) - self.y * (y_delta as f64);
Vec3 { x, y, z }
}
pub fn y_rot(self, radians: f32) -> Vec3 {
let x_delta = math::cos(radians);
let y_delta = math::sin(radians);
let x = self.x * (x_delta as f64) + self.z * (y_delta as f64);
let y = self.y;
let z = self.z * (x_delta as f64) - self.x * (y_delta as f64);
Vec3 { x, y, z }
}
} }
/// The coordinates of a block in the world. For entities (if the coordinate /// The coordinates of a block in the world. For entities (if the coordinate

View file

@ -172,7 +172,8 @@ impl From<&Position> for BlockPos {
} }
} }
/// The last position of the entity that was sent over the network. /// The second most recent position of the entity that was sent over the
/// network. This is currently only updated for our own local player entities.
#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Deref, DerefMut)] #[derive(Component, Clone, Copy, Debug, Default, PartialEq, Deref, DerefMut)]
pub struct LastSentPosition(Vec3); pub struct LastSentPosition(Vec3);
impl From<&LastSentPosition> for Vec3 { impl From<&LastSentPosition> for Vec3 {

View file

@ -161,7 +161,7 @@ impl AzaleaRead for ItemStack {
impl AzaleaWrite for ItemStack { impl AzaleaWrite for ItemStack {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
match self { match self {
ItemStack::Empty => 0.azalea_write_var(buf)?, ItemStack::Empty => 0_i32.azalea_write_var(buf)?,
ItemStack::Present(i) => { ItemStack::Present(i) => {
i.count.azalea_write_var(buf)?; i.count.azalea_write_var(buf)?;
i.kind.azalea_write(buf)?; i.kind.azalea_write(buf)?;
@ -262,8 +262,8 @@ impl AzaleaWrite for DataComponentPatch {
} }
} }
components_with_data_count.azalea_write(buf)?; components_with_data_count.azalea_write_var(buf)?;
components_without_data_count.azalea_write(buf)?; components_without_data_count.azalea_write_var(buf)?;
for (kind, component) in &self.components { for (kind, component) in &self.components {
if let Some(component) = component { if let Some(component) = component {

View file

@ -10,3 +10,29 @@ pub struct ClientboundContainerSetContent {
pub items: Vec<ItemStack>, pub items: Vec<ItemStack>,
pub carried_item: ItemStack, pub carried_item: ItemStack,
} }
#[cfg(test)]
mod tests {
use std::io::Cursor;
use super::ClientboundContainerSetContent;
use crate::packets::ProtocolPacket;
#[test]
fn test_read_write_container_set_content() {
let contents = [
1, 2, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 196, 6, 0, 0, 0,
];
let mut buf = Cursor::new(contents.as_slice());
let packet = ClientboundContainerSetContent::read(&mut buf).unwrap();
println!("{:?}", packet);
assert_eq!(buf.position(), contents.len() as u64);
let mut buf = Vec::new();
packet.write(&mut buf).unwrap();
assert_eq!(buf, contents);
}
}

View file

@ -8,11 +8,19 @@ use azalea_protocol_macros::ClientboundGamePacket;
pub struct ClientboundPlayerPosition { pub struct ClientboundPlayerPosition {
#[var] #[var]
pub id: u32, pub id: u32,
pub change: PositionMoveRotation,
pub relative: RelativeMovements,
}
/// These values are either absolute or relative, depending on the fields from
/// the [`RelativeMovements`].
#[derive(Clone, Debug, AzBuf)]
pub struct PositionMoveRotation {
pub pos: Vec3, pub pos: Vec3,
pub delta_movement: Vec3, /// The updated delta movement (velocity).
pub delta: Vec3,
pub y_rot: f32, pub y_rot: f32,
pub x_rot: f32, pub x_rot: f32,
pub relative_arguments: RelativeMovements,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -22,6 +30,10 @@ pub struct RelativeMovements {
pub z: bool, pub z: bool,
pub y_rot: bool, pub y_rot: bool,
pub x_rot: bool, pub x_rot: bool,
pub delta_x: bool,
pub delta_y: bool,
pub delta_z: bool,
pub rotate_delta: bool,
} }
impl AzaleaRead for RelativeMovements { impl AzaleaRead for RelativeMovements {
@ -34,28 +46,33 @@ impl AzaleaRead for RelativeMovements {
z: set.index(2), z: set.index(2),
y_rot: set.index(3), y_rot: set.index(3),
x_rot: set.index(4), x_rot: set.index(4),
delta_x: set.index(5),
delta_y: set.index(6),
delta_z: set.index(7),
rotate_delta: set.index(8),
}) })
} }
} }
impl AzaleaWrite for RelativeMovements { impl AzaleaWrite for RelativeMovements {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> { fn azalea_write(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
let mut set = FixedBitSet::<5>::new(); let mut set = FixedBitSet::<32>::new();
if self.x { let mut set_bit = |index: usize, value: bool| {
set.set(0); if value {
} set.set(index);
if self.y { }
set.set(1); };
}
if self.z { set_bit(0, self.x);
set.set(2); set_bit(1, self.y);
} set_bit(2, self.z);
if self.y_rot { set_bit(3, self.y_rot);
set.set(3); set_bit(4, self.x_rot);
} set_bit(5, self.delta_x);
if self.x_rot { set_bit(6, self.delta_y);
set.set(4); set_bit(7, self.delta_z);
} set_bit(8, self.rotate_delta);
set.azalea_write(buf) set.azalea_write(buf)
} }
} }

View file

@ -1,12 +1,8 @@
use azalea_buf::AzBuf; use azalea_buf::AzBuf;
use azalea_protocol_macros::ClientboundGamePacket; use azalea_protocol_macros::ClientboundGamePacket;
use super::{c_entity_position_sync::PositionMoveRotation, c_player_position::RelativeMovements};
#[derive(Clone, Debug, AzBuf, ClientboundGamePacket)] #[derive(Clone, Debug, AzBuf, ClientboundGamePacket)]
pub struct ClientboundRecipeBookRemove { pub struct ClientboundRecipeBookRemove {
#[var] #[var]
pub id: u32, pub recipes: Vec<u32>,
pub change: PositionMoveRotation,
pub relatives: RelativeMovements,
} }

View file

@ -1,11 +1,10 @@
use azalea_buf::AzBuf; use azalea_buf::AzBuf;
use azalea_core::position::Vec3;
use azalea_protocol_macros::ServerboundGamePacket; use azalea_protocol_macros::ServerboundGamePacket;
#[derive(Clone, Debug, AzBuf, ServerboundGamePacket)] #[derive(Clone, Debug, AzBuf, ServerboundGamePacket)]
pub struct ServerboundMovePlayerPosRot { pub struct ServerboundMovePlayerPosRot {
pub x: f64, pub pos: Vec3,
pub y: f64,
pub z: f64,
pub y_rot: f32, pub y_rot: f32,
pub x_rot: f32, pub x_rot: f32,
pub on_ground: bool, pub on_ground: bool,