Skip to content

Commit

Permalink
fix: touch and reset may not work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Gk0Wk committed Aug 10, 2023
1 parent 041b85c commit 8391942
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ idea {

val targetJavaVersion = 8

version = "2.1.0"
version = "2.1.1"
group = "city.newnan.violet"
description = "Useful toolkits java library for Bukkit Server Plugin."

Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/city/newnan/violet/config/ConfigManager2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,13 @@ class ConfigManager2
* @throws UnknownConfigFileFormatException 未知的配置文件格式
*/
@Throws(IOException::class, UnknownConfigFileFormatException::class)
fun touch(targetFile: String, templateValue: Any, type: ConfigFileType? = null, overrideExist: Boolean = false): Boolean {
fun touch(targetFile: String, templateValue: () -> Any, type: ConfigFileType? = null, overrideExist: Boolean = false): Boolean {
val file = File(plugin.dataFolder, targetFile)
// 如果文件不存在
if (overrideExist || !file.exists()) {
// 检查父目录
if (!file.parentFile.exists()) file.parentFile.mkdirs()
save(templateValue, file, type)
save(templateValue(), file, type)
return false
}
return true
Expand All @@ -408,7 +408,7 @@ class ConfigManager2
templateFile: String = configFile,
saveToCache: Boolean = true,
useCacheIfPossible: Boolean = true,
templateData: Any? = null): Configure2 {
templateData: (() -> Any)? = null): Configure2 {
// 查找缓存
val path = File(plugin.dataFolder, configFile)
val cacheKey = Pair(path.canonicalPath, configure2TypeReferenceString)
Expand Down Expand Up @@ -533,8 +533,8 @@ class ConfigManager2
* @throws UnknownConfigFileFormatException 未知的配置文件格式
*/
@Throws(IOException::class, UnknownConfigFileFormatException::class)
fun reset(configFile: String, templateFile: String = configFile, templateData: Any? = null, type: ConfigFileType? = null) {
if(templateData != null) touch(configFile, templateData) else touch(configFile, templateFile, type)
fun reset(configFile: String, templateFile: String = configFile, templateData: (() -> Any)? = null, type: ConfigFileType? = null) {
if(templateData != null) touch(configFile, templateData, type, true) else touch(configFile, templateFile, true)
}


Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/city/newnan/violet/i18n/Language.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package city.newnan.violet.i18n

import city.newnan.violet.config.ConfigManager2
import com.fasterxml.jackson.databind.node.ObjectNode
import org.bukkit.plugin.Plugin
import java.io.File
import java.io.IOException
import java.util.*
Expand Down
21 changes: 18 additions & 3 deletions src/test/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import city.newnan.violet.config.ConfigManager2
import city.newnan.violet.config.Configure2
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.databind.deser.std.DateDeserializers
import com.fasterxml.jackson.databind.ser.std.DateSerializer
import java.util.*

data class A(
@JsonSerialize(using = DateSerializer::class)
@JsonDeserialize(using = DateDeserializers.TimestampDeserializer::class)
val date: Date
)

fun main() {
println(object : TypeReference<Configure2>() {}.type.toString())
println(object : TypeReference<HashMap<String, Configure2>>() {}.type.toString())
println(object : TypeReference<HashMap<String, HashMap<String, HashMap<String, Configure2>>>>() {}.type.toString())
val a = A(Date())
val s = ConfigManager2.stringify(a, ConfigManager2.ConfigFileType.Yaml)
println(s)
val b = ConfigManager2.parse<A>(s, ConfigManager2.ConfigFileType.Yaml)
println(a)
println(b)
}

0 comments on commit 8391942

Please sign in to comment.