2023-01-28 19:35:10 -08:00
|
|
|
package gay.pizza.foundation.heimdall.plugin
|
|
|
|
|
|
|
|
|
|
import com.charleskorn.kaml.Yaml
|
|
|
|
|
import com.zaxxer.hikari.HikariConfig
|
|
|
|
|
import com.zaxxer.hikari.HikariDataSource
|
2023-03-05 17:33:54 -08:00
|
|
|
import gay.pizza.foundation.common.BaseFoundationPlugin
|
2023-02-07 04:52:54 -05:00
|
|
|
import gay.pizza.foundation.common.FoundationCoreLoader
|
2023-02-05 19:34:21 -08:00
|
|
|
import gay.pizza.foundation.heimdall.plugin.buffer.BufferFlushThread
|
|
|
|
|
import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer
|
2023-02-07 19:51:25 -05:00
|
|
|
import gay.pizza.foundation.heimdall.plugin.event.EventCollector
|
|
|
|
|
import gay.pizza.foundation.heimdall.plugin.event.EventCollectorProviders
|
2023-02-05 19:34:21 -08:00
|
|
|
import gay.pizza.foundation.heimdall.plugin.export.ExportAllChunksCommand
|
2023-02-07 09:01:43 -05:00
|
|
|
import gay.pizza.foundation.heimdall.plugin.load.ImportWorldLoadCommand
|
2023-02-05 19:34:21 -08:00
|
|
|
import gay.pizza.foundation.heimdall.plugin.model.HeimdallConfig
|
2023-02-07 19:51:25 -05:00
|
|
|
import gay.pizza.foundation.shared.PluginMainClass
|
|
|
|
|
import gay.pizza.foundation.shared.copyDefaultConfig
|
2023-01-28 19:35:10 -08:00
|
|
|
import org.bukkit.event.Listener
|
|
|
|
|
import org.jetbrains.exposed.sql.Database
|
|
|
|
|
import org.postgresql.Driver
|
|
|
|
|
import java.time.Duration
|
|
|
|
|
import kotlin.io.path.inputStream
|
|
|
|
|
|
2023-02-05 19:37:59 -08:00
|
|
|
@PluginMainClass
|
2023-03-05 17:33:54 -08:00
|
|
|
class FoundationHeimdallPlugin : BaseFoundationPlugin(), Listener {
|
2023-01-28 19:35:10 -08:00
|
|
|
private lateinit var config: HeimdallConfig
|
|
|
|
|
private lateinit var pool: HikariDataSource
|
|
|
|
|
internal var db: Database? = null
|
|
|
|
|
|
|
|
|
|
private val buffer = EventBuffer()
|
|
|
|
|
private val bufferFlushThread = BufferFlushThread(this, buffer)
|
|
|
|
|
|
2023-02-09 03:44:43 -05:00
|
|
|
val collectors = mutableListOf<EventCollector<*>>()
|
2023-01-28 19:35:10 -08:00
|
|
|
|
|
|
|
|
override fun onEnable() {
|
2023-02-07 19:51:25 -05:00
|
|
|
val exportChunksCommand = getCommand("export_all_chunks") ?:
|
|
|
|
|
throw Exception("Failed to get export_all_chunks command")
|
2023-02-05 19:34:21 -08:00
|
|
|
exportChunksCommand.setExecutor(ExportAllChunksCommand(this))
|
2023-01-28 19:35:10 -08:00
|
|
|
|
2023-03-05 17:33:54 -08:00
|
|
|
registerCommandExecutor("export_all_chunks", ExportAllChunksCommand(this))
|
|
|
|
|
registerCommandExecutor("export_world_load", ExportAllChunksCommand(this))
|
|
|
|
|
registerCommandExecutor("import_world_load", ExportAllChunksCommand(this))
|
|
|
|
|
|
2023-02-07 19:51:25 -05:00
|
|
|
val importWorldLoadCommand = getCommand("import_world_load") ?:
|
|
|
|
|
throw Exception("Failed to get import_world_load command")
|
2023-02-07 09:01:43 -05:00
|
|
|
importWorldLoadCommand.setExecutor(ImportWorldLoadCommand(this))
|
|
|
|
|
|
2023-02-07 04:52:54 -05:00
|
|
|
val foundation = FoundationCoreLoader.get(server)
|
|
|
|
|
val configPath = copyDefaultConfig<FoundationHeimdallPlugin>(
|
2023-01-28 19:35:10 -08:00
|
|
|
slF4JLogger,
|
2023-02-07 04:52:54 -05:00
|
|
|
foundation.pluginDataPath,
|
2023-01-28 19:35:10 -08:00
|
|
|
"heimdall.yaml"
|
|
|
|
|
)
|
|
|
|
|
config = Yaml.default.decodeFromStream(HeimdallConfig.serializer(), configPath.inputStream())
|
|
|
|
|
if (!config.enabled) {
|
2023-02-09 03:44:43 -05:00
|
|
|
slF4JLogger.info("Heimdall tracking is not enabled.")
|
2023-01-28 19:35:10 -08:00
|
|
|
return
|
|
|
|
|
}
|
2023-02-09 03:44:43 -05:00
|
|
|
slF4JLogger.info("Heimdall tracking is enabled.")
|
2023-01-28 19:35:10 -08:00
|
|
|
if (!Driver.isRegistered()) {
|
|
|
|
|
Driver.register()
|
|
|
|
|
}
|
|
|
|
|
pool = HikariDataSource(HikariConfig().apply {
|
|
|
|
|
jdbcUrl = config.db.url
|
|
|
|
|
username = config.db.username
|
|
|
|
|
password = config.db.password
|
|
|
|
|
maximumPoolSize = 10
|
|
|
|
|
idleTimeout = Duration.ofMinutes(5).toMillis()
|
|
|
|
|
maxLifetime = Duration.ofMinutes(10).toMillis()
|
|
|
|
|
})
|
2023-02-05 19:37:59 -08:00
|
|
|
val initMigrationContent = FoundationHeimdallPlugin::class.java.getResourceAsStream(
|
2023-01-28 19:35:10 -08:00
|
|
|
"/init.sql"
|
|
|
|
|
)?.readAllBytes()?.decodeToString() ?: throw RuntimeException("Unable to find Heimdall init.sql")
|
|
|
|
|
|
2023-02-05 19:34:21 -08:00
|
|
|
val statements = sqlSplitStatements(initMigrationContent)
|
2023-01-28 19:35:10 -08:00
|
|
|
|
|
|
|
|
pool.connection.use { conn ->
|
|
|
|
|
conn.autoCommit = false
|
|
|
|
|
try {
|
|
|
|
|
for (statementAsString in statements) {
|
|
|
|
|
conn.prepareStatement(statementAsString).use {
|
|
|
|
|
it.execute()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
conn.commit()
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
conn.rollback()
|
|
|
|
|
throw e
|
|
|
|
|
} finally {
|
|
|
|
|
conn.autoCommit = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db = Database.connect(pool)
|
|
|
|
|
server.pluginManager.registerEvents(this, this)
|
|
|
|
|
bufferFlushThread.start()
|
|
|
|
|
|
2023-02-07 19:51:25 -05:00
|
|
|
for (collectorProvider in EventCollectorProviders.all) {
|
2023-02-09 03:44:43 -05:00
|
|
|
val collector = collectorProvider.collector(config, buffer)
|
2023-02-07 19:51:25 -05:00
|
|
|
server.pluginManager.registerEvents(collector, this)
|
|
|
|
|
collectors.add(collector)
|
2023-01-28 19:35:10 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDisable() {
|
|
|
|
|
bufferFlushThread.stop()
|
2023-02-07 19:51:25 -05:00
|
|
|
for (collector in collectors) {
|
|
|
|
|
collector.onPluginDisable(server)
|
2023-01-28 19:35:10 -08:00
|
|
|
}
|
2023-02-07 19:51:25 -05:00
|
|
|
collectors.clear()
|
2023-01-28 19:35:10 -08:00
|
|
|
bufferFlushThread.flush()
|
|
|
|
|
}
|
|
|
|
|
}
|