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

feat: commands #23

Merged
merged 5 commits into from
Apr 27, 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
2 changes: 1 addition & 1 deletion PLUGIN_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PLUGIN=1.3.0
PLUGIN=1.4.0
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@
## 详细使用文档(中文)
详见 [此处](https://upt.curiousers.org/docs/PotatoIpDisplay/intro) 。

## 命令与权限

| 命令 | 描述 | 权限 |
|:------------------------------------|:--------|:--------------------------|
| /pipd | 等效 | `potatoipdisplay.command` |
| /potatoipdisplay | 插件信息 | `potatoipdisplay.command` |
| /potatoipdisplay lookup <玩家\|IPv4 > | 查询玩家或IP | `potatoipdisplay.lookup` |
| /potatoipdisplay reload | 重载插件 | `potatoipdisplay.reload` |

## Placeholder API

| 变量 | 描述 | 返回内容(`ip2region`) | 返回内容(`pconline`) |
|:-----------------------------|:------------|:------------------|:-----------------|
| `%potatoipdisplay_ip%` | 玩家的 IP | `11.45.1.4` | `11.45.1.4` |
| `%potatoipdisplay_country%` | IP 所属国家 | `中国` | `中国` |
| `%potatoipdisplay_province%` | IP 所属省份 | `上海` | `上海市` |
| `%potatoipdisplay_city%` | IP 所属城市 | `上海` | `上海` |
| `%potatoipdisplay_region%` | IP 所属区域 | `0` | ` ` |
| `%potatoipdisplay_isp%` | 运营商信息 | `联通` | `上海市 联通` |
| `%potatoipdisplay_fallback%` | fallback 变量 | `上海` | `上海` |
| 变量 | 描述 |
|:-----------------------------|:------------|
| `%potatoipdisplay_ip%` | 玩家的 IP |
| `%potatoipdisplay_country%` | IP 所属国家 |
| `%potatoipdisplay_province%` | IP 所属省份 |
| `%potatoipdisplay_city%` | IP 所属城市 |
| `%potatoipdisplay_region%` | IP 所属区域 |
| `%potatoipdisplay_isp%` | 运营商信息 |
| `%potatoipdisplay_fallback%` | fallback 变量 |

#### 什么是 fallback 变量?

Expand All @@ -42,7 +51,7 @@
![demo](assets/papidemo.png)

## 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>
![bStats](https://bstats.org/signatures/bukkit/PotatoIpDisplay.svg)</a>

## LICENSE
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package indi.nightfish.potato_ip_display

import indi.nightfish.potato_ip_display.command.PotatoIpDisplayCommand
import indi.nightfish.potato_ip_display.integration.PlaceholderIntergration
import indi.nightfish.potato_ip_display.listener.MessageListener
import indi.nightfish.potato_ip_display.listener.PlayerJoinListener
Expand All @@ -8,6 +9,7 @@ import indi.nightfish.potato_ip_display.util.UpdateUtil
import indi.nightfish.potato_ip_display.util.loadConfig
import me.clip.placeholderapi.metrics.bukkit.Metrics
import org.bukkit.Bukkit
import org.bukkit.event.HandlerList
import org.bukkit.plugin.java.JavaPlugin
import java.io.File
import java.util.logging.Level
Expand All @@ -22,30 +24,10 @@ class PotatoIpDisplay : JavaPlugin() {

override fun onEnable() {
super.onEnable()
val pm = Bukkit.getPluginManager()
conf = loadConfig(config)

initPlugin()
initResources()

if (conf.papi.enabled) {
if (pm.getPlugin("PlaceholderAPI") != null) {
PlaceholderIntergration().register()
} else {
throw RuntimeException("PlaceholderAPI enabled in config but NOT installed!")
}
}

/* 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)
}
pm.registerEvents(PlayerJoinListener(), this)

/* Registering commands */
/*getCommand("potatoipdisplay")!!.setExecutor(PotatoIpDisplayCommand())
getCommand("pipd")!!.setExecutor(PotatoIpDisplayCommand())*/

if (conf.options.allowbStats) {
Metrics(this, 21473)
}
Expand All @@ -61,6 +43,11 @@ class PotatoIpDisplay : JavaPlugin() {
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()
}
Expand All @@ -75,6 +62,33 @@ class PotatoIpDisplay : JavaPlugin() {
log("ip2region.xdb saved to plugin directory.")
}
}

}

fun initPlugin() {
val pm = Bukkit.getPluginManager()
conf = loadConfig(config)

PlaceholderIntergration(this).unregister()
if (conf.papi.enabled) {
if (pm.getPlugin("PlaceholderAPI") != null) {
PlaceholderIntergration(this).register()
} else throw RuntimeException("PlaceholderAPI enabled in config but NOT installed!")
}

/* Unregistering events" */
HandlerList.unregisterAll()

/* 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)
if (conf.message.playerLogin.enabled)
pm.registerEvents(PlayerJoinListener(this), this)

/* Registering commands */
getCommand("potatoipdisplay")!!.setExecutor(PotatoIpDisplayCommand(this))
getCommand("pipd")!!.setExecutor(PotatoIpDisplayCommand(this))
}

fun log(message: String, level: Level = Level.INFO) =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,129 @@
package indi.nightfish.potato_ip_display.command

import indi.nightfish.potato_ip_display.PotatoIpDisplay
import indi.nightfish.potato_ip_display.parser.IpParseFactory
import indi.nightfish.potato_ip_display.util.IpAttributeMap
import indi.nightfish.potato_ip_display.util.UpdateUtil
import indi.nightfish.potato_ip_display.util.loadConfig
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
import org.bukkit.command.CommandSender
import org.bukkit.command.TabExecutor

/**
* The /potatoipdisplay command.
*/
class PotatoIpDisplayCommand : CommandExecutor {
class PotatoIpDisplayCommand(plugin: PotatoIpDisplay) : TabExecutor {
private val plugin: PotatoIpDisplay

private val labelList = arrayOf("reload", "version")
private val plugin = Bukkit.getPluginManager().getPlugin("PotatoIpDisplay") as PotatoIpDisplay
override fun onCommand(sender: CommandSender, cmd: Command, label: String, args: Array<String>): Boolean {

return true
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, "§7尝试检查更新……", false)
UpdateUtil.checkForUpdatesAsync { result ->
sendMsg(sender, result, false)
}
return true
}

when (args[0].lowercase()) {
"reload" -> {
if (!sender.hasPermission("potatoipdisplay.reload")) {
sendNoPerms(sender)
return true
}
plugin.reloadConfig()
plugin.conf = loadConfig(plugin.config)
plugin.initPlugin()
sendMsg(sender, "§a重载成功!", true)
return true
}
"lookup" -> {
if (!sender.hasPermission("potatoipdisplay.lookup")) {
sendNoPerms(sender)
return true
}
if (args.size < 2) {
sendMsg(sender, "§e/$label lookup [玩家] §f>> 查询在线玩家", false)
sendMsg(sender, "§e/$label lookup [IPv4] §f>> 查询 IPv4", false)
return true
}

val target = args[1]

val player = Bukkit.getServer().getPlayer(target)
if (player != null) {
sendMsg(sender, "§f查询玩家: §b$target", true)
val playerAddress: String = player.address?.address.toString().replace("/", "")
sendMsg(sender, lookup(playerAddress), false)
return true
}

if (validate(target)) {
sendMsg(sender, "§f查询 IPv4: §b$target", true)
sendMsg(sender, lookup(target), false)
} else sendMsg(sender, "§c查询的玩家离线,或 IPv4 无效", true)
return true
}
else -> {
sendMsg(sender, "§c未知命令", true)
return true
}
}
}

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 == 2 && (args[0] == "lookup") -> {
val matchingPlayers = plugin.server.onlinePlayers.map { it.name }.filter { it.startsWith(args[1]) }
matchingPlayers + listOf("127.0.0.1")
}
else -> emptyList()
}
}

private fun lookup(ip: String): String {
val ipParse = IpParseFactory.getIpParse(ip)
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()
return ip.matches(ipRegex)
}

private fun sendMsg(sender: CommandSender, msg: String, showPrefix: Boolean) {
val prefix = if (showPrefix) "§7[§6PotatoIPDisplay§7] " else ""
sender.sendMessage("$prefix$msg")
}

private fun sendNoPerms(sender: CommandSender) {
sender.sendMessage("§c您没有使用此命令的权限")
}

init {
this.plugin = plugin
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package indi.nightfish.potato_ip_display.integration
import indi.nightfish.potato_ip_display.PotatoIpDisplay
import indi.nightfish.potato_ip_display.parser.IpParseFactory
import me.clip.placeholderapi.expansion.PlaceholderExpansion
import org.bukkit.Bukkit
import org.bukkit.entity.Player


class PlaceholderIntergration : PlaceholderExpansion() {
class PlaceholderIntergration(plugin: PotatoIpDisplay) : PlaceholderExpansion() {

private val plugin = Bukkit.getPluginManager().getPlugin("PotatoIpDisplay") as PotatoIpDisplay
private val plugin: PotatoIpDisplay

override fun getAuthor(): String {
return "[NightFish, yukonisen]"
Expand Down Expand Up @@ -41,4 +40,8 @@ class PlaceholderIntergration : PlaceholderExpansion() {
}
}

init {
this.plugin = plugin
}

}
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package indi.nightfish.potato_ip_display.listener

import indi.nightfish.potato_ip_display.util.IpAttributeMap
import indi.nightfish.potato_ip_display.PotatoIpDisplay
import me.clip.placeholderapi.PlaceholderAPI
import org.bukkit.Bukkit
import indi.nightfish.potato_ip_display.util.IpAttributeMap
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.player.AsyncPlayerChatEvent


class MessageListener : Listener {
private val plugin = Bukkit.getPluginManager().getPlugin("PotatoIpDisplay") as PotatoIpDisplay
class MessageListener(plugin: PotatoIpDisplay) : Listener {
private val plugin: PotatoIpDisplay
private val conf = plugin.conf
@EventHandler(priority = EventPriority.LOWEST)

fun onPlayerChat(event: AsyncPlayerChatEvent) {
val playerName = event.player.name
var msg = event.message
val ipAttr = IpAttributeMap.playerIpAttributeMap[playerName] ?: "未知"

if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
msg = PlaceholderAPI.setPlaceholders(event.player, msg)
}
Bukkit.getServer().broadcastMessage(conf.message.playerChat.string
val chatmsg = (conf.message.playerChat.string
.replace("%ipAttr%", ipAttr)
.replace("%playerName%", playerName)
.replace("%msg%", msg))
event.isCancelled = true
.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 @@ -3,16 +3,15 @@ package indi.nightfish.potato_ip_display.listener
import indi.nightfish.potato_ip_display.PotatoIpDisplay
import indi.nightfish.potato_ip_display.parser.IpParseFactory
import indi.nightfish.potato_ip_display.util.IpAttributeMap
import org.bukkit.Bukkit
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerLoginEvent
import org.bukkit.scheduler.BukkitRunnable


class PlayerJoinListener : Listener {
private val plugin = Bukkit.getPluginManager().getPlugin("PotatoIpDisplay") as PotatoIpDisplay
class PlayerJoinListener(plugin: PotatoIpDisplay) : Listener {
private val plugin: PotatoIpDisplay
private val conf = plugin.conf

@EventHandler
Expand All @@ -25,7 +24,7 @@ class PlayerJoinListener : Listener {
val ipParse = IpParseFactory.getIpParse(playerAddress)
val result = ipParse.getFallback()
IpAttributeMap.playerIpAttributeMap[playerName] = result
plugin.log("Player named $playerName connect to proxy from ${ipParse.getProvince()}${ipParse.getCity()} ${ipParse.getISP()}")
plugin.log("Player named $playerName connected from ${ipParse.getProvince()}${ipParse.getCity()} ${ipParse.getISP()}")
}
}.run()
}
Expand All @@ -38,5 +37,10 @@ class PlayerJoinListener : Listener {
.replace("%ipAttr%", ipAttr))
}
}

init {
this.plugin = plugin
}

}

Loading
Loading