Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docs and commands #25

Merged
merged 7 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Binary file removed assets/chatdemo.png
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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}]")
}

Expand All @@ -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)
Expand All @@ -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) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,30 @@ 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<String>): Boolean {
override fun onCommand(
sender: CommandSender,
cmd: Command,
label: String,
args: Array<String>
): Boolean {

if (!sender.hasPermission("potatoipdisplay.command")) {
sendNoPerms(sender)
return true
}

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)
Expand All @@ -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)
Expand Down Expand Up @@ -80,39 +98,96 @@ 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
}
}
}

override fun onTabComplete(sender: CommandSender, command: Command, alias: String, args: Array<String>): List<String> {
override fun onTabComplete(
sender: CommandSender,
command: Command,
alias: String,
args: Array<String>
): List<String> {
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" +
"§f - IP属地: §a${ipParse.getFallback()}"
}

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")
Expand All @@ -122,8 +197,4 @@ class PotatoIpDisplayCommand(plugin: PotatoIpDisplay) : TabExecutor {
sender.sendMessage("§c您没有使用此命令的权限")
}

init {
this.plugin = plugin
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand Down Expand Up @@ -40,8 +39,4 @@ class PlaceholderIntergration(plugin: PotatoIpDisplay) : PlaceholderExpansion()
}
}

init {
this.plugin = plugin
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,21 @@ 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"))

event.format = chatmsg
}

init {
this.plugin = plugin
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,10 +37,5 @@ class PlayerJoinListener(plugin: PotatoIpDisplay) : Listener {
.replace("%ipAttr%", ipAttr))
}
}

init {
this.plugin = plugin
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading
Loading