From 935a6b9ddde80d95686d863f671c3842c4b5e203 Mon Sep 17 00:00:00 2001 From: KingsDev <63374258+KingsMMA@users.noreply.github.com> Date: Fri, 6 Dec 2024 01:13:10 +1000 Subject: [PATCH] Use firstOrNull in NameCacheService to not throw for uncached players --- .../gg/flyte/twilight/data/service/NameCacheService.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/gg/flyte/twilight/data/service/NameCacheService.kt b/src/main/kotlin/gg/flyte/twilight/data/service/NameCacheService.kt index fd4bec6..7779a49 100644 --- a/src/main/kotlin/gg/flyte/twilight/data/service/NameCacheService.kt +++ b/src/main/kotlin/gg/flyte/twilight/data/service/NameCacheService.kt @@ -38,14 +38,15 @@ object NameCacheService { private fun queryMongoNameByUUID(uuid: UUID): String? { mongoCache.let { - mongoCache.find(Filters.eq("_id", uuid.toString())).first() - .let { + mongoCache.find(Filters.eq("_id", uuid.toString())).firstOrNull() + ?.let { val name = it.getString("name") ?: throw MongoException("Document with '_id' '$uuid' has no field 'name'.") cache[uuid] = name return name } } + return null } private fun queryMojangNameByUUID(uuid: UUID): String { @@ -78,13 +79,14 @@ object NameCacheService { "name", Pattern.compile("^$name$", Pattern.CASE_INSENSITIVE) ) - ).first().let { + ).firstOrNull()?.let { val uuid = UUID.fromString(it.getString("_id")) ?: throw MongoException("Document with 'name' '$name' has no valid UUID at '_id'.") cache[uuid] = name return uuid } } + return null } private fun queryMojangUUIDByName(name: String): UUID {