diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/UpdateCommand.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/UpdateCommand.kt index 32a09e9..5c07129 100644 --- a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/UpdateCommand.kt +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/command/UpdateCommand.kt @@ -1,11 +1,9 @@ package cloud.kubelet.foundation.core.command -import cloud.kubelet.foundation.core.update.UpdateUtil +import cloud.kubelet.foundation.core.service.UpdateService import org.bukkit.command.Command import org.bukkit.command.CommandExecutor import org.bukkit.command.CommandSender -import kotlin.io.path.name -import kotlin.io.path.toPath class UpdateCommand : CommandExecutor { override fun onCommand( @@ -14,39 +12,7 @@ class UpdateCommand : CommandExecutor { label: String, args: Array ): Boolean { - val updateDir = sender.server.pluginsFolder.resolve("update") - updateDir.mkdir() - if (!updateDir.exists()) { - sender.sendMessage("Error: Failed to create plugin update directory.") - return true - } - val updatePath = updateDir.toPath() - - // TODO: Move to separate thread? - val modules = UpdateUtil.fetchManifest() - val plugins = sender.server.pluginManager.plugins.associateBy { it.name.lowercase() } - - sender.sendMessage("Updates:") - modules.forEach { (name, manifest) -> - // Dumb naming problem. Don't want to fix it right now. - val plugin = if (name == "foundation-core") { - plugins["foundation"] - } else { - plugins[name.lowercase()] - } - - if (plugin == null) { - sender.sendMessage("Plugin in manifest, but not installed: $name (${manifest.version})") - } else { - val fileName = plugin.javaClass.protectionDomain.codeSource.location.toURI().toPath().name - val artifactPath = manifest.artifacts.getOrNull(0) ?: return@forEach - - sender.sendMessage("${plugin.name}: Updating ${plugin.description.version} to ${manifest.version}") - UpdateUtil.downloadArtifact(artifactPath, updatePath.resolve(fileName)) - } - } - sender.sendMessage("Restart to take effect") - + UpdateService.updatePlugins(sender) return true } } \ No newline at end of file diff --git a/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/service/UpdateService.kt b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/service/UpdateService.kt new file mode 100644 index 0000000..a8f6106 --- /dev/null +++ b/foundation-core/src/main/kotlin/cloud/kubelet/foundation/core/service/UpdateService.kt @@ -0,0 +1,47 @@ +package cloud.kubelet.foundation.core.service + +import cloud.kubelet.foundation.core.update.UpdateUtil +import org.bukkit.command.CommandSender +import kotlin.io.path.name +import kotlin.io.path.toPath + +// TODO: Switch to classes and use dependency injection with koin. +object UpdateService { + fun updatePlugins(sender: CommandSender, onFinish: (() -> Unit)? = null) { + val updateDir = sender.server.pluginsFolder.resolve("update") + updateDir.mkdir() + if (!updateDir.exists()) { + sender.sendMessage("Error: Failed to create plugin update directory.") + return + } + val updatePath = updateDir.toPath() + + Thread { + val modules = UpdateUtil.fetchManifest() + val plugins = sender.server.pluginManager.plugins.associateBy { it.name.lowercase() } + + sender.sendMessage("Updates:") + modules.forEach { (name, manifest) -> + // Dumb naming problem. Don't want to fix it right now. + val plugin = if (name == "foundation-core") { + plugins["foundation"] + } else { + plugins[name.lowercase()] + } + + if (plugin == null) { + sender.sendMessage("Plugin in manifest, but not installed: $name (${manifest.version})") + } else { + val fileName = plugin.javaClass.protectionDomain.codeSource.location.toURI().toPath().name + val artifactPath = manifest.artifacts.getOrNull(0) ?: return@forEach + + sender.sendMessage("${plugin.name}: Updating ${plugin.description.version} to ${manifest.version}") + UpdateUtil.downloadArtifact(artifactPath, updatePath.resolve(fileName)) + } + } + sender.sendMessage("Restart to take effect") + + if (onFinish != null) onFinish() + }.start() + } +}