Develop (#88)
Merge develop into master * Update Translation to 1.3.0 (#86) * Update Translation to 1.3.0 * Tweak SQL_ERROR word order Co-authored-by: NPBeta <shanhang007@gmail.com> * Update version to 1.3.1 * Remove empty body of PausedTaskEntry Co-authored-by: NPBeta <shanhang007@gmail.com>pull/105/head v1.3.1
parent
4d4107aaf3
commit
82662bc4d3
@ -1,170 +1,189 @@
|
|||||||
package net.trivernis.chunkmaster.lib.database
|
package net.trivernis.chunkmaster.lib.database
|
||||||
|
|
||||||
import net.trivernis.chunkmaster.Chunkmaster
|
import net.trivernis.chunkmaster.Chunkmaster
|
||||||
import org.apache.commons.lang.exception.ExceptionUtils
|
import org.apache.commons.lang.exception.ExceptionUtils
|
||||||
import java.lang.Exception
|
import java.sql.Connection
|
||||||
import java.sql.Connection
|
import java.sql.DriverManager
|
||||||
import java.sql.DriverManager
|
import java.sql.ResultSet
|
||||||
import java.sql.ResultSet
|
|
||||||
|
class SqliteManager(private val chunkmaster: Chunkmaster) {
|
||||||
class SqliteManager(private val chunkmaster: Chunkmaster) {
|
private val tables = listOf(
|
||||||
private val tables = listOf(
|
Pair(
|
||||||
Pair(
|
"generation_tasks",
|
||||||
"generation_tasks",
|
listOf(
|
||||||
listOf(
|
Pair("id", "integer PRIMARY KEY AUTOINCREMENT"),
|
||||||
Pair("id", "integer PRIMARY KEY AUTOINCREMENT"),
|
Pair("center_x", "integer NOT NULL DEFAULT 0"),
|
||||||
Pair("center_x", "integer NOT NULL DEFAULT 0"),
|
Pair("center_z", "integer NOT NULL DEFAULT 0"),
|
||||||
Pair("center_z", "integer NOT NULL DEFAULT 0"),
|
Pair("last_x", "integer NOT NULL DEFAULT 0"),
|
||||||
Pair("last_x", "integer NOT NULL DEFAULT 0"),
|
Pair("last_z", "integer NOT NULL DEFAULT 0"),
|
||||||
Pair("last_z", "integer NOT NULL DEFAULT 0"),
|
Pair("world", "text UNIQUE NOT NULL DEFAULT 'world'"),
|
||||||
Pair("world", "text UNIQUE NOT NULL DEFAULT 'world'"),
|
Pair("radius", "integer DEFAULT -1"),
|
||||||
Pair("radius", "integer DEFAULT -1"),
|
Pair("shape", "text NOT NULL DEFAULT 'square'"),
|
||||||
Pair("shape", "text NOT NULL DEFAULT 'square'"),
|
Pair("state", "text NOT NULL DEFAULT 'GENERATING'")
|
||||||
Pair("state", "text NOT NULL DEFAULT 'GENERATING'")
|
)
|
||||||
)
|
),
|
||||||
),
|
Pair(
|
||||||
Pair(
|
"world_properties",
|
||||||
"world_properties",
|
listOf(
|
||||||
listOf(
|
Pair("name", "text PRIMARY KEY"),
|
||||||
Pair("name", "text PRIMARY KEY"),
|
Pair("center_x", "integer NOT NULL DEFAULT 0"),
|
||||||
Pair("center_x", "integer NOT NULL DEFAULT 0"),
|
Pair("center_z", "integer NOT NULL DEFAULT 0")
|
||||||
Pair("center_z", "integer NOT NULL DEFAULT 0")
|
)
|
||||||
)
|
),
|
||||||
),
|
Pair(
|
||||||
Pair(
|
"pending_chunks",
|
||||||
"pending_chunks",
|
listOf(
|
||||||
listOf(
|
Pair("id", "integer PRIMARY KEY AUTOINCREMENT"),
|
||||||
Pair("id", "integer PRIMARY KEY AUTOINCREMENT"),
|
Pair("task_id", "integer NOT NULL"),
|
||||||
Pair("task_id", "integer NOT NULL"),
|
Pair("chunk_x", "integer NOT NULL"),
|
||||||
Pair("chunk_x", "integer NOT NULL"),
|
Pair("chunk_z", "integer NOT NULL")
|
||||||
Pair("chunk_z", "integer NOT NULL")
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
private val needUpdate = HashSet<Pair<String, Pair<String, String>>>()
|
||||||
private val needUpdate = HashSet<Pair<String, Pair<String, String>>>()
|
private val needCreation = HashSet<String>()
|
||||||
private val needCreation = HashSet<String>()
|
private var connection: Connection? = null
|
||||||
private var connection: Connection? = null
|
private var activeTasks = 0
|
||||||
private var activeTasks = 0
|
|
||||||
|
val worldProperties = WorldProperties(this)
|
||||||
val worldProperties = WorldProperties(this)
|
val pendingChunks = PendingChunks(this)
|
||||||
val pendingChunks = PendingChunks(this)
|
val generationTasks = GenerationTasks(this)
|
||||||
val generationTasks = GenerationTasks(this)
|
|
||||||
|
/**
|
||||||
/**
|
* Returns the connection to the database
|
||||||
* Returns the connection to the database
|
*/
|
||||||
*/
|
fun getConnection(): Connection? {
|
||||||
fun getConnection(): Connection? {
|
if (this.connection != null) {
|
||||||
if (this.connection != null) {
|
return this.connection
|
||||||
return this.connection
|
}
|
||||||
}
|
try {
|
||||||
try {
|
Class.forName("org.sqlite.JDBC")
|
||||||
Class.forName("org.sqlite.JDBC")
|
this.connection = DriverManager.getConnection(
|
||||||
this.connection = DriverManager.getConnection("jdbc:sqlite:${chunkmaster.dataFolder.absolutePath}/" +
|
"jdbc:sqlite:${chunkmaster.dataFolder.absolutePath}/" +
|
||||||
"${chunkmaster.config.getString("database.filename")}")
|
"${chunkmaster.config.getString("database.filename")}"
|
||||||
return this.connection
|
)
|
||||||
} catch (e: Exception) {
|
return this.connection
|
||||||
chunkmaster.logger.severe(chunkmaster.langManager.getLocalized("DATABASE_CONNECTION_ERROR"))
|
} catch (e: Exception) {
|
||||||
chunkmaster.logger.severe(e.message)
|
chunkmaster.logger.severe(chunkmaster.langManager.getLocalized("DATABASE_CONNECTION_ERROR"))
|
||||||
}
|
chunkmaster.logger.severe(e.message)
|
||||||
return null
|
}
|
||||||
}
|
return null
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Checks for and performs an update
|
/**
|
||||||
*/
|
* Checks for and performs an update
|
||||||
fun init() {
|
*/
|
||||||
this.checkUpdate()
|
fun init() {
|
||||||
this.performUpdate()
|
this.checkUpdate()
|
||||||
}
|
this.performUpdate()
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Checks which tables need an update or creation.
|
/**
|
||||||
*/
|
* Checks which tables need an update or creation.
|
||||||
private fun checkUpdate() {
|
*/
|
||||||
val meta = getConnection()!!.metaData
|
private fun checkUpdate() {
|
||||||
|
val meta = getConnection()!!.metaData
|
||||||
for (table in tables) {
|
|
||||||
val resTables = meta.getTables(null, null, table.first, null)
|
for (table in tables) {
|
||||||
|
val resTables = meta.getTables(null, null, table.first, null)
|
||||||
if (resTables.next()) { // table exists
|
|
||||||
for (column in table.second) {
|
if (resTables.next()) { // table exists
|
||||||
val resColumn = meta.getColumns(null, null, table.first, column.first)
|
for (column in table.second) {
|
||||||
if (!resColumn.next()) {
|
val resColumn = meta.getColumns(null, null, table.first, column.first)
|
||||||
needUpdate.add(Pair(table.first, column))
|
if (!resColumn.next()) {
|
||||||
}
|
needUpdate.add(Pair(table.first, column))
|
||||||
resColumn.close()
|
}
|
||||||
}
|
resColumn.close()
|
||||||
} else {
|
}
|
||||||
needCreation.add(table.first)
|
} else {
|
||||||
}
|
needCreation.add(table.first)
|
||||||
resTables.close()
|
}
|
||||||
}
|
resTables.close()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Executes a sql statement on the database.
|
/**
|
||||||
*/
|
* Executes a sql statement on the database.
|
||||||
fun executeStatement(sql: String, values: HashMap<Int, Any>, callback: ((ResultSet?) -> Unit)?) {
|
*/
|
||||||
val connection = getConnection()
|
fun executeStatement(sql: String, values: HashMap<Int, Any>, callback: ((ResultSet?) -> Unit)?) {
|
||||||
activeTasks++
|
val connection = getConnection()
|
||||||
if (connection != null) {
|
activeTasks++
|
||||||
try {
|
if (connection != null) {
|
||||||
//println("'$sql' with values $values")
|
try {
|
||||||
val statement = connection.prepareStatement(sql)
|
//println("'$sql' with values $values")
|
||||||
for (parameterValue in values) {
|
val statement = connection.prepareStatement(sql)
|
||||||
statement.setObject(parameterValue.key, parameterValue.value)
|
for (parameterValue in values) {
|
||||||
}
|
statement.setObject(parameterValue.key, parameterValue.value)
|
||||||
statement.execute()
|
}
|
||||||
val res: ResultSet? = statement.resultSet
|
statement.execute()
|
||||||
if (callback != null) {
|
val res: ResultSet? = statement.resultSet
|
||||||
callback(res)
|
if (callback != null) {
|
||||||
}
|
callback(res)
|
||||||
statement.close()
|
}
|
||||||
} catch (e: Exception) {
|
statement.close()
|
||||||
chunkmaster.logger.severe(chunkmaster.langManager.getLocalized("SQL_ERROR", e.toString()))
|
} catch (e: Exception) {
|
||||||
chunkmaster.logger.info(ExceptionUtils.getStackTrace(e))
|
chunkmaster.logger.severe(chunkmaster.langManager.getLocalized("SQL_ERROR", e.toString()))
|
||||||
} finally {
|
chunkmaster.logger.info(ExceptionUtils.getStackTrace(e))
|
||||||
activeTasks--
|
} finally {
|
||||||
if (activeTasks == 0) {
|
activeTasks--
|
||||||
connection.close()
|
if (activeTasks == 0) {
|
||||||
this.connection = null
|
connection.close()
|
||||||
}
|
this.connection = null
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
chunkmaster.logger.severe(chunkmaster.langManager.getLocalized("NO_DATABASE_CONNECTION"))
|
} else {
|
||||||
}
|
chunkmaster.logger.severe(chunkmaster.langManager.getLocalized("NO_DATABASE_CONNECTION"))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Creates or updates tables that needed an update.
|
/**
|
||||||
*/
|
* Creates or updates tables that needed an update.
|
||||||
private fun performUpdate() {
|
*/
|
||||||
for (table in needCreation) {
|
private fun performUpdate() {
|
||||||
try {
|
for (table in needCreation) {
|
||||||
var tableDef = "CREATE TABLE IF NOT EXISTS $table ("
|
try {
|
||||||
|
var tableDef = "CREATE TABLE IF NOT EXISTS $table ("
|
||||||
for (column in tables.find{it.first == table}!!.second) {
|
|
||||||
tableDef += "${column.first} ${column.second},"
|
for (column in tables.find { it.first == table }!!.second) {
|
||||||
}
|
tableDef += "${column.first} ${column.second},"
|
||||||
tableDef = tableDef.substringBeforeLast(",") + ");"
|
}
|
||||||
chunkmaster.logger.finest(chunkmaster.langManager.getLocalized("CREATE_TABLE_DEFINITION", table, tableDef))
|
tableDef = tableDef.substringBeforeLast(",") + ");"
|
||||||
executeStatement(tableDef, HashMap(), null)
|
chunkmaster.logger.finest(
|
||||||
} catch (e: Exception) {
|
chunkmaster.langManager.getLocalized(
|
||||||
chunkmaster.logger.severe(chunkmaster.langManager.getLocalized("TABLE_CREATE_ERROR", table))
|
"CREATE_TABLE_DEFINITION",
|
||||||
chunkmaster.logger.severe(e.message)
|
table,
|
||||||
chunkmaster.logger.info(ExceptionUtils.getStackTrace(e))
|
tableDef
|
||||||
}
|
)
|
||||||
}
|
)
|
||||||
for (table in needUpdate) {
|
executeStatement(tableDef, HashMap(), null)
|
||||||
val updateSql = "ALTER TABLE ${table.first} ADD COLUMN ${table.second.first} ${table.second.second}"
|
} catch (e: Exception) {
|
||||||
try {
|
chunkmaster.logger.severe(chunkmaster.langManager.getLocalized("TABLE_CREATE_ERROR", table))
|
||||||
executeStatement(updateSql, HashMap(), null)
|
chunkmaster.logger.severe(e.message)
|
||||||
chunkmaster.logger.finest(chunkmaster.langManager.getLocalized("UPDATE_TABLE_DEFINITION", table.first, updateSql))
|
chunkmaster.logger.info(ExceptionUtils.getStackTrace(e))
|
||||||
} catch (e: Exception) {
|
}
|
||||||
chunkmaster.logger.severe(chunkmaster.langManager.getLocalized("UPDATE_TABLE_FAILED", table.first, updateSql))
|
}
|
||||||
chunkmaster.logger.severe(e.message)
|
for (table in needUpdate) {
|
||||||
chunkmaster.logger.info(ExceptionUtils.getStackTrace(e))
|
val updateSql = "ALTER TABLE ${table.first} ADD COLUMN ${table.second.first} ${table.second.second}"
|
||||||
}
|
try {
|
||||||
}
|
executeStatement(updateSql, HashMap(), null)
|
||||||
}
|
chunkmaster.logger.finest(
|
||||||
|
chunkmaster.langManager.getLocalized(
|
||||||
|
"UPDATE_TABLE_DEFINITION",
|
||||||
|
table.first,
|
||||||
|
updateSql
|
||||||
|
)
|
||||||
|
)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
chunkmaster.logger.severe(
|
||||||
|
chunkmaster.langManager.getLocalized(
|
||||||
|
"UPDATE_TABLE_FAILED",
|
||||||
|
table.first,
|
||||||
|
updateSql
|
||||||
|
)
|
||||||
|
)
|
||||||
|
chunkmaster.logger.severe(e.message)
|
||||||
|
chunkmaster.logger.info(ExceptionUtils.getStackTrace(e))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,70 +1,69 @@
|
|||||||
package net.trivernis.chunkmaster.lib.generation.taskentry
|
package net.trivernis.chunkmaster.lib.generation.taskentry
|
||||||
|
|
||||||
import io.papermc.lib.PaperLib
|
import net.trivernis.chunkmaster.lib.generation.GenerationTask
|
||||||
import net.trivernis.chunkmaster.lib.generation.GenerationTask
|
|
||||||
|
class RunningTaskEntry(
|
||||||
class RunningTaskEntry(
|
override val id: Int,
|
||||||
override val id: Int,
|
override val generationTask: GenerationTask
|
||||||
override val generationTask: GenerationTask
|
) : TaskEntry {
|
||||||
) : TaskEntry {
|
|
||||||
|
private var lastProgress: Pair<Long, Double>? = null
|
||||||
private var lastProgress: Pair<Long, Double>? = null
|
private var lastChunkCount: Pair<Long, Int>? = null
|
||||||
private var lastChunkCount: Pair<Long, Int>? = null
|
private var thread = Thread(generationTask)
|
||||||
private var thread = Thread(generationTask)
|
|
||||||
|
/**
|
||||||
/**
|
* Returns the generation Speed
|
||||||
* Returns the generation Speed
|
*/
|
||||||
*/
|
val generationSpeed: Pair<Double?, Double?>
|
||||||
val generationSpeed: Pair<Double?, Double?>
|
get() {
|
||||||
get() {
|
var generationSpeed: Double? = null
|
||||||
var generationSpeed: Double? = null
|
var chunkGenerationSpeed: Double? = null
|
||||||
var chunkGenerationSpeed: Double? = null
|
if (lastProgress != null) {
|
||||||
if (lastProgress != null) {
|
val progressDiff = generationTask.shape.progress() - lastProgress!!.second
|
||||||
val progressDiff = generationTask.shape.progress() - lastProgress!!.second
|
val timeDiff = (System.currentTimeMillis() - lastProgress!!.first).toDouble() / 1000
|
||||||
val timeDiff = (System.currentTimeMillis() - lastProgress!!.first).toDouble() / 1000
|
generationSpeed = progressDiff / timeDiff
|
||||||
generationSpeed = progressDiff / timeDiff
|
}
|
||||||
}
|
if (lastChunkCount != null) {
|
||||||
if (lastChunkCount != null) {
|
val chunkDiff = generationTask.count - lastChunkCount!!.second
|
||||||
val chunkDiff = generationTask.count - lastChunkCount!!.second
|
val timeDiff = (System.currentTimeMillis() - lastChunkCount!!.first).toDouble() / 1000
|
||||||
val timeDiff = (System.currentTimeMillis() - lastChunkCount!!.first).toDouble() / 1000
|
chunkGenerationSpeed = chunkDiff / timeDiff
|
||||||
chunkGenerationSpeed = chunkDiff / timeDiff
|
}
|
||||||
}
|
lastProgress = Pair(System.currentTimeMillis(), generationTask.shape.progress())
|
||||||
lastProgress = Pair(System.currentTimeMillis(), generationTask.shape.progress())
|
lastChunkCount = Pair(System.currentTimeMillis(), generationTask.count)
|
||||||
lastChunkCount = Pair(System.currentTimeMillis(), generationTask.count)
|
return Pair(generationSpeed, chunkGenerationSpeed)
|
||||||
return Pair(generationSpeed, chunkGenerationSpeed)
|
}
|
||||||
}
|
|
||||||
|
init {
|
||||||
init {
|
lastProgress = Pair(System.currentTimeMillis(), generationTask.shape.progress())
|
||||||
lastProgress = Pair(System.currentTimeMillis(), generationTask.shape.progress())
|
lastChunkCount = Pair(System.currentTimeMillis(), generationTask.count)
|
||||||
lastChunkCount = Pair(System.currentTimeMillis(), generationTask.count)
|
}
|
||||||
}
|
|
||||||
|
fun start() {
|
||||||
fun start() {
|
thread.start()
|
||||||
thread.start()
|
}
|
||||||
}
|
|
||||||
|
fun cancel(timeout: Long): Boolean {
|
||||||
fun cancel(timeout: Long): Boolean {
|
if (generationTask.isRunning) {
|
||||||
if (generationTask.isRunning) {
|
generationTask.cancel()
|
||||||
generationTask.cancel()
|
thread.interrupt()
|
||||||
thread.interrupt()
|
}
|
||||||
}
|
return try {
|
||||||
return try {
|
joinThread(timeout)
|
||||||
joinThread(timeout)
|
} catch (e: InterruptedException) {
|
||||||
} catch (e: InterruptedException) {
|
true
|
||||||
true
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private fun joinThread(timeout: Long): Boolean {
|
||||||
private fun joinThread(timeout: Long): Boolean {
|
var threadStopped = false
|
||||||
var threadStopped = false
|
|
||||||
|
for (i in 0..100) {
|
||||||
for (i in 0..100) {
|
if (!thread.isAlive || !generationTask.isRunning) {
|
||||||
if (!thread.isAlive || !generationTask.isRunning) {
|
threadStopped = true
|
||||||
threadStopped = true
|
break
|
||||||
break
|
}
|
||||||
}
|
Thread.sleep(timeout / 100)
|
||||||
Thread.sleep(timeout / 100)
|
}
|
||||||
}
|
return threadStopped
|
||||||
return threadStopped
|
}
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue