Skip to content
This repository has been archived by the owner on May 29, 2021. It is now read-only.

Commit

Permalink
[cleanup] UUID cache saving (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna5ama committed Jan 7, 2021
1 parent bb0310f commit 0b7254b
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions org/kamiblue/capeapi/AbstractUUIDManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import com.google.gson.JsonParser
import com.google.gson.reflect.TypeToken
import org.apache.logging.log4j.Logger
import org.kamiblue.commons.utils.ConnectionUtils
import java.io.*
import java.io.File
import java.io.FileWriter
import java.util.*
import kotlin.collections.LinkedHashMap

Expand All @@ -19,6 +20,8 @@ abstract class AbstractUUIDManager(
@Suppress("DEPRECATION")
private val parser = JsonParser()
private val gson = GsonBuilder().setPrettyPrinting().create()
private val type = TypeToken.getArray(PlayerProfile::class.java).type

private val nameProfileMap = Collections.synchronizedMap(LinkedHashMap<String, PlayerProfile>())
private val uuidNameMap = Collections.synchronizedMap(LinkedHashMap<UUID, PlayerProfile>())

Expand Down Expand Up @@ -107,9 +110,10 @@ abstract class AbstractUUIDManager(

fun load(): Boolean {
fixEmptyJson(file)
val reader = BufferedReader(FileReader(file))
return try {
val cacheList = gson.fromJson<List<PlayerProfile>>(reader, object : TypeToken<List<PlayerProfile>>() {}.type)
val cacheList = file.bufferedReader().use {
gson.fromJson<Array<PlayerProfile>>(it, type)
}
uuidNameMap.clear()
nameProfileMap.clear()
uuidNameMap.putAll(cacheList.associateBy { it.uuid })
Expand All @@ -119,24 +123,20 @@ abstract class AbstractUUIDManager(
} catch (e: Exception) {
logger.warn("Failed loading UUID cache", e)
false
} finally {
reader.close()
}
}

fun save(): Boolean {
val writer = BufferedWriter(FileWriter(file, false))
return try {
val cacheList = uuidNameMap.values.sortedBy { it.name }
gson.toJson(cacheList, writer)
file.bufferedWriter().use {
gson.toJson(cacheList, it)
}
logger.info("UUID cache saved")
true
} catch (e: Exception) {
logger.warn("Failed saving UUID cache", e)
false
} finally {
writer.flush()
writer.close()
}
}

Expand Down

0 comments on commit 0b7254b

Please sign in to comment.