Newer
Older
Twilight / src / main / kotlin / net / taehui / twilight / system / PlatformIDSystem.kt
package net.taehui.twilight.system

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import net.taehui.twilight.Logger
import java.io.IOException
import java.nio.file.Paths
import java.util.concurrent.ConcurrentHashMap

object PlatformIDSystem : Logger {
    private var platformIDAvatarIDMap = ConcurrentHashMap<String, String>()

    fun savePlatformID() {
        ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(Paths.get("Platform ID.json").toFile().absoluteFile, platformIDAvatarIDMap)
        logInfo("Saved Platform ID")
    }

    fun loadPlatformID() {
        try {
            platformIDAvatarIDMap.putAll(
                ObjectMapper().readValue(Paths.get("Platform ID.json").toFile().absoluteFile,
                    object : TypeReference<ConcurrentHashMap<String, String>>() {})
            )
            logInfo("Loaded Platform ID")
        } catch (e: IOException) {
            logFault(e)
        }
    }

    fun putPlatformID(platformID: String, avatarID: String) {
        platformIDAvatarIDMap[if (platformID.startsWith("$")) platformID else "$$platformID"] = avatarID
    }

    fun wipePlatformID(platformID: String) {
        platformIDAvatarIDMap.remove(if (platformID.startsWith("$")) platformID else "$$platformID")
    }

    fun getAvatarID(platformID: String): String {
        return platformIDAvatarIDMap.getOrDefault(platformID, platformID)
    }

    fun hasAvatarID(platformID: String): Boolean {
        return platformIDAvatarIDMap.containsKey(platformID)
    }
}