Skip to content

Commit 682609a

Browse files
committed
knockback
1 parent 971f42e commit 682609a

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ A collection of Rust crates for making Minecraft bots, clients, and tools.
1616

1717
## Features
1818

19-
- [Accurate physics](https://github.com/azalea-rs/azalea/blob/main/azalea-physics/src/lib.rs) (but some features like knockback and water physics aren't yet implemented)
19+
- [Accurate physics](https://github.com/azalea-rs/azalea/blob/main/azalea-physics/src/lib.rs) (but some features like entity collisions and water physics aren't yet implemented)
2020
- [Pathfinder](https://azalea.matdoes.dev/azalea/pathfinder/index.html)
2121
- [Swarms](https://azalea.matdoes.dev/azalea/swarm/index.html)
2222
- [Breaking blocks](https://azalea.matdoes.dev/azalea/struct.Client.html#method.mine)

azalea-client/src/packet_handling/game.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,38 @@ pub fn process_packet_events(ecs: &mut World) {
778778
ClientboundGamePacket::UpdateAttributes(_p) => {
779779
// debug!("Got update attributes packet {p:?}");
780780
}
781-
ClientboundGamePacket::SetEntityMotion(_p) => {
782-
// debug!("Got entity velocity packet {p:?}");
781+
ClientboundGamePacket::SetEntityMotion(p) => {
782+
// vanilla servers use this packet for knockback, but note that the Explode
783+
// packet is also sometimes used by servers for knockback
784+
785+
let mut system_state: SystemState<(
786+
Commands,
787+
Query<(&EntityIdIndex, &InstanceHolder)>,
788+
)> = SystemState::new(ecs);
789+
let (mut commands, mut query) = system_state.get_mut(ecs);
790+
let (entity_id_index, instance_holder) = query.get_mut(player_entity).unwrap();
791+
792+
let Some(entity) = entity_id_index.get(&MinecraftEntityId(p.id)) else {
793+
warn!(
794+
"Got set entity motion packet for unknown entity id {}",
795+
p.id
796+
);
797+
continue;
798+
};
799+
800+
commands.entity(entity).add(RelativeEntityUpdate {
801+
partial_world: instance_holder.partial_instance.clone(),
802+
update: Box::new(move |entity| {
803+
let mut physics = entity.get_mut::<Physics>().unwrap();
804+
physics.delta = Vec3 {
805+
x: p.xa as f64 / 8000.,
806+
y: p.ya as f64 / 8000.,
807+
z: p.za as f64 / 8000.,
808+
};
809+
}),
810+
});
811+
812+
system_state.apply(ecs);
783813
}
784814
ClientboundGamePacket::SetEntityLink(p) => {
785815
debug!("Got set entity link packet {p:?}");
@@ -1154,7 +1184,20 @@ pub fn process_packet_events(ecs: &mut World) {
11541184
ClientboundGamePacket::Cooldown(_) => {}
11551185
ClientboundGamePacket::CustomChatCompletions(_) => {}
11561186
ClientboundGamePacket::DeleteChat(_) => {}
1157-
ClientboundGamePacket::Explode(_) => {}
1187+
ClientboundGamePacket::Explode(p) => {
1188+
trace!("Got explode packet {p:?}");
1189+
let mut system_state: SystemState<Query<&mut Physics>> = SystemState::new(ecs);
1190+
let mut query = system_state.get_mut(ecs);
1191+
let mut physics = query.get_mut(player_entity).unwrap();
1192+
1193+
physics.delta += Vec3 {
1194+
x: p.knockback_x as f64,
1195+
y: p.knockback_y as f64,
1196+
z: p.knockback_z as f64,
1197+
};
1198+
1199+
system_state.apply(ecs);
1200+
}
11581201
ClientboundGamePacket::ForgetLevelChunk(_) => {}
11591202
ClientboundGamePacket::HorseScreenOpen(_) => {}
11601203
ClientboundGamePacket::MapItemData(_) => {}

azalea-entity/src/plugin/indexing.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub struct EntityUuidIndex {
2626
/// An index of Minecraft entity IDs to Azalea ECS entities. This is a
2727
/// `Component` so local players can keep track of entity IDs independently from
2828
/// the instance.
29+
///
30+
/// If you need a per-instance instead of per-client version of this, you can
31+
/// use [`Instance::entity_by_id`].
2932
#[derive(Component, Default)]
3033
pub struct EntityIdIndex {
3134
/// An index of entities by their MinecraftEntityId

0 commit comments

Comments
 (0)