aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data/VanillaItemManager.kt
blob: f2bcd51ca14eab72600e1e07f425478b7304d44c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package at.hannibal2.skyhanni.data

import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import java.io.BufferedReader
import java.io.File
import java.io.FileInputStream
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets

object VanillaItemManager {
    private val gson = GsonBuilder().setPrettyPrinting().create()

    private val vanillaItems: MutableList<String> = ArrayList()

    fun isVanillaItem(internalName: String): Boolean {
        return vanillaItems.contains(internalName)
    }

    init {
        load()
    }

    private fun load() {
        vanillaItems.clear()
        val itemDirectory = File("config/notenoughupdates/repo/items")
        if (!itemDirectory.isDirectory) return
        val files = itemDirectory.listFiles() ?: return
        for (file in files) {
            val jsonObject = getJsonFromFile(file)
            if (jsonObject != null) {
                if (jsonObject.has("vanilla") && jsonObject["vanilla"].asBoolean) {
                    val name = file.name
                    val internalName = name.split(".")[0]
                    vanillaItems.add(internalName)
                }
            }
        }

    }

    private fun getJsonFromFile(file: File): JsonObject? {
        try {
            BufferedReader(
                InputStreamReader(FileInputStream(file), StandardCharsets.UTF_8)
            ).use { reader -> return gson.fromJson(reader, JsonObject::class.java) }
        } catch (e: Exception) {
            return null
        }
    }
}