Skip to content

Commit

Permalink
Plugin update checker (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
tranlong5252 authored Mar 12, 2024
1 parent 34cb526 commit 76ee43d
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 2 deletions.
8 changes: 8 additions & 0 deletions dotman-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
5 changes: 5 additions & 0 deletions dotman-plugin/src/main/java/net/minevn/dotman/DotMan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +54,7 @@ class DotMan : MineVNPlugin(), Listener {
MainCmd.init()
AdminCmd.init()
TopNapCmd.init()
UpdateChecker.init()
expansion = Expansion().apply { register() }
}

Expand Down Expand Up @@ -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 {
Expand Down
55 changes: 55 additions & 0 deletions dotman-plugin/src/main/java/net/minevn/dotman/UpdateChecker.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions dotman-plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion dotman-plugin/src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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%'
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.'
2 changes: 1 addition & 1 deletion dotman-plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: DotMan
main: net.minevn.dotman.DotMan
version: 1.0.0
version: '${version}'
author: MineVN
depend: [ MineVNLib, PlayerPoints, PlaceholderAPI ]
commands:
Expand Down

0 comments on commit 76ee43d

Please sign in to comment.