Skip to content

Instantly share code, notes, and snippets.

@Proximyst
Created January 27, 2017 17:37
Show Gist options
  • Save Proximyst/aeed18bd91f84344286d82230bb0cc72 to your computer and use it in GitHub Desktop.
Save Proximyst/aeed18bd91f84344286d82230bb0cc72 to your computer and use it in GitHub Desktop.
package com.proximyst.allbut
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import org.bukkit.plugin.Plugin
import java.io.File
import java.io.FileReader
import java.nio.file.Files
import java.nio.file.StandardOpenOption
import java.util.*
open class JsonConfig (cfile: File, cmain: Plugin) {
private val main = cmain
val file = cfile
val config: MutableMap<String, Any> = HashMap()
constructor(name: String, main: Plugin) : this(File(main.dataFolder, name), main)
init {
reload()
}
fun reload() {
if (!file.exists())
main.saveResource(file.name, false)
config.putAll(prettyGson.fromJson(FileReader(file), config.javaClass))
}
fun save() {
val json = prettyGson.toJson(config)
file.delete()
Files.write(file.toPath(), json.toByteArray(), StandardOpenOption.CREATE, StandardOpenOption.WRITE)
}
}
val prettyGson: Gson = GsonBuilder().setPrettyPrinting().create()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment