Cleanup heimdall code.

This commit is contained in:
2023-02-05 19:34:21 -08:00
parent 7d040c8dd8
commit 2e05aef95c
5 changed files with 48 additions and 37 deletions

View File

@ -1,14 +1,14 @@
package gay.pizza.foundation.heimdall.plugin
import gay.pizza.foundation.heimdall.plugin.buffer.BufferFlushThread
import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer
import gay.pizza.foundation.heimdall.plugin.event.*
import gay.pizza.foundation.heimdall.plugin.model.HeimdallConfig
import gay.pizza.foundation.heimdall.plugin.export.ExportChunksCommand
import com.charleskorn.kaml.Yaml
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import gay.pizza.foundation.core.Util
import gay.pizza.foundation.heimdall.plugin.buffer.BufferFlushThread
import gay.pizza.foundation.heimdall.plugin.buffer.EventBuffer
import gay.pizza.foundation.heimdall.plugin.event.*
import gay.pizza.foundation.heimdall.plugin.export.ExportAllChunksCommand
import gay.pizza.foundation.heimdall.plugin.model.HeimdallConfig
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
@ -20,8 +20,6 @@ import org.bukkit.event.player.*
import org.bukkit.plugin.java.JavaPlugin
import org.jetbrains.exposed.sql.Database
import org.postgresql.Driver
import org.slf4j.Logger
import java.nio.file.Path
import java.time.Duration
import java.time.Instant
import java.util.*
@ -42,7 +40,7 @@ class HeimdallPlugin : JavaPlugin(), Listener {
override fun onEnable() {
val exportChunksCommand = getCommand("export_all_chunks") ?: throw Exception("Failed to get export_all_chunks command")
exportChunksCommand.setExecutor(ExportChunksCommand(this))
exportChunksCommand.setExecutor(ExportAllChunksCommand(this))
val pluginDataPath = dataFolder.toPath()
pluginDataPath.toFile().mkdir()
@ -73,7 +71,7 @@ class HeimdallPlugin : JavaPlugin(), Listener {
"/init.sql"
)?.readAllBytes()?.decodeToString() ?: throw RuntimeException("Unable to find Heimdall init.sql")
val statements = initMigrationContent.sqlSplitStatements()
val statements = sqlSplitStatements(initMigrationContent)
pool.connection.use { conn ->
conn.autoCommit = false

View File

@ -12,27 +12,27 @@ import org.bukkit.plugin.Plugin
import java.io.File
import java.util.zip.GZIPOutputStream
class ChunkExporter(private val plugin: Plugin, val world: World) {
class ChunkExporter(private val plugin: Plugin) {
private val json = Json {
ignoreUnknownKeys = true
}
fun exportLoadedChunksAsync() {
exportChunkListAsync(world.loadedChunks.toList())
fun exportLoadedChunksAsync(world: World) {
exportChunkListAsync(world, world.loadedChunks.toList())
}
private fun exportChunkListAsync(chunks: List<Chunk>) {
plugin.slF4JLogger.info("Exporting ${chunks.size} Chunks")
private fun exportChunkListAsync(world: World, chunks: List<Chunk>) {
plugin.slF4JLogger.info("Exporting ${chunks.size} chunks")
val snapshots = chunks.map { it.chunkSnapshot }
Thread {
for (snapshot in snapshots) {
exportChunkSnapshot(snapshot)
exportChunkSnapshot(world, snapshot)
}
plugin.slF4JLogger.info("Exported ${chunks.size} Chunks")
plugin.slF4JLogger.info("Exported ${chunks.size} chunks for world ${world.name}")
}.start()
}
private fun exportChunkSnapshot(snapshot: ChunkSnapshot) {
private fun exportChunkSnapshot(world: World, snapshot: ChunkSnapshot) {
val blocks = mutableMapOf<String, Pair<Int, ExportedBlock>>()
val blockList = mutableListOf<ExportedBlock>()
val sections = mutableListOf<ExportedChunkSection>()
@ -60,7 +60,14 @@ class ChunkExporter(private val plugin: Plugin, val world: World) {
gzipOutputStream.close()
}
private fun exportChunkSection(blocks: MutableMap<String, Pair<Int, ExportedBlock>>, blockList: MutableList<ExportedBlock>, snapshot: ChunkSnapshot, yRange: IntRange, x: Int, z: Int): ExportedChunkSection {
private fun exportChunkSection(
blocks: MutableMap<String, Pair<Int, ExportedBlock>>,
blockList: MutableList<ExportedBlock>,
snapshot: ChunkSnapshot,
yRange: IntRange,
x: Int,
z: Int
): ExportedChunkSection {
val contents = mutableListOf<Int>()
for (y in yRange) {
val blockData = snapshot.getBlockData(x, y, z)

View File

@ -0,0 +1,23 @@
package gay.pizza.foundation.heimdall.plugin.export
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.plugin.Plugin
class ExportAllChunksCommand(private val plugin: Plugin) : CommandExecutor {
override fun onCommand(
sender: CommandSender,
command: Command,
label: String,
args: Array<out String>
): Boolean {
sender.sendMessage("Exporting all chunks...")
plugin.slF4JLogger.info("Exporting all chunks")
val export = ChunkExporter(plugin)
for (world in sender.server.worlds) {
export.exportLoadedChunksAsync(world)
}
return true
}
}

View File

@ -1,17 +0,0 @@
package gay.pizza.foundation.heimdall.plugin.export
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.plugin.Plugin
class ExportChunksCommand(private val plugin: Plugin) : CommandExecutor {
override fun onCommand(sender: CommandSender, command: Command, label: String, args: Array<out String>): Boolean {
plugin.slF4JLogger.info("Exporting All Chunks")
for (world in sender.server.worlds) {
val export = ChunkExporter(plugin, world)
export.exportLoadedChunksAsync()
}
return true
}
}

View File

@ -1,6 +1,6 @@
package gay.pizza.foundation.heimdall.plugin
fun String.sqlSplitStatements(): List<String> {
fun sqlSplitStatements(input: String): List<String> {
val statements = mutableListOf<String>()
val buffer = StringBuilder()
fun flush() {
@ -9,7 +9,7 @@ fun String.sqlSplitStatements(): List<String> {
statements.add(trimmed)
}
}
for (line in lines()) {
for (line in input.lines()) {
if (line.trim() == "--") {
flush()
} else {