valence_server_common/
despawn.rs

1use bevy_ecs::prelude::*;
2
3/// A marker [`Component`] for entities that should be despawned at the end of
4/// the tick.
5///
6/// In Valence, some entities such as Minecraft entities must not be removed
7/// from the [`World`] directly. Valence needs an opportunity to perform
8/// deinitialization work while the entity's components still exist.
9///
10/// To resolve this problem, you must give the entities you wish to despawn the
11/// `Despawned` component. At the end of the tick, Valence will despawn all
12/// entities with this component for you.
13///
14/// The `Despawned` component can be used on entities that Valence does not know
15/// about. The entity will be despawned regardless.
16#[derive(Component, Copy, Clone, Default, PartialEq, Eq, Debug)]
17pub struct Despawned;
18
19pub(super) fn despawn_marked_entities(
20    entities: Query<Entity, With<Despawned>>,
21    mut commands: Commands,
22) {
23    for entity in &entities {
24        commands.entity(entity).despawn();
25    }
26}