diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/HeimdallPlugin.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/HeimdallPlugin.kt index 2e88484..08f81da 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/HeimdallPlugin.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/HeimdallPlugin.kt @@ -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 diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/export/ChunkExporter.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/export/ChunkExporter.kt index 991eee4..8f883e2 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/export/ChunkExporter.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/export/ChunkExporter.kt @@ -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) { - plugin.slF4JLogger.info("Exporting ${chunks.size} Chunks") + private fun exportChunkListAsync(world: World, chunks: List) { + 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>() val blockList = mutableListOf() val sections = mutableListOf() @@ -60,7 +60,14 @@ class ChunkExporter(private val plugin: Plugin, val world: World) { gzipOutputStream.close() } - private fun exportChunkSection(blocks: MutableMap>, blockList: MutableList, snapshot: ChunkSnapshot, yRange: IntRange, x: Int, z: Int): ExportedChunkSection { + private fun exportChunkSection( + blocks: MutableMap>, + blockList: MutableList, + snapshot: ChunkSnapshot, + yRange: IntRange, + x: Int, + z: Int + ): ExportedChunkSection { val contents = mutableListOf() for (y in yRange) { val blockData = snapshot.getBlockData(x, y, z) diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/export/ExportAllChunksCommand.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/export/ExportAllChunksCommand.kt new file mode 100644 index 0000000..40e2f74 --- /dev/null +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/export/ExportAllChunksCommand.kt @@ -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 + ): 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 + } +} diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/export/ExportChunksCommand.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/export/ExportChunksCommand.kt deleted file mode 100644 index 4729883..0000000 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/export/ExportChunksCommand.kt +++ /dev/null @@ -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): Boolean { - plugin.slF4JLogger.info("Exporting All Chunks") - for (world in sender.server.worlds) { - val export = ChunkExporter(plugin, world) - export.exportLoadedChunksAsync() - } - return true - } -} diff --git a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/Extensions.kt b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/sql.kt similarity index 81% rename from foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/Extensions.kt rename to foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/sql.kt index 000cbec..422d25c 100644 --- a/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/Extensions.kt +++ b/foundation-heimdall/src/main/kotlin/gay/pizza/foundation/heimdall/plugin/sql.kt @@ -1,6 +1,6 @@ package gay.pizza.foundation.heimdall.plugin -fun String.sqlSplitStatements(): List { +fun sqlSplitStatements(input: String): List { val statements = mutableListOf() val buffer = StringBuilder() fun flush() { @@ -9,7 +9,7 @@ fun String.sqlSplitStatements(): List { statements.add(trimmed) } } - for (line in lines()) { + for (line in input.lines()) { if (line.trim() == "--") { flush() } else {