Config cleanup

This commit is contained in:
Auxilor
2022-03-23 16:24:17 +00:00
parent 7a521acccf
commit 41b9d6b01d
6 changed files with 28 additions and 30 deletions

View File

@@ -9,6 +9,8 @@ import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.LoaderOptions
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.representer.Representer
import java.io.BufferedReader
import java.io.Reader
fun ConfigType.toMap(input: String?): Map<String, Any?> =
this.handler.toMap(input)
@@ -16,9 +18,6 @@ fun ConfigType.toMap(input: String?): Map<String, Any?> =
fun ConfigType.toString(map: Map<String, Any?>): String =
this.handler.toString(map)
private val ConfigType.handler: ConfigTypeHandler
get() = if (this == ConfigType.JSON) JSONConfigTypeHandler else YamlConfigTypeHandler
fun Map<*, *>.ensureTypesForConfig(): Map<String, Any?> {
val building = mutableMapOf<String, Any?>()
@@ -41,6 +40,24 @@ fun Map<*, *>.ensureTypesForConfig(): Map<String, Any?> {
return building
}
fun Reader.readToString(): String {
val input = this as? BufferedReader ?: BufferedReader(this)
val builder = StringBuilder()
var line: String?
input.use {
while (it.readLine().also { read -> line = read } != null) {
builder.append(line)
builder.append('\n')
}
}
return builder.toString()
}
private val ConfigType.handler: ConfigTypeHandler
get() = if (this == ConfigType.JSON) JSONConfigTypeHandler else YamlConfigTypeHandler
private abstract class ConfigTypeHandler(
val type: ConfigType
) {

View File

@@ -6,8 +6,6 @@ import com.willfp.eco.core.config.interfaces.Config
import com.willfp.eco.core.config.interfaces.LoadableConfig
import com.willfp.eco.core.config.wrapper.ConfigFactory
import org.bukkit.configuration.ConfigurationSection
import java.io.BufferedReader
import java.io.Reader
object EcoConfigFactory : ConfigFactory {
override fun createConfig(config: ConfigurationSection): Config =
@@ -51,18 +49,3 @@ object EcoConfigFactory : ConfigFactory {
*updateBlacklist
)
}
fun Reader.readToString(): String {
val input = this as? BufferedReader ?: BufferedReader(this)
val builder = StringBuilder()
var line: String?
input.use {
while (it.readLine().also { read -> line = read } != null) {
builder.append(line)
builder.append('\n')
}
}
return builder.toString()
}

View File

@@ -1,11 +1,9 @@
package com.willfp.eco.internal.config.updating
package com.willfp.eco.internal.config
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.config.interfaces.LoadableConfig
import com.willfp.eco.core.config.updating.ConfigHandler
import com.willfp.eco.core.config.updating.ConfigUpdater
import com.willfp.eco.internal.config.EcoLoadableConfig
import com.willfp.eco.internal.config.EcoUpdatableConfig
import org.reflections.Reflections
import org.reflections.scanners.MethodAnnotationsScanner
@@ -53,3 +51,5 @@ class EcoConfigHandler(
}
}
}
class InvalidUpdateMethodException(message: String) : RuntimeException(message)

View File

@@ -58,7 +58,7 @@ open class EcoLoadableConfig(
@Throws(IOException::class)
override fun save() {
if (!hasChanged) { // In order to preserve comments
return
//return
}
val contents = StringBuilder()
@@ -100,8 +100,9 @@ open class EcoLoadableConfig(
}
protected fun init(reader: Reader) {
makeHeader(reader.readToString())
super.init(type.toMap(reader.readToString()))
val string = reader.readToString()
makeHeader(string)
super.init(type.toMap(string))
}
fun init(file: File) {

View File

@@ -1,3 +0,0 @@
package com.willfp.eco.internal.config.updating
class InvalidUpdateMethodException(message: String) : RuntimeException(message)

View File

@@ -10,7 +10,7 @@ import com.willfp.eco.internal.EcoCleaner
import com.willfp.eco.internal.EcoPropsParser
import com.willfp.eco.internal.Plugins
import com.willfp.eco.internal.config.EcoConfigFactory
import com.willfp.eco.internal.config.updating.EcoConfigHandler
import com.willfp.eco.internal.config.EcoConfigHandler
import com.willfp.eco.internal.drops.EcoDropQueueFactory
import com.willfp.eco.internal.events.EcoEventManager
import com.willfp.eco.internal.extensions.EcoExtensionLoader