Skip to content

Commit

Permalink
Feat: refine xdb parsing
Browse files Browse the repository at this point in the history
add xdb buffer mode
use zh_CN config file
update README.md
  • Loading branch information
yukonisen committed Mar 31, 2024
1 parent c75c6d8 commit eb3000f
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 90 deletions.
46 changes: 5 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

一款用于显示玩家Ip归属地的Minecraft插件,支持 Bukkit 和 Velocity

## 使用文档(中文)
详见 [此处](https://upt.curiousers.org/docs/PotatoIpDisplay/intro)

## Placeholder API

| 变量 | 描述 | 返回示例 |
Expand All @@ -10,50 +13,11 @@
| `%potatoipdisplay_country%` | IP 所属国家 | `中国` |
| `%potatoipdisplay_province%` | IP 所属省份 | `上海` |
| `%potatoipdisplay_city%` | IP 所属城市 | `上海` |
| `%potatoipdisplay_district%` | IP 所属区 | `0`(可能无法识别 |
| `%potatoipdisplay_region%` | IP 所属区域 | `未知`(可能无法识别 |
| `%potatoipdisplay_isp%` | 运营商信息 | `联通` |

## 配置文件(中文)

```
# PotatoIpDisplay-bukkit 配置文件
# 一款用于显示玩家 IP 归属地的 Minecraft 插件
# 详情:[https://github.com/dmzz-yyhyy/PotatoIpDisplay]
# 请勿修改!
config-version: 1
![demo](assets/papidemo.png)

# 常规设置
options:
# 查询模式: "pconline"(在线 API)或 "ip2region"(本地)
# "ip2region" 模式为本地查询,插件会自动保存内置的文件至对应路径。
# 也可以从 [https://github.com/lionsoul2014/ip2region/tree/master/data] 下载
mode: "ip2region"
# 开启 bStats 统计
allow-bstats: true
# 消息设置
messages:
player-chat:
# 是否接管玩家消息事件。
# 与其他消息格式化插件冲突。如有,设为 false 并开启 PlaceholderAPI 支持。
enabled: true
# 消息格式
string: "§7[§b%ipAttr%§7] §f%playerName% §7>> §f%msg%"
player-login:
# 是否在玩家登录后发送一条消息显示 IP 归属地。
enabled: true
# 消息格式
string: "§7[§6PotatoIpDisplay§7] §e您当前IP归属地 §7[§b%ipAttr%§7]"
# Placeholder API 设置
papi:
# 启用 PAPI 支持。
enabled: false
# 可用变量:
# [https://github.com/dmzz-yyhyy/PotatoIpDisplay#placeholder-api]
```
## bStats
<a href="https://bstats.org/plugin/bukkit/PotatoIpDisplay/21473">![https://bstats.org/plugin/bukkit/PotatoIpDisplay/21473](https://bstats.org/signatures/bukkit/PotatoIpDisplay.svg)</a>

Expand Down
Binary file added assets/chatdemo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/papidemo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/main/kotlin/indi/nightfish/potato_ip_display/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import org.bukkit.configuration.file.FileConfiguration

data class Config(
val configVersion: Int,
val pluginConfigVersion: Int,
val options: Options,
val message: Message,
val papi: PAPISupport,
) {
data class Options(
val mode: String,
val xdbBuffer: String,
val allowbStats: Boolean
)

Expand All @@ -36,9 +38,11 @@ data class Config(
fun loadConfig(fc: FileConfiguration): Config {
return Config(
fc.getInt("config-version"),
1, // plugin reserved config version

Config.Options(
fc.getString("options.mode") ?: "ip2region",
fc.getString("options.xdb-buffer") ?: "vindex",
fc.getBoolean("options.allow-bstats")
),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PlaceholderIntergration(plugin: JavaPlugin) : PlaceholderExpansion() {
"country" -> ipParse.getCountry()
"province" -> ipParse.getProvince()
"city" -> ipParse.getCity()
"district" -> ipParse.getDistrict()
"region" -> ipParse.getRegion()
"isp" -> ipParse.getISP()
else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ class PlayerJoinListener : Listener {
val playerAddress = event.realAddress.hostAddress
val playerName = event.player.name
val ipParse = IpParseFactory.getIpParse(playerAddress)
IpATTRMap.playerIpATTRMap[playerName] = ipParse.getProvince()
var result = ipParse.getProvince()

if (result == "未知" || result == "") {
result = ipParse.getCity()
if (result == "未知" || result == "") {
result = ipParse.getCountry()
}
}

IpATTRMap.playerIpATTRMap[playerName] = result
Bukkit.getServer().logger.info("Player named $playerName connect to proxy from ${ipParse.getISP()}")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package indi.nightfish.potato_ip_display.parser

interface IpParse {
fun getDistrict(): String
fun getRegion(): String
fun getCountry(): String
fun getProvince(): String
fun getCity(): String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,80 @@
package indi.nightfish.potato_ip_display.parser.provider

import indi.nightfish.potato_ip_display.PotatoIpDisplay
import indi.nightfish.potato_ip_display.parser.IpParse
import org.bukkit.Bukkit
import org.lionsoul.ip2region.xdb.Searcher


class Ip2regionParser(private val ip: String) : IpParse {
private val dbPath: String = "plugins/PotatoIpDisplay/ip2region.xdb"
private val unknown: String = "未知"
private val plugin = Bukkit.getPluginManager().getPlugin("PotatoIpDisplay") as PotatoIpDisplay
private val xdbBuffer = plugin.conf.options.xdbBuffer

private val dbFile: String = "plugins/PotatoIpDisplay/ip2region.xdb"

private val searcher: Searcher by lazy {
Searcher.newWithFileOnly(dbFile)
private val searcher: Searcher = when (xdbBuffer) {
"none" -> {
Searcher.newWithFileOnly(dbPath)
}
"vindex" -> {
val vIndex: ByteArray = Searcher.loadVectorIndexFromFile(dbPath)
Searcher.newWithVectorIndex(dbPath, vIndex)
}
"cbuff" -> {
val cBuff: ByteArray = Searcher.loadContentFromFile(dbPath)
Searcher.newWithBuffer(cBuff)
}
else -> {
throw IllegalArgumentException("Invalid xdbBuffer in config >> $xdbBuffer")
}
}


/* Structure of the information returned:
INDEX: | 0 | 1 | 2 | 3 | 4 |
| COUNTRY | ? | PROVINCE | CITY | ISP |
IP1 | 中国 | 0 | 上海 | 上海市 | 联通 |
IP2 | 美国 | 0 | 加利福尼亚 | 0 | 0 |
IP3 | 0 | 0 | 0 | 内网IP | 内网IP |
INDEX: | 0 | 1 | 2 | 3 | 4 |
| COUNTRY | REGION | PROVINCE | CITY | ISP |
IP1 | 中国 | 0 | 上海 | 上海市 | 联通 |
IP2 | 美国 | 0 | 加利福尼亚 | 0 | 0 |
IP3 | 0 | 0 | 0 | 内网IP | 内网IP |
*/

override fun getDistrict(): String {
return "null"
override fun getRegion(): String {
// ip2region doesn't seem to be able to return region information
val region = try {
searcher.search(ip).split("|")[1]
} catch (e: Exception) { "" }
return if (region == "0") unknown else region
}

override fun getCountry(): String {
val country = try {
searcher.search(ip).split("|")[0]
} catch (e: Exception) {
""
}
// If null replace with getCity()
return if (country == "0") getCity() else country
} catch (e: Exception) { "" }
return if (country == "0") unknown else country
}

override fun getProvince(): String {
val province = try {
searcher.search(ip).split("|")[2].replace("", "")
} catch (e: Exception) {
""
}
} catch (e: Exception) { "" }
// If null replace with getCountry()
return if (province == "0") getCountry() else province
return if (province == "0") unknown else province
}


override fun getCity(): String {
return try {
val city = try {
searcher.search(ip).split("|")[3].replace("", "")
} catch (e: Exception) {
""
}
} catch (e: Exception) { "" }
return if (city == "0") unknown else city
}

override fun getISP(): String {
return try {
val isp = try {
searcher.search(ip).split("|")[4]
} catch (e: Exception) {
""
}
} catch (e: Exception) { "" }
return if (isp == "0") unknown else isp
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import indi.nightfish.potato_ip_display.parser.IpParse

class PconlineParser(ip: String) : IpParse {
private val parseData: IpGson = Gson().fromJson("https://whois.pconline.com.cn/ipJson.jsp?ip=$ip&json=true", IpGson::class.java)
override fun getDistrict(): String = parseData.region
override fun getRegion(): String = parseData.region

override fun getCountry(): String = if (parseData.pro.replace("", "") == "") {
Regex(pattern = """[\u4e00-\u9fa5]+""")
Expand Down
43 changes: 25 additions & 18 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
# Configurations for PotatoIpDisplay-bukkit,
# A Plugin for Checking Player Ip Attributions.
# Learn more at [https://github.com/dmzz-yyhyy/PotatoIpDisplay]
# PotatoIpDisplay-bukkit 配置文件
# 一款用于显示玩家 IP 归属地的 Minecraft 插件
# 详情:[https://github.com/dmzz-yyhyy/PotatoIpDisplay]

# DO NOT TOUCH THIS!
# 请勿修改!
config-version: 1

# General options
# 常规设置
options:
# Working API: "pconline"(remote) or "ip2region"(local)
# "ip2region" depends on ip2region.xdb in the plugin directory to work.
# Get it from [https://github.com/lionsoul2014/ip2region/tree/master/data]
# 查询模式: "pconline"(在线 API)或 "ip2region"(本地)
# "ip2region" 模式为本地查询,插件会自动保存内置的文件至对应路径。
# 也可以从 [https://github.com/lionsoul2014/ip2region/tree/master/data] 下载
mode: "ip2region"
# Allow bStats analytics
# 缓存模式:只有模式设置为 ip2region 时生效,用于加速查询。
# "none": 完全基于文件,不进行缓存。
# "vindex": 额外占用固定的 512 KB 内存,缓存 VectorIndex 数据。
# "cbuff": 额外占用文件大小的内存,缓存整个 ip2region.xdb。
xdb-buffer: "vindex"
# 开启 bStats 统计
allow-bstats: true

# 消息设置
messages:
# Messages sent by players. Use plugin to format
player-chat:
# Whether to listen for, and take over player chat messages
enabled: true
# 是否接管玩家消息事件。
# 与其他消息格式化插件冲突。
enabled: false
# 消息格式
string: "§7[§b%ipAttr%§7] §f%playerName% §7>> §f%msg%"

# Send a message to the player after logging in
player-login:
# Send a message after player login
# 是否在玩家登录后发送一条消息显示 IP 归属地。
enabled: true
# 消息格式
string: "§7[§6PotatoIpDisplay§7] §e您当前IP归属地 §7[§b%ipAttr%§7]"

# Placeholder API options
# Placeholder API 设置
papi:
# Enable PAPI support.
# 启用 PAPI 支持。需要安装 Placeholder API。
enabled: false
# Available placeholders:
# [https://github.com/dmzz-yyhyy/PotatoIpDisplay#placeholder-api]
# 可用变量:
# [https://github.com/dmzz-yyhyy/PotatoIpDisplay#placeholder-api]

0 comments on commit eb3000f

Please sign in to comment.