1use std::{
4 collections::{HashMap, HashSet},
5 fmt::{self, Debug},
6};
7
8use azalea_core::{entity_id::MinecraftEntityId, position::ChunkPos};
9use azalea_world::{World, WorldName, Worlds};
10use bevy_ecs::prelude::*;
11use derive_more::{Deref, DerefMut};
12use nohash_hasher::IntMap;
13use tracing::{debug, trace, warn};
14use uuid::Uuid;
15
16use super::LoadedBy;
17use crate::{EntityUuid, LocalEntity, Position};
18
19#[derive(Default, Resource)]
20pub struct EntityUuidIndex {
21 entity_by_uuid: HashMap<Uuid, Entity>,
23}
24impl EntityUuidIndex {
25 pub fn new() -> Self {
26 Self {
27 entity_by_uuid: HashMap::default(),
28 }
29 }
30
31 pub fn get(&self, uuid: &Uuid) -> Option<Entity> {
32 self.entity_by_uuid.get(uuid).copied()
33 }
34
35 pub fn contains_key(&self, uuid: &Uuid) -> bool {
36 self.entity_by_uuid.contains_key(uuid)
37 }
38
39 pub fn insert(&mut self, uuid: Uuid, entity: Entity) {
40 self.entity_by_uuid.insert(uuid, entity);
41 }
42
43 pub fn remove(&mut self, uuid: &Uuid) -> Option<Entity> {
44 self.entity_by_uuid.remove(uuid)
45 }
46}
47
48#[derive(Component, Default)]
56pub struct EntityIdIndex {
57 entity_by_id: IntMap<MinecraftEntityId, Entity>,
59 id_by_entity: HashMap<Entity, MinecraftEntityId>,
60}
61
62impl EntityIdIndex {
63 pub fn get_by_minecraft_entity(&self, id: MinecraftEntityId) -> Option<Entity> {
64 self.entity_by_id.get(&id).copied()
65 }
66 pub fn get_by_ecs_entity(&self, entity: Entity) -> Option<MinecraftEntityId> {
67 self.id_by_entity.get(&entity).copied()
68 }
69
70 pub fn contains_minecraft_entity(&self, id: MinecraftEntityId) -> bool {
71 self.entity_by_id.contains_key(&id)
72 }
73 pub fn contains_ecs_entity(&self, id: Entity) -> bool {
74 self.id_by_entity.contains_key(&id)
75 }
76
77 pub fn insert(&mut self, id: MinecraftEntityId, entity: Entity) {
78 self.entity_by_id.insert(id, entity);
79 self.id_by_entity.insert(entity, id);
80 trace!("Inserted {id} -> {entity:?} into a client's EntityIdIndex");
81 }
82
83 pub fn remove_by_minecraft_entity(&mut self, id: MinecraftEntityId) -> Option<Entity> {
84 if let Some(entity) = self.entity_by_id.remove(&id) {
85 trace!(
86 "Removed {id} -> {entity:?} from a client's EntityIdIndex (using EntityIdIndex::remove)"
87 );
88 self.id_by_entity.remove(&entity);
89 Some(entity)
90 } else {
91 trace!(
92 "Failed to remove {id} from a client's EntityIdIndex (using EntityIdIndex::remove)"
93 );
94 None
95 }
96 }
97
98 pub fn remove_by_ecs_entity(&mut self, entity: Entity) -> Option<MinecraftEntityId> {
99 if let Some(id) = self.id_by_entity.remove(&entity) {
100 trace!(
101 "Removed {id} -> {entity:?} from a client's EntityIdIndex (using EntityIdIndex::remove_by_ecs_entity)."
102 );
103 self.entity_by_id.remove(&id);
104 Some(id)
105 } else {
106 trace!(
111 "Failed to remove {entity:?} from a client's EntityIdIndex (using EntityIdIndex::remove_by_ecs_entity). This may be expected behavior."
112 );
113 None
114 }
115 }
116}
117
118impl Debug for EntityUuidIndex {
119 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120 f.debug_struct("EntityUuidIndex").finish()
121 }
122}
123
124#[derive(Component, Debug, Deref, DerefMut)]
126pub struct EntityChunkPos(pub ChunkPos);
127
128pub fn update_entity_chunk_positions(
132 mut query: Query<(Entity, &Position, &WorldName, &mut EntityChunkPos), Changed<Position>>,
133 worlds: Res<Worlds>,
134) {
135 for (entity, pos, world_name, mut entity_chunk_pos) in query.iter_mut() {
136 let old_chunk = **entity_chunk_pos;
137 let new_chunk = ChunkPos::from(*pos);
138 if old_chunk != new_chunk {
139 **entity_chunk_pos = new_chunk;
140
141 if old_chunk != new_chunk {
142 let Some(world_lock) = worlds.get(world_name) else {
143 continue;
144 };
145 let mut world = world_lock.write();
146
147 if let Some(entities) = world.entities_by_chunk.get_mut(&old_chunk) {
149 entities.remove(&entity);
150 }
151 world
152 .entities_by_chunk
153 .entry(new_chunk)
154 .or_default()
155 .insert(entity);
156 trace!("Entity {entity:?} moved from {old_chunk:?} to {new_chunk:?}");
157 }
158 }
159 }
160}
161
162pub fn insert_entity_chunk_position(
164 query: Query<(Entity, &Position, &WorldName), Added<EntityChunkPos>>,
165 worlds: Res<Worlds>,
166) {
167 for (entity, pos, world_name) in query.iter() {
168 let Some(world_lock) = worlds.get(world_name) else {
169 continue;
171 };
172 let mut world = world_lock.write();
173
174 let chunk = ChunkPos::from(*pos);
175 world
176 .entities_by_chunk
177 .entry(chunk)
178 .or_default()
179 .insert(entity);
180 }
181}
182
183#[allow(clippy::type_complexity)]
185pub fn remove_despawned_entities_from_indexes(
186 mut commands: Commands,
187 mut entity_uuid_index: ResMut<EntityUuidIndex>,
188 worlds: Res<Worlds>,
189 query: Query<
190 (
191 Entity,
192 &EntityUuid,
193 &MinecraftEntityId,
194 &Position,
195 &WorldName,
196 &LoadedBy,
197 ),
198 (Changed<LoadedBy>, Without<LocalEntity>),
199 >,
200 mut entity_id_index_query: Query<&mut EntityIdIndex>,
201) {
202 for (entity, uuid, minecraft_id, position, world_name, loaded_by) in &query {
203 let Some(world_lock) = worlds.get(world_name) else {
204 debug!("Despawned entity {entity:?} because it's in a world that isn't loaded anymore");
206 if entity_uuid_index.entity_by_uuid.remove(uuid).is_none() {
207 warn!(
208 "Tried to remove entity {entity:?} from the uuid index but it was not there."
209 );
210 }
211 commands.entity(entity).despawn();
213
214 continue;
215 };
216
217 let mut world = world_lock.write();
218
219 if !loaded_by.is_empty() {
221 continue;
222 }
223
224 let chunk = ChunkPos::from(position);
226 match world.entities_by_chunk.get_mut(&chunk) {
227 Some(entities_in_chunk) => {
228 if entities_in_chunk.remove(&entity) {
229 if entities_in_chunk.is_empty() {
231 world.entities_by_chunk.remove(&chunk);
232 }
233 } else {
234 let mut found_in_other_chunks = HashSet::new();
236 for (other_chunk, entities_in_other_chunk) in &mut world.entities_by_chunk {
237 if entities_in_other_chunk.remove(&entity) {
238 found_in_other_chunks.insert(other_chunk);
239 }
240 }
241 if found_in_other_chunks.is_empty() {
242 warn!(
243 "Tried to remove entity {entity:?} from chunk {chunk:?} but the entity was not there or in any other chunks."
244 );
245 } else {
246 warn!(
247 "Tried to remove entity {entity:?} from chunk {chunk:?} but the entity was not there. Found in and removed from other chunk(s): {found_in_other_chunks:?}"
248 );
249 }
250 }
251 }
252 _ => {
253 let mut found_in_other_chunks = HashSet::new();
254 for (other_chunk, entities_in_other_chunk) in &mut world.entities_by_chunk {
255 if entities_in_other_chunk.remove(&entity) {
256 found_in_other_chunks.insert(other_chunk);
257 }
258 }
259 if found_in_other_chunks.is_empty() {
260 warn!(
261 "Tried to remove entity {entity:?} from chunk {chunk:?} but the chunk was not found and the entity wasn't in any other chunks."
262 );
263 } else {
264 warn!(
265 "Tried to remove entity {entity:?} from chunk {chunk:?} but the chunk was not found. Entity found in and removed from other chunk(s): {found_in_other_chunks:?}"
266 );
267 }
268 }
269 }
270 if entity_uuid_index.entity_by_uuid.remove(uuid).is_none() {
272 warn!("Tried to remove entity {entity:?} from the uuid index but it was not there.");
273 }
274 if world.entity_by_id.remove(minecraft_id).is_none() {
275 debug!(
276 "Tried to remove entity {entity:?} from the per-world entity id index but it was not there. This may be expected if you're in a shared world."
277 );
278 }
279
280 for mut entity_id_index in entity_id_index_query.iter_mut() {
282 entity_id_index.remove_by_ecs_entity(entity);
283 }
284
285 commands.entity(entity).despawn();
287 debug!("Despawned entity {entity:?} because it was not loaded by anything.");
288 }
289}
290
291pub fn add_entity_to_indexes(
292 entity_id: MinecraftEntityId,
293 ecs_entity: Entity,
294 entity_uuid: Option<Uuid>,
295 entity_id_index: &mut EntityIdIndex,
296 entity_uuid_index: &mut EntityUuidIndex,
297 world: &mut World,
298) {
299 entity_id_index.insert(entity_id, ecs_entity);
301
302 world.entity_by_id.insert(entity_id, ecs_entity);
304
305 if let Some(uuid) = entity_uuid {
306 entity_uuid_index.insert(uuid, ecs_entity);
308 }
309}