Add i18n structure

pull/20/head
trivernis 5 years ago
parent f88100be5f
commit 900600714e

@ -2,6 +2,7 @@ package net.trivernis.chunkmaster
import io.papermc.lib.PaperLib import io.papermc.lib.PaperLib
import net.trivernis.chunkmaster.commands.* import net.trivernis.chunkmaster.commands.*
import net.trivernis.chunkmaster.lib.LanguageManager
import net.trivernis.chunkmaster.lib.generation.GenerationManager import net.trivernis.chunkmaster.lib.generation.GenerationManager
import net.trivernis.chunkmaster.lib.SqliteManager import net.trivernis.chunkmaster.lib.SqliteManager
import org.bstats.bukkit.Metrics import org.bstats.bukkit.Metrics
@ -12,6 +13,7 @@ import java.lang.Exception
class Chunkmaster: JavaPlugin() { class Chunkmaster: JavaPlugin() {
lateinit var sqliteManager: SqliteManager lateinit var sqliteManager: SqliteManager
lateinit var generationManager: GenerationManager lateinit var generationManager: GenerationManager
lateinit var langManager: LanguageManager
private lateinit var tpsTask: BukkitTask private lateinit var tpsTask: BukkitTask
var mspt = 20 // keep track of the milliseconds per tick var mspt = 20 // keep track of the milliseconds per tick
private set private set
@ -25,6 +27,8 @@ class Chunkmaster: JavaPlugin() {
val metrics = Metrics(this) val metrics = Metrics(this)
langManager = LanguageManager(this)
langManager.loadProperties()
initDatabase() initDatabase()
generationManager = GenerationManager(this, server) generationManager = GenerationManager(this, server)
generationManager.init() generationManager.init()
@ -70,6 +74,7 @@ class Chunkmaster: JavaPlugin() {
config.addDefault("generation.max-loaded-chunks", 10) config.addDefault("generation.max-loaded-chunks", 10)
config.addDefault("generation.ignore-worldborder", false) config.addDefault("generation.ignore-worldborder", false)
config.addDefault("database.filename", "chunkmaster.db") config.addDefault("database.filename", "chunkmaster.db")
config.addDefault("language", "EN")
config.options().copyDefaults(true) config.options().copyDefaults(true)
saveConfig() saveConfig()
} }

@ -50,7 +50,9 @@ class CmdList(private val chunkmaster: Chunkmaster): Subcommand {
response.append("\n - ").color(ChatColor.WHITE).bold(false) response.append("\n - ").color(ChatColor.WHITE).bold(false)
.append("#${task.id}").color(ChatColor.BLUE).append(" - ").color(ChatColor.WHITE) .append("#${task.id}").color(ChatColor.BLUE).append(" - ").color(ChatColor.WHITE)
.append(genTask.world.name).color(ChatColor.GREEN).append(" - Progress: ").color(ChatColor.WHITE) .append(genTask.world.name).color(ChatColor.GREEN).append(" - Progress: ").color(ChatColor.WHITE)
.append("${genTask.count} chunks").color(ChatColor.BLUE) .append("${"%.1f".format(genTask.count)} chunks").color(ChatColor.BLUE)
.append(", ETA: ").color(ChatColor.WHITE)
.append("")
if (genTask.stopAfter > 0) { if (genTask.stopAfter > 0) {
response.append(" (${(genTask.count.toDouble()/genTask.stopAfter.toDouble())*100}%).") response.append(" (${(genTask.count.toDouble()/genTask.stopAfter.toDouble())*100}%).")

@ -0,0 +1,45 @@
package net.trivernis.chunkmaster.lib
import net.trivernis.chunkmaster.Chunkmaster
import java.io.File
import java.lang.Exception
import java.util.Properties
import java.util.logging.Level
class LanguageManager(private val plugin: Chunkmaster) {
private val langProps = Properties()
private val languageFolder = "${plugin.dataFolder.absolutePath}/i18n"
private var langFileLoaded = false
fun loadProperties() {
val language = plugin.config.getString("language")
val langFile = "$languageFolder/$language.i18n.properties"
val file = File(langFile)
val loader = Thread.currentThread().contextClassLoader
val defaultStream = this.javaClass.getResourceAsStream("/DEFAULT.i18n.properties")
if (defaultStream != null) {
langProps.load(defaultStream)
defaultStream.close()
} else {
plugin.logger.severe("Couldn't load default language properties.")
}
if (file.exists()) {
try {
val inputStream = loader.getResourceAsStream(langFile)
if (inputStream != null) {
langProps.load(inputStream)
langFileLoaded = true
inputStream.close()
}
} catch (e: Exception) {
plugin.logger.warning("Language file $langFile could not be loaded!")
plugin.logger.fine(e.toString())
}
} else {
plugin.logger.warning("Language File $langFile could not be found!")
}
}
fun getLocalized(key: String) {
langProps.getProperty(key)
}
}

@ -47,12 +47,9 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
) )
var id = 0 var id = 0
chunkmaster.sqliteManager.executeStatement( chunkmaster.sqliteManager.executeStatement("""
"""
SELECT id FROM generation_tasks ORDER BY id DESC LIMIT 1 SELECT id FROM generation_tasks ORDER BY id DESC LIMIT 1
""".trimIndent(), """.trimIndent(), HashMap()) {
HashMap()
) {
it.next() it.next()
id = it.getInt("id") id = it.getInt("id")
} }
@ -114,11 +111,9 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
} }
if (taskEntry != null) { if (taskEntry != null) {
taskEntry.cancel() taskEntry.cancel()
chunkmaster.sqliteManager.executeStatement( chunkmaster.sqliteManager.executeStatement("""
"""
DELETE FROM generation_tasks WHERE id = ?; DELETE FROM generation_tasks WHERE id = ?;
""".trimIndent(), """.trimIndent(), HashMap(mapOf(1 to taskEntry.id)),
HashMap(mapOf(1 to taskEntry.id)),
null null
) )
@ -171,8 +166,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server
* Starts all generation tasks. * Starts all generation tasks.
*/ */
fun startAll() { fun startAll() {
chunkmaster.sqliteManager.executeStatement("SELECT * FROM generation_tasks", HashMap()) { chunkmaster.sqliteManager.executeStatement("SELECT * FROM generation_tasks", HashMap()) { res ->
val res = it
while (res.next()) { while (res.next()) {
try { try {
val id = res.getInt("id") val id = res.getInt("id")

Loading…
Cancel
Save