-
Notifications
You must be signed in to change notification settings - Fork 7
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
Showing
4 changed files
with
71 additions
and
90 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
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,63 @@ | ||
package com.e404.clean | ||
|
||
import com.e404.clean.util.Log | ||
import com.e404.clean.util.Log.color | ||
import com.e404.clean.util.instance | ||
import com.e404.clean.util.sendMsgWithPrefix | ||
import com.google.gson.JsonParser | ||
import org.bukkit.Bukkit | ||
import org.bukkit.event.EventHandler | ||
import org.bukkit.event.Listener | ||
import org.bukkit.event.player.PlayerJoinEvent | ||
import java.net.URL | ||
|
||
@Suppress("deprecation") | ||
object Update : Listener { | ||
private const val url = "https://api.github.com/repos/4o4E/EClean/releases" | ||
private const val post = "https://www.mcbbs.net/thread-1305548-1-1.html" | ||
private val jp = JsonParser() | ||
private var latest: String? = null | ||
private lateinit var nowVer: String | ||
|
||
// 返回最新的版本 | ||
private fun getLatest() = jp.parse(URL(url).readText()) | ||
.asJsonArray[0] | ||
.asJsonObject["tag_name"] | ||
.asString!! | ||
|
||
private fun String.asVersion() = replace(".", "").toInt() | ||
|
||
fun init() { | ||
nowVer = instance().description.version | ||
Bukkit.getPluginManager().registerEvents(this, instance()) | ||
Bukkit.getScheduler().runTaskTimerAsynchronously(instance(), Runnable { | ||
runCatching { | ||
val v = getLatest() | ||
val now = instance().description.version | ||
if (v.asVersion() > now.asVersion()) { | ||
latest = v | ||
Log.info("""有新版本, 当前版本: &c$nowVer&f, 最新版本: &a$latest | ||
|&f更新发布于:&b $post""" | ||
.trimMargin() | ||
.color()) | ||
return@runCatching | ||
} | ||
}.onFailure { | ||
Log.warn("检查版本更新时出现异常, 若需要手动更新请前往&b $post") | ||
} | ||
Log.info("当前版本: &a${nowVer}已是最新版本") | ||
}, 0, 20 * 60 * 60 * 6) | ||
} | ||
|
||
@EventHandler | ||
fun onOpJoinGame(event: PlayerJoinEvent) { | ||
val p = event.player | ||
if (!p.isOp | ||
|| latest == null | ||
) return | ||
p.sendMsgWithPrefix("""&f插件有更新哦, 当前版本: &c$nowVer&f, 最新版本: &a$latest | ||
|&f更新发布于:&b $post""" | ||
.trimMargin() | ||
.color()) | ||
} | ||
} |
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