-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: improvements & add local ip2region support
- Loading branch information
Showing
15 changed files
with
243 additions
and
123 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
64 changes: 64 additions & 0 deletions
64
src/main/kotlin/indi/nightfish/potato_ip_display/PIPDConfig.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,64 @@ | ||
package indi.nightfish.potato_ip_display | ||
|
||
import org.bukkit.configuration.file.FileConfiguration | ||
|
||
data class PIPDConfig( | ||
val configVersion: Int, | ||
val options: Options, | ||
val message: Message, | ||
val papi: PAPISupport, | ||
) { | ||
data class Options( | ||
val mode: String | ||
) | ||
|
||
data class Message( | ||
val playerChat: PlayerChat, | ||
val playerLogin: PlayerLogin | ||
) { | ||
data class PlayerChat( | ||
val enabled: Boolean, | ||
val string: String | ||
) | ||
|
||
data class PlayerLogin( | ||
val enabled: Boolean, | ||
val string: String | ||
) | ||
} | ||
|
||
data class PAPISupport( | ||
val enabled: Boolean, | ||
val format: String | ||
) | ||
} | ||
|
||
fun loadPIPDConfig(fc: FileConfiguration): PIPDConfig { | ||
return PIPDConfig( | ||
fc.getInt("config-version"), | ||
|
||
PIPDConfig.Options( | ||
fc.getString("options.mode") | ||
?: "ip2region" | ||
), | ||
|
||
PIPDConfig.Message( | ||
PIPDConfig.Message.PlayerChat( | ||
fc.getBoolean("messages.player-chat.enabled"), | ||
fc.getString("messages.player-chat.string") | ||
?: "§7[§b\$ipAttr§7] §f\$playerName §7>> §f\$msg" // FALLBACK CHAT FORMAT | ||
), | ||
PIPDConfig.Message.PlayerLogin( | ||
fc.getBoolean("messages.player-login.enabled"), | ||
fc.getString("messages.player-login.string") | ||
?: "§7[§6PotatoIpDisplay§7] §e您当前ip归属地 §7[§b\$ipAttr§7]" | ||
) | ||
), | ||
|
||
PIPDConfig.PAPISupport( | ||
fc.getBoolean("papi.enabled"), | ||
fc.getString("papi.format") ?: "" | ||
|
||
) | ||
) | ||
} |
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
7 changes: 0 additions & 7 deletions
7
src/main/kotlin/indi/nightfish/potato_ip_display/PotatoPlugin.kt
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
src/main/kotlin/indi/nightfish/potato_ip_display/ip/Ip2regionParse.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,63 @@ | ||
package indi.nightfish.potato_ip_display.ip | ||
|
||
import org.lionsoul.ip2region.xdb.Searcher | ||
|
||
|
||
class Ip2regionParse(private val ip: String) : IpParse { | ||
|
||
private val dbFile: String = "plugins/PotatoIpDisplay/ip2region.xdb" | ||
|
||
private val searcher: Searcher by lazy { | ||
Searcher.newWithFileOnly(dbFile) | ||
} | ||
|
||
/* 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 | | ||
*/ | ||
|
||
override fun getRegion(): String { | ||
return "null" | ||
} | ||
|
||
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 | ||
} | ||
|
||
override fun getProvince(): String { | ||
val province = try { | ||
searcher.search(ip).split("|")[2].replace("省", "") | ||
} catch (e: Exception) { | ||
"" | ||
} | ||
// If null replace with getCountry() | ||
return if (province == "0") getCountry() else province | ||
} | ||
|
||
|
||
override fun getCity(): String { | ||
return try { | ||
searcher.search(ip).split("|")[3].replace("市", "") | ||
} catch (e: Exception) { | ||
"" | ||
} | ||
} | ||
|
||
override fun getISP(): String { | ||
return try { | ||
searcher.search(ip).split("|")[4] | ||
} catch (e: Exception) { | ||
"" | ||
} | ||
} | ||
} |
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
12 changes: 11 additions & 1 deletion
12
src/main/kotlin/indi/nightfish/potato_ip_display/ip/IpParseFactory.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 |
---|---|---|
@@ -1,7 +1,17 @@ | ||
package indi.nightfish.potato_ip_display.ip | ||
|
||
import indi.nightfish.potato_ip_display.PotatoIpDisplay | ||
import org.bukkit.Bukkit | ||
|
||
object IpParseFactory { | ||
|
||
fun getIpParse(ip: String): IpParse{ | ||
return WebIpParse(ip) | ||
val plugin = Bukkit.getPluginManager().getPlugin("PotatoIpDisplay") as PotatoIpDisplay | ||
val conf = plugin.conf | ||
return when (val mode = conf.options.mode) { | ||
"pconline" -> PconlineParse(ip) | ||
"ip2region" -> Ip2regionParse(ip) | ||
else -> throw IllegalArgumentException("Invalid mode in config >> $mode") | ||
} | ||
} | ||
} |
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
Oops, something went wrong.