diff --git a/src/main/kotlin/pl/syntaxdevteam/plotsx/common/ConfigHandler.kt b/src/main/kotlin/pl/syntaxdevteam/plotsx/common/ConfigHandler.kt index 2555a0c..21d4023 100644 --- a/src/main/kotlin/pl/syntaxdevteam/plotsx/common/ConfigHandler.kt +++ b/src/main/kotlin/pl/syntaxdevteam/plotsx/common/ConfigHandler.kt @@ -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") @@ -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 } } diff --git a/src/main/kotlin/pl/syntaxdevteam/plotsx/loader/PluginInitializer.kt b/src/main/kotlin/pl/syntaxdevteam/plotsx/loader/PluginInitializer.kt index 6f1075d..7930390 100644 --- a/src/main/kotlin/pl/syntaxdevteam/plotsx/loader/PluginInitializer.kt +++ b/src/main/kotlin/pl/syntaxdevteam/plotsx/loader/PluginInitializer.kt @@ -46,6 +46,7 @@ class PluginInitializer(private val plugin: PlotsX) { plugin.saveDefaultConfig() plugin.configHandler = ConfigHandler(plugin) plugin.configHandler.verifyAndUpdateConfig() + plugin.configHandler.verifyAndUpdateLanguageFiles() plugin.initializeClaimMode() }