-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34cb526
commit 76ee43d
Showing
8 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
dotman-plugin/src/main/java/net/minevn/dotman/UpdateChecker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters