From df9d776ff8e3945ce7d367e6cecb54957ee0fd7a Mon Sep 17 00:00:00 2001 From: Kumpelinus Date: Mon, 21 Jul 2025 22:28:41 +0200 Subject: Add TicksAlive component (#229) * Add TicksAlive component * Rename TicksAlive to TicksConnected * Move component to plugins/tick_counter.rs and add doc comment --- azalea-client/tests/ticks_alive.rs | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 azalea-client/tests/ticks_alive.rs (limited to 'azalea-client/tests') diff --git a/azalea-client/tests/ticks_alive.rs b/azalea-client/tests/ticks_alive.rs new file mode 100644 index 00000000..3be9a09c --- /dev/null +++ b/azalea-client/tests/ticks_alive.rs @@ -0,0 +1,57 @@ +use azalea_client::{test_utils::prelude::*, tick_counter::TicksConnected}; +use azalea_core::resource_location::ResourceLocation; +use azalea_protocol::packets::{config::{ClientboundFinishConfiguration, ClientboundRegistryData}, ConnectionProtocol}; +use azalea_registry::{DataRegistry, DimensionType}; +use simdnbt::owned::{NbtCompound, NbtTag}; + +#[test] +fn counter_increments_and_resets_on_disconnect() { + init_tracing(); + + let mut simulation = Simulation::new(ConnectionProtocol::Configuration); + + simulation.receive_packet(ClientboundRegistryData { + registry_id: ResourceLocation::new("minecraft:dimension_type"), + entries: vec![( + ResourceLocation::new("minecraft:overworld"), + Some(NbtCompound::from_values(vec![ + ("height".into(), NbtTag::Int(384)), + ("min_y".into(), NbtTag::Int(-64)), + ])), + )] + .into_iter() + .collect(), + }); + + simulation.receive_packet(ClientboundFinishConfiguration); + + simulation.tick(); + // we need a second tick to handle the state switch properly + simulation.tick(); + + assert!(!simulation.has_component::()); + + simulation.receive_packet(make_basic_login_packet( + DimensionType::new_raw(0), // overworld + ResourceLocation::new("minecraft:overworld"), + )); + simulation.tick(); + + assert!(simulation.has_component::()); + assert_eq!(simulation.component::().0, 1); + + // Tick three times; counter should read 2, 3, 4. + for expected in 2..=4 { + simulation.tick(); + let counter = simulation.component::(); + assert_eq!( + counter.0, expected, + "after {expected} tick(s) counter should be {expected}" + ); + } + + simulation.disconnect(); + simulation.tick(); + + assert!(!simulation.has_component::()); +} -- cgit v1.2.3