diff options
| author | Kumpelinus <kumpelinus@jat.de> | 2025-07-21 22:28:41 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-21 15:28:41 -0500 |
| commit | df9d776ff8e3945ce7d367e6cecb54957ee0fd7a (patch) | |
| tree | aa72f41953fb05e69b660b092d13ecd76e77c1bd /azalea-client/src/plugins | |
| parent | ebc2e0c067d8b2c901ae02e032159e2c80eac7bc (diff) | |
| download | azalea-drasl-df9d776ff8e3945ce7d367e6cecb54957ee0fd7a.tar.xz | |
Add TicksAlive component (#229)
* Add TicksAlive component
* Rename TicksAlive to TicksConnected
* Move component to plugins/tick_counter.rs and add doc comment
Diffstat (limited to 'azalea-client/src/plugins')
| -rw-r--r-- | azalea-client/src/plugins/disconnect.rs | 4 | ||||
| -rw-r--r-- | azalea-client/src/plugins/mod.rs | 2 | ||||
| -rw-r--r-- | azalea-client/src/plugins/packet/game/mod.rs | 18 | ||||
| -rw-r--r-- | azalea-client/src/plugins/tick_counter.rs | 37 |
4 files changed, 45 insertions, 16 deletions
diff --git a/azalea-client/src/plugins/disconnect.rs b/azalea-client/src/plugins/disconnect.rs index 80993476..ab39ba5e 100644 --- a/azalea-client/src/plugins/disconnect.rs +++ b/azalea-client/src/plugins/disconnect.rs @@ -11,7 +11,7 @@ use tracing::info; use super::login::IsAuthenticated; use crate::{ chat_signing, client::JoinedClientBundle, connection::RawConnection, loading::HasClientLoaded, - local_player::InstanceHolder, + local_player::InstanceHolder, tick_counter::TicksConnected, }; pub struct DisconnectPlugin; @@ -72,6 +72,8 @@ pub struct RemoveOnDisconnectBundle { pub is_authenticated: IsAuthenticated, // send ServerboundPlayerLoaded next time we join. pub has_client_loaded: HasClientLoaded, + // TickCounter is reset on reconnect + pub ticks_alive: TicksConnected, } /// A system that removes the several components from our clients when they get diff --git a/azalea-client/src/plugins/mod.rs b/azalea-client/src/plugins/mod.rs index 7c5cd3a3..2319f6fc 100644 --- a/azalea-client/src/plugins/mod.rs +++ b/azalea-client/src/plugins/mod.rs @@ -22,6 +22,7 @@ pub mod pong; pub mod respawn; pub mod task_pool; pub mod tick_broadcast; +pub mod tick_counter; pub mod tick_end; /// This plugin group will add all the default plugins necessary for Azalea to @@ -54,6 +55,7 @@ impl PluginGroup for DefaultPlugins { .add(loading::PlayerLoadedPlugin) .add(brand::BrandPlugin) .add(tick_broadcast::TickBroadcastPlugin) + .add(tick_counter::TickCounterPlugin) .add(pong::PongPlugin) .add(connection::ConnectionPlugin) .add(login::LoginPlugin) diff --git a/azalea-client/src/plugins/packet/game/mod.rs b/azalea-client/src/plugins/packet/game/mod.rs index fd6b712c..b7886208 100644 --- a/azalea-client/src/plugins/packet/game/mod.rs +++ b/azalea-client/src/plugins/packet/game/mod.rs @@ -22,22 +22,9 @@ pub use events::*; use tracing::{debug, error, trace, warn}; use crate::{ - ClientInformation, - block_update::QueuedServerBlockUpdates, - chat::{ChatPacket, ChatReceivedEvent}, - chunks, - connection::RawConnection, - declare_packet_handlers, - disconnect::DisconnectEvent, - interact::BlockStatePredictionHandler, - inventory::{ + block_update::QueuedServerBlockUpdates, chat::{ChatPacket, ChatReceivedEvent}, chunks, connection::RawConnection, declare_packet_handlers, disconnect::DisconnectEvent, interact::BlockStatePredictionHandler, inventory::{ ClientSideCloseContainerEvent, Inventory, MenuOpenedEvent, SetContainerContentEvent, - }, - loading::HasClientLoaded, - local_player::{Hunger, InstanceHolder, LocalGameMode, PlayerAbilities, TabList}, - movement::{KnockbackEvent, KnockbackType}, - packet::as_system, - player::{GameProfileComponent, PlayerInfo}, + }, loading::HasClientLoaded, local_player::{Hunger, InstanceHolder, LocalGameMode, PlayerAbilities, TabList}, movement::{KnockbackEvent, KnockbackType}, packet::as_system, player::{GameProfileComponent, PlayerInfo}, tick_counter::TicksConnected, ClientInformation }; pub fn process_packet(ecs: &mut World, player: Entity, packet: &ClientboundGamePacket) { @@ -299,6 +286,7 @@ impl GamePacketHandler<'_> { previous: p.common.previous_game_type.into(), }, entity_bundle, + TicksConnected(0), )); azalea_entity::indexing::add_entity_to_indexes( diff --git a/azalea-client/src/plugins/tick_counter.rs b/azalea-client/src/plugins/tick_counter.rs new file mode 100644 index 00000000..21515959 --- /dev/null +++ b/azalea-client/src/plugins/tick_counter.rs @@ -0,0 +1,37 @@ +use azalea_core::tick::GameTick; +use azalea_physics::PhysicsSet; +use azalea_world::InstanceName; + +use bevy_app::{App, Plugin}; +use bevy_ecs::prelude::*; + +use crate::{mining::MiningSet, movement::send_position, tick_broadcast::send_tick_broadcast}; + +/// Counts the number of game ticks elapsed on the **local client** since the +/// `login` packet was received. +#[derive(Component, Clone, Debug, Default)] +pub struct TicksConnected(pub u64); + +/// Inserts the counter-increment system into the `GameTick` schedule **before** +/// physics, mining and movement. +pub struct TickCounterPlugin; + +impl Plugin for TickCounterPlugin { + fn build(&self, app: &mut App) { + app.add_systems( + GameTick, + increment_counter + .before(PhysicsSet) + .before(MiningSet) + .before(send_position) + .before(send_tick_broadcast), + ); + } +} + +/// Increment the [`GameTickCounter`] on every entity that lives in an instance. +fn increment_counter(mut query: Query<&mut TicksConnected, With<InstanceName>>) { + for mut counter in &mut query { + counter.0 += 1; + } +} |
