Skip to content

Commit e268c49

Browse files
committed
fix incorrect packets
1 parent 1f06a15 commit e268c49

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+543
-691
lines changed

azalea-client/src/inventory.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub struct Inventory {
8585
/// guaranteed to be anything specific, and may change every time you open a
8686
/// container (unless it's 0, in which case it means that no container is
8787
/// open).
88-
pub id: u8,
88+
pub id: i32,
8989
/// The current container menu that the player has open. If no container is
9090
/// open, this will be `None`.
9191
pub container_menu: Option<azalea_inventory::Menu>,
@@ -584,7 +584,7 @@ impl Default for Inventory {
584584
#[derive(Event, Debug)]
585585
pub struct MenuOpenedEvent {
586586
pub entity: Entity,
587-
pub window_id: u32,
587+
pub window_id: i32,
588588
pub menu_type: MenuKind,
589589
pub title: FormattedText,
590590
}
@@ -594,7 +594,7 @@ fn handle_menu_opened_event(
594594
) {
595595
for event in events.read() {
596596
let mut inventory = query.get_mut(event.entity).unwrap();
597-
inventory.id = event.window_id as u8;
597+
inventory.id = event.window_id;
598598
inventory.container_menu = Some(Menu::from_kind(event.menu_type));
599599
inventory.container_menu_title = Some(event.title.clone());
600600
}
@@ -609,7 +609,7 @@ pub struct CloseContainerEvent {
609609
pub entity: Entity,
610610
/// The ID of the container to close. 0 for the player's inventory. If this
611611
/// is not the same as the currently open inventory, nothing will happen.
612-
pub id: u8,
612+
pub id: i32,
613613
}
614614
fn handle_container_close_event(
615615
query: Query<(Entity, &Inventory)>,
@@ -661,7 +661,7 @@ pub fn handle_client_side_close_container_event(
661661
#[derive(Event, Debug)]
662662
pub struct ContainerClickEvent {
663663
pub entity: Entity,
664-
pub window_id: u8,
664+
pub window_id: i32,
665665
pub operation: ClickOperation,
666666
}
667667
pub fn handle_container_click_event(
@@ -715,7 +715,7 @@ pub fn handle_container_click_event(
715715
pub struct SetContainerContentEvent {
716716
pub entity: Entity,
717717
pub slots: Vec<ItemStack>,
718-
pub container_id: u8,
718+
pub container_id: i32,
719719
}
720720
fn handle_set_container_content_event(
721721
mut events: EventReader<SetContainerContentEvent>,

azalea-client/src/local_player.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ pub fn death_event(query: Query<&LocalPlayerEvents, Added<Dead>>) {
138138
}
139139

140140
#[derive(Error, Debug)]
141-
#[expect(clippy::large_enum_variant)]
142141
pub enum HandlePacketError {
143142
#[error("{0}")]
144143
Poison(String),

azalea-client/src/movement.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,27 +193,23 @@ pub fn send_position(
193193
Some(
194194
ServerboundMovePlayerPosRot {
195195
pos: **position,
196-
x_rot: direction.x_rot,
197-
y_rot: direction.y_rot,
196+
look_direction: *direction,
198197
on_ground: physics.on_ground(),
199198
}
200199
.into_variant(),
201200
)
202201
} else if sending_position {
203202
Some(
204203
ServerboundMovePlayerPos {
205-
x: position.x,
206-
y: position.y,
207-
z: position.z,
204+
pos: **position,
208205
on_ground: physics.on_ground(),
209206
}
210207
.into_variant(),
211208
)
212209
} else if sending_direction {
213210
Some(
214211
ServerboundMovePlayerRot {
215-
x_rot: direction.x_rot,
216-
y_rot: direction.y_rot,
212+
look_direction: *direction,
217213
on_ground: physics.on_ground(),
218214
}
219215
.into_variant(),

azalea-client/src/packet_handling/game.rs

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,7 @@ pub fn process_packet_events(ecs: &mut World) {
510510
player_entity,
511511
ServerboundMovePlayerPosRot {
512512
pos: new_pos,
513-
y_rot: new_y_rot,
514-
x_rot: new_x_rot,
513+
look_direction: LookDirection::new(new_y_rot, new_x_rot),
515514
// this is always false
516515
on_ground: false,
517516
},
@@ -842,10 +841,10 @@ pub fn process_packet_events(ecs: &mut World) {
842841
continue;
843842
};
844843

845-
let new_pos = p.position;
844+
let new_pos = p.change.pos;
846845
let new_look_direction = LookDirection {
847-
x_rot: (p.x_rot as i32 * 360) as f32 / 256.,
848-
y_rot: (p.y_rot as i32 * 360) as f32 / 256.,
846+
x_rot: (p.change.look_direction.x_rot as i32 * 360) as f32 / 256.,
847+
y_rot: (p.change.look_direction.y_rot as i32 * 360) as f32 / 256.,
849848
};
850849
commands.entity(entity).queue(RelativeEntityUpdate {
851850
partial_world: instance_holder.partial_instance.clone(),
@@ -879,35 +878,34 @@ pub fn process_packet_events(ecs: &mut World) {
879878

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

882-
let entity = entity_id_index.get(&MinecraftEntityId(p.entity_id));
883-
884-
if let Some(entity) = entity {
885-
let new_delta = p.delta.clone();
886-
let new_on_ground = p.on_ground;
887-
commands.entity(entity).queue(RelativeEntityUpdate {
888-
partial_world: instance_holder.partial_instance.clone(),
889-
update: Box::new(move |entity_mut| {
890-
let mut physics = entity_mut.get_mut::<Physics>().unwrap();
891-
let new_pos = physics.vec_delta_codec.decode(
892-
new_delta.xa as i64,
893-
new_delta.ya as i64,
894-
new_delta.za as i64,
895-
);
896-
physics.vec_delta_codec.set_base(new_pos);
897-
physics.set_on_ground(new_on_ground);
898-
899-
let mut position = entity_mut.get_mut::<Position>().unwrap();
900-
if new_pos != **position {
901-
**position = new_pos;
902-
}
903-
}),
904-
});
905-
} else {
881+
let Some(entity) = entity_id_index.get(&MinecraftEntityId(p.entity_id)) else {
906882
warn!(
907883
"Got move entity pos packet for unknown entity id {}",
908884
p.entity_id
909885
);
910-
}
886+
continue;
887+
};
888+
889+
let new_delta = p.delta.clone();
890+
let new_on_ground = p.on_ground;
891+
commands.entity(entity).queue(RelativeEntityUpdate {
892+
partial_world: instance_holder.partial_instance.clone(),
893+
update: Box::new(move |entity_mut| {
894+
let mut physics = entity_mut.get_mut::<Physics>().unwrap();
895+
let new_pos = physics.vec_delta_codec.decode(
896+
new_delta.xa as i64,
897+
new_delta.ya as i64,
898+
new_delta.za as i64,
899+
);
900+
physics.vec_delta_codec.set_base(new_pos);
901+
physics.set_on_ground(new_on_ground);
902+
903+
let mut position = entity_mut.get_mut::<Position>().unwrap();
904+
if new_pos != **position {
905+
**position = new_pos;
906+
}
907+
}),
908+
});
911909

912910
system_state.apply(ecs);
913911
}
@@ -1191,7 +1189,7 @@ pub fn process_packet_events(ecs: &mut World) {
11911189
events.send(SetContainerContentEvent {
11921190
entity: player_entity,
11931191
slots: p.items.clone(),
1194-
container_id: p.container_id as u8,
1192+
container_id: p.container_id,
11951193
});
11961194
}
11971195
}
@@ -1235,7 +1233,7 @@ pub fn process_packet_events(ecs: &mut World) {
12351233
if let Some(slot) = inventory.inventory_menu.slot_mut(p.slot.into()) {
12361234
*slot = p.item_stack.clone();
12371235
}
1238-
} else if p.container_id == (inventory.id as i8)
1236+
} else if p.container_id == inventory.id
12391237
&& (p.container_id != 0 || !is_creative_mode_and_inventory_closed)
12401238
{
12411239
// var2.containerMenu.setItem(var4, var1.getStateId(), var3);
@@ -1260,20 +1258,18 @@ pub fn process_packet_events(ecs: &mut World) {
12601258
ClientboundGamePacket::DeleteChat(_) => {}
12611259
ClientboundGamePacket::Explode(p) => {
12621260
trace!("Got explode packet {p:?}");
1263-
let mut system_state: SystemState<EventWriter<KnockbackEvent>> =
1264-
SystemState::new(ecs);
1265-
let mut knockback_events = system_state.get_mut(ecs);
1261+
if let Some(knockback) = p.knockback {
1262+
let mut system_state: SystemState<EventWriter<KnockbackEvent>> =
1263+
SystemState::new(ecs);
1264+
let mut knockback_events = system_state.get_mut(ecs);
12661265

1267-
knockback_events.send(KnockbackEvent {
1268-
entity: player_entity,
1269-
knockback: KnockbackType::Set(Vec3 {
1270-
x: p.knockback_x as f64,
1271-
y: p.knockback_y as f64,
1272-
z: p.knockback_z as f64,
1273-
}),
1274-
});
1266+
knockback_events.send(KnockbackEvent {
1267+
entity: player_entity,
1268+
knockback: KnockbackType::Set(knockback),
1269+
});
12751270

1276-
system_state.apply(ecs);
1271+
system_state.apply(ecs);
1272+
}
12771273
}
12781274
ClientboundGamePacket::ForgetLevelChunk(p) => {
12791275
debug!("Got forget level chunk packet {p:?}");

azalea-protocol/src/common/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! Some serializable data types that are used by several packets.
22
33
pub mod client_information;
4+
pub mod movements;
5+
pub mod recipe;
46
pub mod server_links;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::io::{self, Cursor, Write};
2+
3+
use azalea_buf::{AzBuf, AzaleaRead, AzaleaWrite, BufReadError};
4+
use azalea_core::{bitset::FixedBitSet, position::Vec3};
5+
use azalea_entity::LookDirection;
6+
7+
/// The updated position, velocity, and rotations for an entity.
8+
///
9+
/// Often, this field comes alongside a [`RelativeMovements`] field, which
10+
/// specifies which parts of this struct should be treated as relative.
11+
#[derive(AzBuf, Clone, Debug)]
12+
pub struct PositionMoveRotation {
13+
pub pos: Vec3,
14+
/// The updated delta movement (velocity).
15+
pub delta: Vec3,
16+
pub look_direction: LookDirection,
17+
}
18+
19+
#[derive(Debug, Clone)]
20+
pub struct RelativeMovements {
21+
pub x: bool,
22+
pub y: bool,
23+
pub z: bool,
24+
pub y_rot: bool,
25+
pub x_rot: bool,
26+
pub delta_x: bool,
27+
pub delta_y: bool,
28+
pub delta_z: bool,
29+
pub rotate_delta: bool,
30+
}
31+
32+
impl AzaleaRead for RelativeMovements {
33+
fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
34+
// yes minecraft seriously wastes that many bits, smh
35+
let set = FixedBitSet::<{ 32_usize.div_ceil(8) }>::azalea_read(buf)?;
36+
Ok(RelativeMovements {
37+
x: set.index(0),
38+
y: set.index(1),
39+
z: set.index(2),
40+
y_rot: set.index(3),
41+
x_rot: set.index(4),
42+
delta_x: set.index(5),
43+
delta_y: set.index(6),
44+
delta_z: set.index(7),
45+
rotate_delta: set.index(8),
46+
})
47+
}
48+
}
49+
50+
impl AzaleaWrite for RelativeMovements {
51+
fn azalea_write(&self, buf: &mut impl Write) -> Result<(), io::Error> {
52+
let mut set = FixedBitSet::<{ 32_usize.div_ceil(8) }>::new();
53+
let mut set_bit = |index: usize, value: bool| {
54+
if value {
55+
set.set(index);
56+
}
57+
};
58+
59+
set_bit(0, self.x);
60+
set_bit(1, self.y);
61+
set_bit(2, self.z);
62+
set_bit(3, self.y_rot);
63+
set_bit(4, self.x_rot);
64+
set_bit(5, self.delta_x);
65+
set_bit(6, self.delta_y);
66+
set_bit(7, self.delta_z);
67+
set_bit(8, self.rotate_delta);
68+
69+
set.azalea_write(buf)
70+
}
71+
}

0 commit comments

Comments
 (0)