Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 57 additions & 18 deletions src/main/kotlin/pl/syntaxdevteam/plotsx/common/ConfigHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import java.io.File

class ConfigHandler(private val plugin: PlotsX) {

private val bundledLanguages = listOf("pl", "en")

fun verifyAndUpdateConfig() {
val confFile = File(plugin.dataFolder, "config.yml")
val defaultConfStream = plugin.getResource("config.yml")
Expand All @@ -19,28 +21,65 @@ class ConfigHandler(private val plugin: PlotsX) {
val defaultConfig = YamlConfiguration.loadConfiguration(defaultConfStream.reader())
val currentConfig = YamlConfiguration.loadConfiguration(confFile)

var updated = false
if (synchronizeSections(defaultConfig, currentConfig)) {
plugin.logger.success("Updating $confFile file with missing entries.")
currentConfig.save(confFile)
}
}

fun synchronizeSections(defaultSection: ConfigurationSection, currentSection: ConfigurationSection) {
for (key in defaultSection.getKeys(false)) {
if (!currentSection.contains(key)) {
currentSection[key] = defaultSection[key]
updated = true
} else if (defaultSection.isConfigurationSection(key)) {
require(currentSection.isConfigurationSection(key)) { "${currentSection.currentPath}.$key must be a configuration section" }
synchronizeSections(
defaultSection.getConfigurationSection(key)!!,
currentSection.getConfigurationSection(key)!!
)
}
/**
* Uzupełnia wszystkie dostarczane przez plugin pliki językowe, a nie tylko język
* wybrany w config.yml. MessageHandler synchronizuje wyłącznie aktywny plik, więc
* po zmianie języka stary plik mógł nie zawierać wiadomości dodanych w aktualizacji.
*/
fun verifyAndUpdateLanguageFiles() {
bundledLanguages.forEach { language ->
val resourcePath = "lang/messages_$language.yml"
val messageFile = File(plugin.dataFolder, resourcePath)
val defaultStream = plugin.getResource(resourcePath)

if (defaultStream == null) {
plugin.logger.err("Default $resourcePath file not found in plugin resources!")
return@forEach
}
}

synchronizeSections(defaultConfig, currentConfig)
if (!messageFile.exists()) {
messageFile.parentFile.mkdirs()
plugin.saveResource(resourcePath, false)
return@forEach
}

if (updated) {
plugin.logger.success("Updating $confFile file with missing entries.")
currentConfig.save(confFile)
val defaultMessages = defaultStream.reader().use { reader ->
YamlConfiguration.loadConfiguration(reader)
}
val currentMessages = YamlConfiguration.loadConfiguration(messageFile)

if (synchronizeSections(defaultMessages, currentMessages)) {
plugin.logger.success("Updating $messageFile file with missing entries.")
currentMessages.save(messageFile)
}
}
}

private fun synchronizeSections(
defaultSection: ConfigurationSection,
currentSection: ConfigurationSection
): Boolean {
var updated = false
for (key in defaultSection.getKeys(false)) {
if (!currentSection.contains(key)) {
currentSection[key] = defaultSection[key]
updated = true
} else if (defaultSection.isConfigurationSection(key)) {
require(currentSection.isConfigurationSection(key)) {
"${currentSection.currentPath}.$key must be a configuration section"
}
updated = synchronizeSections(
defaultSection.getConfigurationSection(key)!!,
currentSection.getConfigurationSection(key)!!
) || updated
}
}
return updated
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class PluginInitializer(private val plugin: PlotsX) {
plugin.saveDefaultConfig()
plugin.configHandler = ConfigHandler(plugin)
plugin.configHandler.verifyAndUpdateConfig()
plugin.configHandler.verifyAndUpdateLanguageFiles()
plugin.initializeClaimMode()
}

Expand Down
Loading