diff --git a/README.md b/README.md index 35e97ba..09942d6 100644 --- a/README.md +++ b/README.md @@ -25,12 +25,14 @@ ## 命令与权限 -| 命令 | 描述 | 权限 | -|:------------------------------------|:--------|:--------------------------| -| /pipd | 等效 | `potatoipdisplay.command` | -| /potatoipdisplay | 插件信息 | `potatoipdisplay.command` | -| /potatoipdisplay lookup <玩家\|IPv4 > | 查询玩家或IP | `potatoipdisplay.lookup` | -| /potatoipdisplay reload | 重载插件 | `potatoipdisplay.reload` | +| 命令 | 描述 | 权限 | 默认 | +|:--------------------------------------|:-----------------------|:--------------------------|:---| +| /pipd | 等效于 `/potatoipdisplay` | `potatoipdisplay.command` | OP | +| /potatoipdisplay | 插件信息 | `potatoipdisplay.command` | OP | +| /potatoipdisplay about | 显示关于信息 | `potatoipdisplay.command` | OP | +| /potatoipdisplay clear [player/cache] | 清除玩家/查询缓存 | `potatoipdisplay.command` | OP | +| /potatoipdisplay lookup [玩家/IPv4] | 查询玩家或IP | `potatoipdisplay.lookup` | OP | +| /potatoipdisplay reload | 重载插件 | `potatoipdisplay.reload` | OP | ## Placeholder API diff --git a/assets/chatdemo.png b/assets/chatdemo.png deleted file mode 100644 index 2d323ef..0000000 Binary files a/assets/chatdemo.png and /dev/null differ diff --git a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/PotatoIpDisplay.kt b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/PotatoIpDisplay.kt index 6606402..e51d39b 100644 --- a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/PotatoIpDisplay.kt +++ b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/PotatoIpDisplay.kt @@ -15,6 +15,12 @@ import java.io.File import java.util.logging.Level class PotatoIpDisplay : JavaPlugin() { + + companion object Instance { + lateinit var instance: PotatoIpDisplay + val plugin by lazy { instance } + } + lateinit var conf: Config override fun onLoad() { @@ -24,13 +30,11 @@ class PotatoIpDisplay : JavaPlugin() { override fun onEnable() { super.onEnable() - + instance = this initPlugin() initResources() - if (conf.options.allowbStats) { - Metrics(this, 21473) - } + if (conf.options.allowbStats) Metrics(this, 21473) log("PotatoIpDisplay has been enabled. [mode: ${conf.options.mode}]") } @@ -42,20 +46,17 @@ class PotatoIpDisplay : JavaPlugin() { private fun initResources() { val configFile = File(dataFolder, "config.yml") val dbFile = File(dataFolder, "ip2region.xdb") - val authors = this.description.authors - if ("yukonisen" !in authors || "NightFish" !in authors) { - this.isEnabled = false - } - - if(!configFile.exists()) { - this.saveDefaultConfig() - } val isLite = false + val ip2regionDatabaseURL = + "https://raw.githubusercontent.com/lionsoul2014/ip2region/master/data/ip2region.xdb" + + if ("yukonisen" !in authors || "NightFish" !in authors) this.isEnabled = false + if (!configFile.exists()) this.saveDefaultConfig() + if (conf.options.mode == "ip2region" && !dbFile.exists()) { - if(isLite) { - UpdateUtil.downloadDatabase( - "https://raw.githubusercontent.com/lionsoul2014/ip2region/master/data/ip2region.xdb", dbFile.toPath()) + if (isLite) { + UpdateUtil.downloadDatabase(ip2regionDatabaseURL, dbFile.toPath()) // TODO: Download db files from internet for lite builds } else { /* For non-lite builds, ip2region.xdb included in jar */ saveResource("ip2region.xdb", false) @@ -69,26 +70,25 @@ class PotatoIpDisplay : JavaPlugin() { val pm = Bukkit.getPluginManager() conf = loadConfig(config) - PlaceholderIntergration(this).unregister() + PlaceholderIntergration().unregister() if (conf.papi.enabled) { if (pm.getPlugin("PlaceholderAPI") != null) { - PlaceholderIntergration(this).register() + PlaceholderIntergration().register() } else throw RuntimeException("PlaceholderAPI enabled in config but NOT installed!") } - /* Unregistering events" */ + /* Unregistering events */ HandlerList.unregisterAll() - /* Registering events" */ + /* Registering events */ if (conf.message.playerChat.enabled) - // NOTE: formerly "parseOnly", but now we determine if PIPD listens for messages depending on it - pm.registerEvents(MessageListener(this), this) + pm.registerEvents(MessageListener(), this) if (conf.message.playerLogin.enabled) - pm.registerEvents(PlayerJoinListener(this), this) + pm.registerEvents(PlayerJoinListener(), this) /* Registering commands */ - getCommand("potatoipdisplay")!!.setExecutor(PotatoIpDisplayCommand(this)) - getCommand("pipd")!!.setExecutor(PotatoIpDisplayCommand(this)) + getCommand("potatoipdisplay")!!.setExecutor(PotatoIpDisplayCommand()) + getCommand("pipd")!!.setExecutor(PotatoIpDisplayCommand()) } fun log(message: String, level: Level = Level.INFO) = diff --git a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/command/PotatoIpDisplayCommand.kt b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/command/PotatoIpDisplayCommand.kt index b5abcf5..2a541e3 100644 --- a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/command/PotatoIpDisplayCommand.kt +++ b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/command/PotatoIpDisplayCommand.kt @@ -13,10 +13,15 @@ import org.bukkit.command.TabExecutor /** * The /potatoipdisplay command. */ -class PotatoIpDisplayCommand(plugin: PotatoIpDisplay) : TabExecutor { - private val plugin: PotatoIpDisplay +class PotatoIpDisplayCommand : TabExecutor { + private val plugin = PotatoIpDisplay.plugin - override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array): Boolean { + override fun onCommand( + sender: CommandSender, + cmd: Command, + label: String, + args: Array + ): Boolean { if (!sender.hasPermission("potatoipdisplay.command")) { sendNoPerms(sender) @@ -24,16 +29,14 @@ class PotatoIpDisplayCommand(plugin: PotatoIpDisplay) : TabExecutor { } if (args.isEmpty()) { - sendMsg(sender, "§f版本 §b${plugin.description.version} §fby §b${plugin.description.authors.joinToString(", ")}", true) + var mode = plugin.conf.options.mode if (mode == "ip2region") { mode += " [${plugin.conf.options.xdbBuffer}]" } - val totalSize = IpAttributeMap.ip2regionRawDataMap.size + - IpAttributeMap.pconlineRawDataMap.size + - IpAttributeMap.ipApiRawDataMap.size - sendMsg(sender, "§f工作模式: §b$mode§f,总计 §b$totalSize §f条缓存项目", true) + + sendMsg(sender, "§f工作模式: §b$mode§f", true) sendMsg(sender, "§7尝试检查更新……", false) UpdateUtil.checkForUpdatesAsync { result -> sendMsg(sender, result, false) @@ -42,17 +45,32 @@ class PotatoIpDisplayCommand(plugin: PotatoIpDisplay) : TabExecutor { } when (args[0].lowercase()) { + "about" -> { + sendMsg(sender, "§f版本 §b${plugin.description.version} §fby §b${plugin.description.authors.joinToString(", ")}", true) + sendMsg(sender, "§6PotatoIPDisplay §f是§a免费§f的开源插件,详见:", false) + sendMsg(sender, "GitHub§f > §7\n https://github.com/dmzz-yyhyy/PotatoIpDisplay", false) + sendMsg(sender, "使用文档§f > §7\n https://upt.curiousers.org/docs/PotatoIpDisplay/intro", false) + return true + } + "reload" -> { if (!sender.hasPermission("potatoipdisplay.reload")) { sendNoPerms(sender) return true } - plugin.reloadConfig() - plugin.conf = loadConfig(plugin.config) - plugin.initPlugin() - sendMsg(sender, "§a重载成功!", true) + plugin.log("PotatoIPDisplay (${plugin.description.version}) 正在尝试重载。若遇到插件问题,请重启服务器。") + runCatching { + plugin.reloadConfig() + plugin.conf = loadConfig(plugin.config) + plugin.initPlugin() + }.onFailure { + sendMsg(sender, "§c重载失败,请检查控制台输出信息", true) + }.onSuccess { + sendMsg(sender, "§a重载成功!", true) + } return true } + "lookup" -> { if (!sender.hasPermission("potatoipdisplay.lookup")) { sendNoPerms(sender) @@ -80,6 +98,23 @@ class PotatoIpDisplayCommand(plugin: PotatoIpDisplay) : TabExecutor { } else sendMsg(sender, "§c查询的玩家离线,或 IPv4 无效", true) return true } + + "clear" -> { + if (args.size < 2) { + val map = IpAttributeMap + val totalCacheSize = + map.ip2regionRawDataMap.size + map.pconlineRawDataMap.size + map.ipApiRawDataMap.size + val playerCacheSize = map.playerIpAttributeMap.size + sendMsg(sender, "§e/$label clear player §f>> 清除玩家缓存 (当前 §b$playerCacheSize §f项)", false) + sendMsg(sender, "§e/$label clear cache §f>> 清除查询缓存 (当前 §b$totalCacheSize §f项)", false) + return true + } + + val clearedItems = clear(args[1].lowercase()) + sendMsg(sender, "§f清除完成,共清除了 §b$clearedItems §f项", true) + return true + } + else -> { sendMsg(sender, "§c未知命令", true) return true @@ -87,21 +122,35 @@ class PotatoIpDisplayCommand(plugin: PotatoIpDisplay) : TabExecutor { } } - override fun onTabComplete(sender: CommandSender, command: Command, alias: String, args: Array): List { + override fun onTabComplete( + sender: CommandSender, + command: Command, + alias: String, + args: Array + ): List { return when { !sender.hasPermission("potatoipdisplay.command") -> emptyList() - args.size == 1 -> listOf("lookup", "reload").filter { it.startsWith(args[0]) } + args.size == 1 -> listOf("lookup", "reload", "clear", "about").filter { + it.startsWith(args[0]) + } + args.size == 2 && (args[0] == "lookup") -> { - val matchingPlayers = plugin.server.onlinePlayers.map { it.name }.filter { it.startsWith(args[1]) } + val matchingPlayers = + plugin.server.onlinePlayers.map { it.name }.filter { it.startsWith(args[1]) } matchingPlayers + listOf("127.0.0.1") } + + args.size == 2 && (args[0] == "clear") -> { + listOf("player", "cache") + } + else -> emptyList() } } private fun lookup(ip: String): String { val ipParse = IpParseFactory.getIpParse(ip) - return "§f - IP: §e$ip \n" + + return "§f - IP: §e$ip \n" + "§f - 国家: §e${ipParse.getCountry()} \n" + "§f - 省市: §e${ipParse.getProvince()} ${ipParse.getCity()} \n" + "§f - ISP: §e${ipParse.getISP()} \n" + @@ -109,10 +158,36 @@ class PotatoIpDisplayCommand(plugin: PotatoIpDisplay) : TabExecutor { } private fun validate(ip: String): Boolean { - val ipRegex = """(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}""".toRegex() + val ipRegex = + """(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}""".toRegex() return ip.matches(ipRegex) } + private fun clear(target: String): Int { + val map = IpAttributeMap + var clearedItems = 0 + + when (target) { + "player" -> { + clearedItems += map.playerIpAttributeMap.size + map.playerIpAttributeMap.clear() + } + + "cache" -> { + clearedItems += map.ip2regionRawDataMap.size + clearedItems += map.pconlineRawDataMap.size + clearedItems += map.ipApiRawDataMap.size + map.ip2regionRawDataMap.clear() + map.pconlineRawDataMap.clear() + map.ipApiRawDataMap.clear() + } + + else -> {} + } + return clearedItems + } + + private fun sendMsg(sender: CommandSender, msg: String, showPrefix: Boolean) { val prefix = if (showPrefix) "§7[§6PotatoIPDisplay§7] " else "" sender.sendMessage("$prefix$msg") @@ -122,8 +197,4 @@ class PotatoIpDisplayCommand(plugin: PotatoIpDisplay) : TabExecutor { sender.sendMessage("§c您没有使用此命令的权限") } - init { - this.plugin = plugin - } - } \ No newline at end of file diff --git a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/integration/PlaceholderIntergration.kt b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/integration/PlaceholderIntergration.kt index 12e0c1d..f7ddc46 100644 --- a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/integration/PlaceholderIntergration.kt +++ b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/integration/PlaceholderIntergration.kt @@ -6,9 +6,8 @@ import me.clip.placeholderapi.expansion.PlaceholderExpansion import org.bukkit.entity.Player -class PlaceholderIntergration(plugin: PotatoIpDisplay) : PlaceholderExpansion() { - - private val plugin: PotatoIpDisplay +class PlaceholderIntergration : PlaceholderExpansion() { + private val plugin = PotatoIpDisplay.plugin override fun getAuthor(): String { return "[NightFish, yukonisen]" @@ -40,8 +39,4 @@ class PlaceholderIntergration(plugin: PotatoIpDisplay) : PlaceholderExpansion() } } - init { - this.plugin = plugin - } - } \ No newline at end of file diff --git a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/listener/MessageListener.kt b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/listener/MessageListener.kt index f5512fb..bb4bbf6 100644 --- a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/listener/MessageListener.kt +++ b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/listener/MessageListener.kt @@ -8,16 +8,16 @@ import org.bukkit.event.Listener import org.bukkit.event.player.AsyncPlayerChatEvent -class MessageListener(plugin: PotatoIpDisplay) : Listener { - private val plugin: PotatoIpDisplay - private val conf = plugin.conf +class MessageListener : Listener { + private val plugin = PotatoIpDisplay.plugin + @EventHandler(priority = EventPriority.LOWEST) fun onPlayerChat(event: AsyncPlayerChatEvent) { val playerName = event.player.name val ipAttr = IpAttributeMap.playerIpAttributeMap[playerName] ?: "未知" - val chatmsg = (conf.message.playerChat.string + val chatmsg = (plugin.conf.message.playerChat.string .replace("%ipAttr%", ipAttr) .replace("%playerName%", "%1\$s") .replace("%msg%", "%2\$s")) @@ -25,8 +25,4 @@ class MessageListener(plugin: PotatoIpDisplay) : Listener { event.format = chatmsg } - init { - this.plugin = plugin - } - } \ No newline at end of file diff --git a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/listener/PlayerJoinListener.kt b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/listener/PlayerJoinListener.kt index 9c114c6..24868c3 100644 --- a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/listener/PlayerJoinListener.kt +++ b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/listener/PlayerJoinListener.kt @@ -10,8 +10,8 @@ import org.bukkit.event.player.PlayerLoginEvent import org.bukkit.scheduler.BukkitRunnable -class PlayerJoinListener(plugin: PotatoIpDisplay) : Listener { - private val plugin: PotatoIpDisplay +class PlayerJoinListener: Listener { + val plugin = PotatoIpDisplay.plugin private val conf = plugin.conf @EventHandler @@ -37,10 +37,5 @@ class PlayerJoinListener(plugin: PotatoIpDisplay) : Listener { .replace("%ipAttr%", ipAttr)) } } - - init { - this.plugin = plugin - } - } diff --git a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/IpParseFactory.kt b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/IpParseFactory.kt index 0093b41..07f2215 100644 --- a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/IpParseFactory.kt +++ b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/IpParseFactory.kt @@ -4,14 +4,12 @@ import indi.nightfish.potato_ip_display.PotatoIpDisplay import indi.nightfish.potato_ip_display.parser.provider.Ip2regionParser import indi.nightfish.potato_ip_display.parser.provider.IpApiParser import indi.nightfish.potato_ip_display.parser.provider.PconlineParser -import org.bukkit.Bukkit object IpParseFactory { fun getIpParse(ip: String): IpParse{ - val plugin = Bukkit.getPluginManager().getPlugin("PotatoIpDisplay") as PotatoIpDisplay - val conf = plugin.conf - return when (val mode = conf.options.mode) { + val plugin = PotatoIpDisplay.plugin + return when (val mode = plugin.conf.options.mode) { "pconline" -> PconlineParser(ip) "ip2region" -> Ip2regionParser(ip) "ip-api" -> IpApiParser(ip) diff --git a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/provider/Ip2regionParser.kt b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/provider/Ip2regionParser.kt index 2b2b7d3..3fc419d 100644 --- a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/provider/Ip2regionParser.kt +++ b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/provider/Ip2regionParser.kt @@ -3,14 +3,13 @@ package indi.nightfish.potato_ip_display.parser.provider import indi.nightfish.potato_ip_display.PotatoIpDisplay import indi.nightfish.potato_ip_display.parser.IpParse import indi.nightfish.potato_ip_display.util.IpAttributeMap -import org.bukkit.Bukkit import org.lionsoul.ip2region.xdb.Searcher import java.io.File import java.util.concurrent.CompletableFuture class Ip2regionParser(private val ip: String) : IpParse { - private val plugin = Bukkit.getPluginManager().getPlugin("PotatoIpDisplay") as PotatoIpDisplay + private val plugin = PotatoIpDisplay.plugin private val dataFolder get() = plugin.dataFolder private val dbPath = File(dataFolder, "ip2region.xdb").toPath().toString() private val unknown: String = "未知" @@ -19,10 +18,8 @@ class Ip2regionParser(private val ip: String) : IpParse { private val searcher by lazy { when (xdbBuffer) { "none" -> Searcher.newWithFileOnly(dbPath) - "vindex" -> Searcher.newWithVectorIndex(dbPath, - Searcher.loadVectorIndexFromFile(dbPath)) - "cbuff" -> Searcher.newWithBuffer( - Searcher.loadContentFromFile(dbPath)) + "vindex" -> Searcher.newWithVectorIndex(dbPath, Searcher.loadVectorIndexFromFile(dbPath)) + "cbuff" -> Searcher.newWithBuffer(Searcher.loadContentFromFile(dbPath)) else -> throw IllegalArgumentException("Invalid xdbBuffer in config >> $xdbBuffer") } } @@ -76,9 +73,7 @@ class Ip2regionParser(private val ip: String) : IpParse { private fun getIp2regionDataAsync(): String { val map = IpAttributeMap.ip2regionRawDataMap[ip] - if (map != null) { - return map - } + if (map != null) return map val future = CompletableFuture() val thread = Thread { diff --git a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/provider/IpApiParser.kt b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/provider/IpApiParser.kt index 2573abb..ae7e5e5 100644 --- a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/provider/IpApiParser.kt +++ b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/provider/IpApiParser.kt @@ -13,7 +13,9 @@ import java.util.concurrent.CompletableFuture class IpApiParser(private val ip: String) : IpParse { private val get = getIpApiDataAsync() private val unknown: String = "未知" - private val isReservedRange: Boolean = (get["status"]?.asString == "fail") && (get["message"]?.asString == "reserved range") + private val isReservedRange: Boolean = + (get["status"]?.asString == "fail") && (get["message"]?.asString == "reserved range") + override fun getRegion(): String = get["regionName"]?.asString ?: unknown @@ -64,7 +66,8 @@ class IpApiParser(private val ip: String) : IpParse { IpAttributeMap.ipApiRawDataMap[ip] = jsonObject } } catch (_: Exception) { - val jsonObject: JsonObject = Gson().fromJson("{\"err\":\"failed\"}", JsonObject::class.java) + val jsonObject: JsonObject = + Gson().fromJson("{\"err\":\"failed\"}", JsonObject::class.java) future.complete(jsonObject) throw RuntimeException("Error while querying $ip. Common network problem.") } diff --git a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/provider/PconlineParser.kt b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/provider/PconlineParser.kt index 154f34c..9e97c32 100644 --- a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/provider/PconlineParser.kt +++ b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/parser/provider/PconlineParser.kt @@ -38,9 +38,7 @@ class PconlineParser(private val ip: String) : IpParse { override fun getFallback(): String { val values = arrayOf(getProvince(), getCountry(), getCity()) for (value in values) { - if (value.isNotBlank() && value != "") { - return value - } + if (value.isNotBlank() && value != "") return value } return unknown } diff --git a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/util/UpdateUtil.kt b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/util/UpdateUtil.kt index d5be20e..6779bc7 100644 --- a/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/util/UpdateUtil.kt +++ b/bukkit/src/main/kotlin/indi/nightfish/potato_ip_display/util/UpdateUtil.kt @@ -13,8 +13,7 @@ import kotlin.math.min object UpdateUtil { private val httpClient = HttpClient.newHttpClient() - private val plugin = Bukkit.getPluginManager().getPlugin("PotatoIpDisplay") as PotatoIpDisplay - + private val plugin = PotatoIpDisplay.plugin fun downloadDatabase(url: String, path: Path) { plugin.log("Start download database file") @@ -28,11 +27,13 @@ object UpdateUtil { if (response.statusCode() != 200) { throw IOException("Download database file FAILED: ${response.statusCode()}") } - plugin.log("Successful downloaded to $path") } catch (e: Exception) { - plugin.log("Download Failed! Please manually download from [$url],\n" + - "then move it to the plugin directory! (plugins\\PotatoIpDisplay\\{FILE})", Level.WARNING) + plugin.log( + "Download Failed! Please manually download from [$url],\n" + + "then move it to the plugin directory! (plugins\\PotatoIpDisplay\\{FILE})", + Level.WARNING + ) /*e.printStackTrace()*/ Bukkit.getPluginManager().disablePlugin(plugin) } @@ -41,24 +42,25 @@ object UpdateUtil { fun checkForUpdatesAsync(onComplete: (String) -> Unit) { val local: String = plugin.description.version + val githubUpdateURL = + "https://raw.githubusercontent.com/dmzz-yyhyy/PotatoIpDisplay/master/PLUGIN_VERSION" val thread = Thread { try { - val request = HttpRequest.newBuilder(URI.create("https://raw.githubusercontent.com/dmzz-yyhyy/PotatoIpDisplay/master/PLUGIN_VERSION")).GET().build() + val request = HttpRequest.newBuilder(URI.create(githubUpdateURL)).GET().build() val response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()) if (response.statusCode() == 200) { val remote = response.body().trim().split("=")[1] when (compareVersions(local, remote)) { -1 -> onComplete("§b有可用的更新: $remote") /* local < remote */ - 0 -> onComplete("§a已是最新。") /* local = remote */ + 0 -> onComplete("§a已是最新") /* local = remote */ 1 -> onComplete("§a已是最新。远程: §f$remote") /* local > remote, NEWER than remote??? */ else -> onComplete("unknown error while comparing versions: $local vs $remote") } - } else { - onComplete("§c无法获取当前远程版本。") - } + } else onComplete("§c无法获取当前远程版本") + return@Thread } catch (e: Exception) { - onComplete("§c无法从 GitHub 检查更新。") + onComplete("§c无法从 GitHub 检查更新") return@Thread } } @@ -66,7 +68,6 @@ object UpdateUtil { } - /* p=version[P]lugin, r=version[R]emote */ private fun compareVersions(p: String, r: String): Int { val px = p.split(".") @@ -82,5 +83,4 @@ object UpdateUtil { } - } \ No newline at end of file diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index 1f67471..c40ac6d 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -19,6 +19,7 @@ permissions: children: - potatoipdisplay.reload - potatoipdisplay.lookup + - potatoipdisplay.command potatoipdisplay.command: description: "Run PotatoIPDisplay commands" default: op