This mod maintains a dynamic <DimensionID, Map<ChunkPosition, Set<EntityUUID>>> map in loading entities lifecycle, which is an alternative of Level#getEntitiesOfClass that help collect entities.
EntitiesInChunkData#ENTITIES is the core data:
public static final Map<ResourceLocation, Map<ChunkPos, Set<UUID>>> ENTITIES = new ConcurrentHashMap<>();
removeEntity and addEntity operations won't execute until a server tick ends.