From 76ee43dfafb7e33fb05ff3612069aa10418159e7 Mon Sep 17 00:00:00 2001 From: Long Pham Date: Wed, 13 Mar 2024 01:30:48 +0700 Subject: [PATCH] Plugin update checker (#15) --- dotman-plugin/build.gradle.kts | 8 +++ .../src/main/java/net/minevn/dotman/DotMan.kt | 5 ++ .../java/net/minevn/dotman/UpdateChecker.kt | 55 +++++++++++++++++++ .../java/net/minevn/dotman/config/Language.kt | 3 + .../net/minevn/dotman/config/MainConfig.kt | 1 + dotman-plugin/src/main/resources/config.yml | 3 + dotman-plugin/src/main/resources/messages.yml | 6 +- dotman-plugin/src/main/resources/plugin.yml | 2 +- 8 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 dotman-plugin/src/main/java/net/minevn/dotman/UpdateChecker.kt diff --git a/dotman-plugin/build.gradle.kts b/dotman-plugin/build.gradle.kts index c57e91e..0208dc0 100644 --- a/dotman-plugin/build.gradle.kts +++ b/dotman-plugin/build.gradle.kts @@ -58,6 +58,14 @@ tasks { } } + processResources { + filesMatching(listOf("**/plugin.yml")) { + expand(mapOf("version" to project.version.toString())) + } + duplicatesStrategy = DuplicatesStrategy.INCLUDE + filteringCharset = Charsets.UTF_8.name() + } + shadowJar { archiveFileName.set("$jarName.jar") } diff --git a/dotman-plugin/src/main/java/net/minevn/dotman/DotMan.kt b/dotman-plugin/src/main/java/net/minevn/dotman/DotMan.kt index a073c4a..1d6d844 100644 --- a/dotman-plugin/src/main/java/net/minevn/dotman/DotMan.kt +++ b/dotman-plugin/src/main/java/net/minevn/dotman/DotMan.kt @@ -23,6 +23,7 @@ import org.bukkit.Bukkit import org.bukkit.entity.Player import org.bukkit.event.EventHandler import org.bukkit.event.Listener +import org.bukkit.event.player.PlayerJoinEvent import org.bukkit.event.player.PlayerLoginEvent import org.bukkit.event.player.PlayerQuitEvent import java.util.logging.Level @@ -53,6 +54,7 @@ class DotMan : MineVNPlugin(), Listener { MainCmd.init() AdminCmd.init() TopNapCmd.init() + UpdateChecker.init() expansion = Expansion().apply { register() } } @@ -99,6 +101,9 @@ class DotMan : MineVNPlugin(), Listener { @EventHandler fun onQuit(e: PlayerQuitEvent) = updateUUID(e.player) + + @EventHandler + fun onJoin(e: PlayerJoinEvent) = UpdateChecker.sendUpdateMessage(e.player) // endregion companion object { diff --git a/dotman-plugin/src/main/java/net/minevn/dotman/UpdateChecker.kt b/dotman-plugin/src/main/java/net/minevn/dotman/UpdateChecker.kt new file mode 100644 index 0000000..3a3a69a --- /dev/null +++ b/dotman-plugin/src/main/java/net/minevn/dotman/UpdateChecker.kt @@ -0,0 +1,55 @@ +package net.minevn.dotman + +import net.minevn.dotman.utils.Utils.Companion.runNotSync +import net.minevn.dotman.utils.Utils.Companion.warning +import net.minevn.libs.bukkit.parseJson +import net.minevn.libs.get +import org.bukkit.command.CommandSender + +class UpdateChecker { + + companion object { + private const val URL = "https://api.github.com/repos/MineVN/DotMan/releases/latest" + private val plugin = DotMan.instance + private val language = plugin.language + private var releaseVersion : String = "" + private val currentVersion = plugin.description.version.trim() + private var latestVersion = currentVersion + private var latest = false + + fun init() { + runNotSync { + latest = checkUpdate() + sendUpdateMessage(plugin.server.consoleSender, true) + } + } + + fun sendUpdateMessage(receiver: CommandSender, notifyLatestVersion: Boolean = false) { + if (!plugin.config.checkUpdate || !receiver.hasPermission("dotman.update")) return + if (!latest) { + language.updateAvailable + .replace("%NEW_VERSION%", latestVersion) + .replace("%CURRENT_VERSION%", currentVersion) + .let { receiver.sendMessage(it) } + receiver.sendMessage(language.updateAvailableLink.replace("%URL%", releaseVersion)) + return + } + if (notifyLatestVersion) { + receiver.sendMessage(language.updateLatest) + } + } + + private fun checkUpdate(): Boolean { + try { + get(URL).parseJson().asJsonObject.let { + latestVersion = it["tag_name"].asString + releaseVersion = it["html_url"].asString + return latestVersion == currentVersion + } + } catch (e: Exception) { + e.warning("Không thể kiểm tra cập nhật") + return true + } + } + } +} diff --git a/dotman-plugin/src/main/java/net/minevn/dotman/config/Language.kt b/dotman-plugin/src/main/java/net/minevn/dotman/config/Language.kt index 40c8734..6a50018 100644 --- a/dotman-plugin/src/main/java/net/minevn/dotman/config/Language.kt +++ b/dotman-plugin/src/main/java/net/minevn/dotman/config/Language.kt @@ -2,6 +2,9 @@ package net.minevn.dotman.config class Language : FileConfig("messages") { + val updateLatest = get("update-latest") + val updateAvailable = get("update-available") + val updateAvailableLink = get("update-available-link") val errorUnknown = get("error-unknown") val errorUnknownCardType = get("error-unknown-card-type") val errorUnknownCardPrice = get("error-unknown-card-price") diff --git a/dotman-plugin/src/main/java/net/minevn/dotman/config/MainConfig.kt b/dotman-plugin/src/main/java/net/minevn/dotman/config/MainConfig.kt index bd86b77..8ce79d4 100644 --- a/dotman-plugin/src/main/java/net/minevn/dotman/config/MainConfig.kt +++ b/dotman-plugin/src/main/java/net/minevn/dotman/config/MainConfig.kt @@ -7,6 +7,7 @@ import net.minevn.dotman.utils.dateAndTimeFormat class MainConfig : FileConfig("config") { + val checkUpdate = config.getBoolean("check-update", true) val announceCharge = config.getBoolean("announce-charge", true) val prefix = config.getString("prefix", "&6&lDotMan > &r")!!.color() val pointUnit = config.getString("point-unit", "point")!!.color() diff --git a/dotman-plugin/src/main/resources/config.yml b/dotman-plugin/src/main/resources/config.yml index 7a23e0e..f17c9e2 100644 --- a/dotman-plugin/src/main/resources/config.yml +++ b/dotman-plugin/src/main/resources/config.yml @@ -57,3 +57,6 @@ database: # Bật/tắt tính năng thông báo người chơi khác khi nạp thẻ thành công announce-charge: true + +# Bật/tắt tính năng kiểm tra cập nhật phiên bản mới +check-update: true \ No newline at end of file diff --git a/dotman-plugin/src/main/resources/messages.yml b/dotman-plugin/src/main/resources/messages.yml index c60f4e5..f173dc4 100644 --- a/dotman-plugin/src/main/resources/messages.yml +++ b/dotman-plugin/src/main/resources/messages.yml @@ -36,4 +36,8 @@ error-unknown: '&cLỗi không xác định.' error-unknown-card-type: '&cLoại thẻ không hợp lệ hoặc đang bảo trì.' error-unknown-card-price: '&cMệnh giá thẻ không hợp lệ.' -log-output: '&a%ORDER%. &b%PLAYER% &d%CARD_TYPE%(%CARD_PRICE% VNĐ) &anhận &e%POINTS_RECEIVED% %POINT_UNIT% &angày &e%DATE%' \ No newline at end of file +log-output: '&a%ORDER%. &b%PLAYER% &d%CARD_TYPE%(%CARD_PRICE% VNĐ) &anhận &e%POINTS_RECEIVED% %POINT_UNIT% &angày &e%DATE%' + +update-available: '%PREFIX% &aĐã có phiên bản mới: &e%NEW_VERSION% &a(từ &e%CURRENT_VERSION%&a).' +update-available-link: '&aTải về tại: &e%URL%' +update-latest: '%PREFIX% &aBạn đang sử dụng phiên bản mới nhất.' diff --git a/dotman-plugin/src/main/resources/plugin.yml b/dotman-plugin/src/main/resources/plugin.yml index 2b361a6..bdb5b5a 100644 --- a/dotman-plugin/src/main/resources/plugin.yml +++ b/dotman-plugin/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ name: DotMan main: net.minevn.dotman.DotMan -version: 1.0.0 +version: '${version}' author: MineVN depend: [ MineVNLib, PlayerPoints, PlaceholderAPI ] commands: