From cf66c4be100c898c64d57f9b92f31f809764dc2e Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 6 Mar 2025 04:11:05 +0000 Subject: fix despawning entities on dimension change --- .../despawn_entities_when_changing_dimension.rs | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 azalea-client/tests/despawn_entities_when_changing_dimension.rs (limited to 'azalea-client/tests') diff --git a/azalea-client/tests/despawn_entities_when_changing_dimension.rs b/azalea-client/tests/despawn_entities_when_changing_dimension.rs new file mode 100644 index 00000000..39cea091 --- /dev/null +++ b/azalea-client/tests/despawn_entities_when_changing_dimension.rs @@ -0,0 +1,97 @@ +use azalea_client::{InConfigState, InGameState, test_simulation::*}; +use azalea_core::{ + delta::PositionDelta8, + position::{ChunkPos, Vec3}, + resource_location::ResourceLocation, +}; +use azalea_entity::{LocalEntity, metadata::Cow}; +use azalea_protocol::packets::{ + ConnectionProtocol, + config::{ClientboundFinishConfiguration, ClientboundRegistryData}, + game::ClientboundAddEntity, +}; +use azalea_registry::DimensionType; +use azalea_world::InstanceName; +use bevy_ecs::{entity::Entity, query::With}; +use bevy_log::tracing_subscriber; +use simdnbt::owned::{NbtCompound, NbtTag}; +use uuid::Uuid; + +#[test] +fn test_despawn_entities_when_changing_dimension() { + let _ = tracing_subscriber::fmt::try_init(); + + 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)), + ])), + ), + ( + ResourceLocation::new("minecraft:nether"), + Some(NbtCompound::from_values(vec![ + ("height".into(), NbtTag::Int(256)), + ("min_y".into(), NbtTag::Int(0)), + ])), + ), + ] + .into_iter() + .collect(), + }); + simulation.tick(); + simulation.receive_packet(ClientboundFinishConfiguration); + simulation.tick(); + + // + // OVERWORLD + // + + simulation.receive_packet(make_basic_login_packet( + DimensionType::new_raw(0), // overworld + ResourceLocation::new("azalea:a"), + )); + simulation.tick(); + + simulation.receive_packet(make_basic_empty_chunk(ChunkPos::new(0, 0), (384 + 64) / 16)); + simulation.tick(); + // spawn a cow + simulation.receive_packet(ClientboundAddEntity { + id: 123.into(), + uuid: Uuid::from_u128(1234), + entity_type: azalea_registry::EntityKind::Cow, + position: Vec3::new(0., 64., 0.), + x_rot: 0, + y_rot: 0, + y_head_rot: 0, + data: 0, + velocity: PositionDelta8::default(), + }); + simulation.tick(); + // make sure it's spawned + let mut cow_query = simulation.app.world_mut().query_filtered::<(), With>(); + let cow_iter = cow_query.iter(simulation.app.world()); + assert_eq!(cow_iter.count(), 1, "cow should be spawned"); + + // + // NETHER + // + + simulation.receive_packet(make_basic_respawn_packet( + DimensionType::new_raw(1), // nether + ResourceLocation::new("azalea:b"), + )); + simulation.tick(); + + // cow should be completely deleted from the ecs + let cow_iter = cow_query.iter(simulation.app.world()); + assert_eq!( + cow_iter.count(), + 0, + "cow should be despawned after switching dimensions" + ); +} -- cgit v1.2.3