diff --git a/build.gradle b/build.gradle index 89de8e0..1270d03 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { id 'com.gradleup.shadow' version '9.4.3' } -version = '1.0.0' +version = '1.1.0' repositories { mavenCentral() diff --git a/src/main/kotlin/uk/ewancroft/minefetch/MinefetchPlugin.kt b/src/main/kotlin/uk/ewancroft/minefetch/MinefetchPlugin.kt index acb1aa5..166c543 100644 --- a/src/main/kotlin/uk/ewancroft/minefetch/MinefetchPlugin.kt +++ b/src/main/kotlin/uk/ewancroft/minefetch/MinefetchPlugin.kt @@ -3,6 +3,7 @@ package uk.ewancroft.minefetch import uk.ewancroft.minefetch.command.MinefetchCommand import uk.ewancroft.minefetch.util.HostInfo import uk.ewancroft.minefetch.util.SysInfoReader +import uk.ewancroft.minefetch.util.UpdateChecker import net.kyori.adventure.text.Component import net.kyori.adventure.text.format.NamedTextColor import net.kyori.adventure.text.format.TextColor @@ -18,6 +19,7 @@ class MinefetchPlugin : JavaPlugin() { sysInfoReader = SysInfoReader("/sysinfo/host.json") val cmd = getCommand("minefetch") ?: return cmd.setExecutor(MinefetchCommand(this)) + UpdateChecker(this, "ewanc26", "Minefetch").checkAsync() logger.info("Minefetch enabled.") } diff --git a/src/main/kotlin/uk/ewancroft/minefetch/util/UpdateChecker.kt b/src/main/kotlin/uk/ewancroft/minefetch/util/UpdateChecker.kt new file mode 100644 index 0000000..f5ca7cf --- /dev/null +++ b/src/main/kotlin/uk/ewancroft/minefetch/util/UpdateChecker.kt @@ -0,0 +1,38 @@ +package uk.ewancroft.minefetch.util + +import org.bukkit.plugin.java.JavaPlugin +import org.json.JSONObject +import java.net.URI + +class UpdateChecker( + private val plugin: JavaPlugin, + private val repoOwner: String, + private val repoName: String, +) { + + fun checkAsync() { + plugin.server.scheduler.runTaskAsynchronously(plugin, Runnable { + try { + val url = URI("https://api.github.com/repos/$repoOwner/$repoName/releases/latest").toURL() + val connection = url.openConnection() + connection.setRequestProperty("Accept", "application/vnd.github.v3+json") + connection.setRequestProperty("User-Agent", "$repoName/$repoOwner") + connection.connectTimeout = 5000 + connection.readTimeout = 5000 + + val json = connection.inputStream.bufferedReader().readText() + val tagName = JSONObject(json).optString("tag_name") ?: return@Runnable + val currentVersion = plugin.pluginMeta.version + val latestVersion = tagName.removePrefix("v") + + if (currentVersion != latestVersion) { + val msg = "Update available: v$latestVersion (current: v$currentVersion) — $url" + plugin.server.scheduler.runTask(plugin, Runnable { + plugin.logger.info(msg) + }) + } + } catch (_: Exception) { + } + }) + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2d7dee5..d06da83 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: Minefetch -version: '1.0.0' +version: '1.1.0' main: uk.ewancroft.minefetch.MinefetchPlugin api-version: '1.21' description: Displays host and container system information in-game.