aboutsummaryrefslogtreecommitdiff
path: root/azalea-client/src/plugins/attack.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-04-05 00:50:39 +0330
committermat <git@matdoes.dev>2025-04-05 00:50:39 +0330
commitf250978cdd789d8ceb10cb225970a408933f508b (patch)
tree5cc543750b69b68056fa36249147d7dba584afc8 /azalea-client/src/plugins/attack.rs
parent5fd57fd630bea256639332f117848d6f1fcfd132 (diff)
downloadazalea-drasl-f250978cdd789d8ceb10cb225970a408933f508b.tar.xz
send attack packets at the end of the tick
Diffstat (limited to 'azalea-client/src/plugins/attack.rs')
-rw-r--r--azalea-client/src/plugins/attack.rs56
1 files changed, 40 insertions, 16 deletions
diff --git a/azalea-client/src/plugins/attack.rs b/azalea-client/src/plugins/attack.rs
index 5ba84772..34122108 100644
--- a/azalea-client/src/plugins/attack.rs
+++ b/azalea-client/src/plugins/attack.rs
@@ -33,6 +33,7 @@ impl Plugin for AttackPlugin {
(
increment_ticks_since_last_attack,
update_attack_strength_scale.after(PhysicsSet),
+ handle_attack_queued.before(super::tick_end::game_tick_packet),
)
.chain(),
);
@@ -61,38 +62,45 @@ impl Client {
}
}
-#[derive(Event)]
-pub struct AttackEvent {
- pub entity: Entity,
+/// A component that indicates that this client will be attacking the given
+/// entity next tick.
+#[derive(Component, Clone, Debug)]
+struct AttackQueued {
pub target: MinecraftEntityId,
}
-pub fn handle_attack_event(
- mut events: EventReader<AttackEvent>,
+fn handle_attack_queued(
+ mut commands: Commands,
mut query: Query<(
+ Entity,
+ &AttackQueued,
&LocalGameMode,
&mut TicksSinceLastAttack,
&mut Physics,
&mut Sprinting,
- &mut ShiftKeyDown,
+ &ShiftKeyDown,
)>,
- mut commands: Commands,
- mut swing_arm_event: EventWriter<SwingArmEvent>,
) {
- for event in events.read() {
- let (game_mode, mut ticks_since_last_attack, mut physics, mut sprinting, sneaking) =
- query.get_mut(event.entity).unwrap();
+ for (
+ entity,
+ attack_queued,
+ game_mode,
+ mut ticks_since_last_attack,
+ mut physics,
+ mut sprinting,
+ sneaking,
+ ) in &mut query
+ {
+ commands.entity(entity).remove::<AttackQueued>();
- swing_arm_event.send(SwingArmEvent {
- entity: event.entity,
- });
commands.trigger(SendPacketEvent::new(
- event.entity,
+ entity,
ServerboundInteract {
- entity_id: event.target,
+ entity_id: attack_queued.target,
action: s_interact::ActionType::Attack,
using_secondary_action: **sneaking,
},
));
+ commands.trigger(SwingArmEvent { entity });
// we can't attack if we're in spectator mode but it still sends the attack
// packet
@@ -107,6 +115,22 @@ pub fn handle_attack_event(
}
}
+/// Queues up an attack packet for next tick by inserting the [`AttackQueued`]
+/// component to our client.
+#[derive(Event)]
+pub struct AttackEvent {
+ /// Our client entity that will send the packets to attack.
+ pub entity: Entity,
+ pub target: MinecraftEntityId,
+}
+pub fn handle_attack_event(mut events: EventReader<AttackEvent>, mut commands: Commands) {
+ for event in events.read() {
+ commands.entity(event.entity).insert(AttackQueued {
+ target: event.target,
+ });
+ }
+}
+
#[derive(Default, Bundle)]
pub struct AttackBundle {
pub ticks_since_last_attack: TicksSinceLastAttack,