1+ package io.github.grassproject.framework.config
2+
3+ import io.github.grassproject.framework.core.GPPlugin
4+ import org.bukkit.configuration.file.FileConfiguration
5+ import org.bukkit.configuration.file.YamlConfiguration
6+ import java.io.File
7+
8+ fun GPPlugin.saveResource (resource : String , file : File ) {
9+ if (file.exists()) return
10+ file.parentFile.mkdirs()
11+
12+ val inputStream = getResource(resource)
13+ // ?: throw IllegalArgumentException("Resource '$resource' not found in plugin JAR!")
14+
15+ if (inputStream != null ) {
16+ file.outputStream().use { output ->
17+ inputStream.use { input ->
18+ input.copyTo(output)
19+ }
20+ }
21+ } else {
22+ file.createNewFile()
23+ }
24+ }
25+
26+ fun GPPlugin.init (vararg configs : GPFile ) {
27+ configs.forEach { yaml ->
28+ val file = File (dataFolder, yaml.name)
29+ if (! file.exists()) saveResource(yaml.name, file)
30+
31+ yaml.reload(file)
32+ }
33+ }
34+
35+ abstract class Config {
36+
37+ abstract var config: FileConfiguration
38+ open var autoSave: Boolean = true
39+
40+ open operator fun contains (key : String ) = config.contains(key)
41+ open operator fun get (key : String ) = config[key]
42+ open operator fun set (key : String , value : Any? ) {
43+ config.set(key, value)
44+ if (autoSave) save()
45+ }
46+
47+ open val sections: Set <String > get() = config.getKeys(false )
48+ open fun section (path : String ) = config.getConfigurationSection(path)
49+ open fun keys (path : String , deep : Boolean = false) = section(path)?.getKeys(deep) ? : emptySet()
50+
51+ open fun list (path : String ) = config.getList(path).orEmpty()
52+ open fun string (path : String , def : String = "") = config.getString(path, def)!!
53+ open fun stringList (path : String ) = config.getStringList(path)
54+ open fun boolean (path : String , def : Boolean = false) = config.getBoolean(path, def)
55+ open fun booleanList (path : String ) = config.getBooleanList(path)
56+ open fun int (path : String , def : Int = 0) = config.getInt(path, def)
57+ open fun intList (path : String ) = config.getIntegerList(path)
58+ open fun long (path : String , def : Long = 0L) = config.getLong(path, def)
59+ open fun longList (path : String ) = config.getLongList(path)
60+ open fun double (path : String , def : Double = 0.0) = config.getDouble(path, def)
61+ open fun doubleList (path : String ) = config.getDoubleList(path)
62+
63+ inline fun <reified T : Enum <T >> enum (path : String , def : T ): T {
64+ val str = string(path, def.name)
65+ return runCatching { enumValueOf<T >(str.uppercase()) }.getOrDefault(def)
66+ }
67+
68+ fun <T : Enum <T >> setEnum (path : String , value : T ) = set(path, value.name)
69+
70+ open fun save () {}
71+ }
72+
73+ open class ConfigFile (open val file : File ) : Config() {
74+ override var config: FileConfiguration = YamlConfiguration .loadConfiguration(file)
75+ set(value) {
76+ field = value
77+ save()
78+ }
79+
80+ override fun save () {
81+ config.save(file)
82+ }
83+
84+ fun reload (file : File ? = null) {
85+ config = if (file != null ) YamlConfiguration .loadConfiguration(file)
86+ else YamlConfiguration .loadConfiguration(this .file)
87+ }
88+ }
89+
90+ open class GPFile (pluginFolder : File , open var name : String ) :
91+ ConfigFile (File (pluginFolder, name)) {
92+
93+ constructor (pluginFolder: File , name: String , autoCreate: Boolean = true ) : this (pluginFolder, name) {
94+ if (autoCreate && ! file.exists()) {
95+ file.parentFile.mkdirs()
96+ file.createNewFile()
97+ }
98+ }
99+ }
0 commit comments