azalea_client/plugins/
tick_counter.rs

1use azalea_core::tick::GameTick;
2use azalea_physics::PhysicsSet;
3use azalea_world::InstanceName;
4use bevy_app::{App, Plugin};
5use bevy_ecs::prelude::*;
6
7use crate::{mining::MiningSet, movement::send_position, tick_broadcast::send_tick_broadcast};
8
9/// Counts the number of game ticks elapsed on the **local client** since the
10/// `login` packet was received.
11#[derive(Component, Clone, Debug, Default)]
12pub struct TicksConnected(pub u64);
13
14/// Inserts the counter-increment system into the `GameTick` schedule **before**
15/// physics, mining and movement.
16pub struct TickCounterPlugin;
17
18impl Plugin for TickCounterPlugin {
19    fn build(&self, app: &mut App) {
20        app.add_systems(
21            GameTick,
22            increment_counter
23                .before(PhysicsSet)
24                .before(MiningSet)
25                .before(send_position)
26                .before(send_tick_broadcast),
27        );
28    }
29}
30
31/// Increment the [`GameTickCounter`] on every entity that lives in an instance.
32fn increment_counter(mut query: Query<&mut TicksConnected, With<InstanceName>>) {
33    for mut counter in &mut query {
34        counter.0 += 1;
35    }
36}