Skip to content

Commit

Permalink
fix incorrect packets
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Dec 19, 2024
1 parent 1f06a15 commit e268c49
Show file tree
Hide file tree
Showing 46 changed files with 543 additions and 691 deletions.
12 changes: 6 additions & 6 deletions azalea-client/src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub struct Inventory {
/// guaranteed to be anything specific, and may change every time you open a
/// container (unless it's 0, in which case it means that no container is
/// open).
pub id: u8,
pub id: i32,
/// The current container menu that the player has open. If no container is
/// open, this will be `None`.
pub container_menu: Option<azalea_inventory::Menu>,
Expand Down Expand Up @@ -584,7 +584,7 @@ impl Default for Inventory {
#[derive(Event, Debug)]
pub struct MenuOpenedEvent {
pub entity: Entity,
pub window_id: u32,
pub window_id: i32,
pub menu_type: MenuKind,
pub title: FormattedText,
}
Expand All @@ -594,7 +594,7 @@ fn handle_menu_opened_event(
) {
for event in events.read() {
let mut inventory = query.get_mut(event.entity).unwrap();
inventory.id = event.window_id as u8;
inventory.id = event.window_id;
inventory.container_menu = Some(Menu::from_kind(event.menu_type));
inventory.container_menu_title = Some(event.title.clone());
}
Expand All @@ -609,7 +609,7 @@ pub struct CloseContainerEvent {
pub entity: Entity,
/// The ID of the container to close. 0 for the player's inventory. If this
/// is not the same as the currently open inventory, nothing will happen.
pub id: u8,
pub id: i32,
}
fn handle_container_close_event(
query: Query<(Entity, &Inventory)>,
Expand Down Expand Up @@ -661,7 +661,7 @@ pub fn handle_client_side_close_container_event(
#[derive(Event, Debug)]
pub struct ContainerClickEvent {
pub entity: Entity,
pub window_id: u8,
pub window_id: i32,
pub operation: ClickOperation,
}
pub fn handle_container_click_event(
Expand Down Expand Up @@ -715,7 +715,7 @@ pub fn handle_container_click_event(
pub struct SetContainerContentEvent {
pub entity: Entity,
pub slots: Vec<ItemStack>,
pub container_id: u8,
pub container_id: i32,
}
fn handle_set_container_content_event(
mut events: EventReader<SetContainerContentEvent>,
Expand Down
1 change: 0 additions & 1 deletion azalea-client/src/local_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ pub fn death_event(query: Query<&LocalPlayerEvents, Added<Dead>>) {
}

#[derive(Error, Debug)]
#[expect(clippy::large_enum_variant)]
pub enum HandlePacketError {
#[error("{0}")]
Poison(String),
Expand Down
10 changes: 3 additions & 7 deletions azalea-client/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,27 +193,23 @@ pub fn send_position(
Some(
ServerboundMovePlayerPosRot {
pos: **position,
x_rot: direction.x_rot,
y_rot: direction.y_rot,
look_direction: *direction,
on_ground: physics.on_ground(),
}
.into_variant(),
)
} else if sending_position {
Some(
ServerboundMovePlayerPos {
x: position.x,
y: position.y,
z: position.z,
pos: **position,
on_ground: physics.on_ground(),
}
.into_variant(),
)
} else if sending_direction {
Some(
ServerboundMovePlayerRot {
x_rot: direction.x_rot,
y_rot: direction.y_rot,
look_direction: *direction,
on_ground: physics.on_ground(),
}
.into_variant(),
Expand Down
84 changes: 40 additions & 44 deletions azalea-client/src/packet_handling/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,7 @@ pub fn process_packet_events(ecs: &mut World) {
player_entity,
ServerboundMovePlayerPosRot {
pos: new_pos,
y_rot: new_y_rot,
x_rot: new_x_rot,
look_direction: LookDirection::new(new_y_rot, new_x_rot),
// this is always false
on_ground: false,
},
Expand Down Expand Up @@ -842,10 +841,10 @@ pub fn process_packet_events(ecs: &mut World) {
continue;
};

let new_pos = p.position;
let new_pos = p.change.pos;
let new_look_direction = LookDirection {
x_rot: (p.x_rot as i32 * 360) as f32 / 256.,
y_rot: (p.y_rot as i32 * 360) as f32 / 256.,
x_rot: (p.change.look_direction.x_rot as i32 * 360) as f32 / 256.,
y_rot: (p.change.look_direction.y_rot as i32 * 360) as f32 / 256.,
};
commands.entity(entity).queue(RelativeEntityUpdate {
partial_world: instance_holder.partial_instance.clone(),
Expand Down Expand Up @@ -879,35 +878,34 @@ pub fn process_packet_events(ecs: &mut World) {

debug!("Got move entity pos packet {p:?}");

let entity = entity_id_index.get(&MinecraftEntityId(p.entity_id));

if let Some(entity) = entity {
let new_delta = p.delta.clone();
let new_on_ground = p.on_ground;
commands.entity(entity).queue(RelativeEntityUpdate {
partial_world: instance_holder.partial_instance.clone(),
update: Box::new(move |entity_mut| {
let mut physics = entity_mut.get_mut::<Physics>().unwrap();
let new_pos = physics.vec_delta_codec.decode(
new_delta.xa as i64,
new_delta.ya as i64,
new_delta.za as i64,
);
physics.vec_delta_codec.set_base(new_pos);
physics.set_on_ground(new_on_ground);

let mut position = entity_mut.get_mut::<Position>().unwrap();
if new_pos != **position {
**position = new_pos;
}
}),
});
} else {
let Some(entity) = entity_id_index.get(&MinecraftEntityId(p.entity_id)) else {
warn!(
"Got move entity pos packet for unknown entity id {}",
p.entity_id
);
}
continue;
};

let new_delta = p.delta.clone();
let new_on_ground = p.on_ground;
commands.entity(entity).queue(RelativeEntityUpdate {
partial_world: instance_holder.partial_instance.clone(),
update: Box::new(move |entity_mut| {
let mut physics = entity_mut.get_mut::<Physics>().unwrap();
let new_pos = physics.vec_delta_codec.decode(
new_delta.xa as i64,
new_delta.ya as i64,
new_delta.za as i64,
);
physics.vec_delta_codec.set_base(new_pos);
physics.set_on_ground(new_on_ground);

let mut position = entity_mut.get_mut::<Position>().unwrap();
if new_pos != **position {
**position = new_pos;
}
}),
});

system_state.apply(ecs);
}
Expand Down Expand Up @@ -1191,7 +1189,7 @@ pub fn process_packet_events(ecs: &mut World) {
events.send(SetContainerContentEvent {
entity: player_entity,
slots: p.items.clone(),
container_id: p.container_id as u8,
container_id: p.container_id,
});
}
}
Expand Down Expand Up @@ -1235,7 +1233,7 @@ pub fn process_packet_events(ecs: &mut World) {
if let Some(slot) = inventory.inventory_menu.slot_mut(p.slot.into()) {
*slot = p.item_stack.clone();
}
} else if p.container_id == (inventory.id as i8)
} else if p.container_id == inventory.id
&& (p.container_id != 0 || !is_creative_mode_and_inventory_closed)
{
// var2.containerMenu.setItem(var4, var1.getStateId(), var3);
Expand All @@ -1260,20 +1258,18 @@ pub fn process_packet_events(ecs: &mut World) {
ClientboundGamePacket::DeleteChat(_) => {}
ClientboundGamePacket::Explode(p) => {
trace!("Got explode packet {p:?}");
let mut system_state: SystemState<EventWriter<KnockbackEvent>> =
SystemState::new(ecs);
let mut knockback_events = system_state.get_mut(ecs);
if let Some(knockback) = p.knockback {
let mut system_state: SystemState<EventWriter<KnockbackEvent>> =
SystemState::new(ecs);
let mut knockback_events = system_state.get_mut(ecs);

knockback_events.send(KnockbackEvent {
entity: player_entity,
knockback: KnockbackType::Set(Vec3 {
x: p.knockback_x as f64,
y: p.knockback_y as f64,
z: p.knockback_z as f64,
}),
});
knockback_events.send(KnockbackEvent {
entity: player_entity,
knockback: KnockbackType::Set(knockback),
});

system_state.apply(ecs);
system_state.apply(ecs);
}
}
ClientboundGamePacket::ForgetLevelChunk(p) => {
debug!("Got forget level chunk packet {p:?}");
Expand Down
2 changes: 2 additions & 0 deletions azalea-protocol/src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Some serializable data types that are used by several packets.
pub mod client_information;
pub mod movements;
pub mod recipe;
pub mod server_links;
71 changes: 71 additions & 0 deletions azalea-protocol/src/common/movements.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::io::{self, Cursor, Write};

use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError};
use azalea_core::{bitset::FixedBitSet, position::Vec3};
use azalea_entity::LookDirection;

/// The updated position, velocity, and rotations for an entity.
///
/// Often, this field comes alongside a [`RelativeMovements`] field, which
/// specifies which parts of this struct should be treated as relative.
#[derive(AzBuf, Clone, Debug)]
pub struct PositionMoveRotation {
pub pos: Vec3,
/// The updated delta movement (velocity).
pub delta: Vec3,
pub look_direction: LookDirection,
}

#[derive(Debug, Clone)]
pub struct RelativeMovements {
pub x: bool,
pub y: bool,
pub z: bool,
pub y_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 {
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
// yes minecraft seriously wastes that many bits, smh
let set = FixedBitSet::<{ 32_usize.div_ceil(8) }>::azalea_read(buf)?;
Ok(RelativeMovements {
x: set.index(0),
y: set.index(1),
z: set.index(2),
y_rot: set.index(3),
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 {
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), io::Error> {
let mut set = FixedBitSet::<{ 32_usize.div_ceil(8) }>::new();
let mut set_bit = |index: usize, value: bool| {
if value {
set.set(index);
}
};

set_bit(0, self.x);
set_bit(1, self.y);
set_bit(2, self.z);
set_bit(3, self.y_rot);
set_bit(4, self.x_rot);
set_bit(5, self.delta_x);
set_bit(6, self.delta_y);
set_bit(7, self.delta_z);
set_bit(8, self.rotate_delta);

set.azalea_write(buf)
}
}
Loading

0 comments on commit e268c49

Please sign in to comment.