Skip to content

Commit

Permalink
更新区块实体统计和世界实体统计
Browse files Browse the repository at this point in the history
  • Loading branch information
4o4E committed Mar 14, 2022
1 parent 69ff11c commit 2f65ddf
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 16 deletions.
4 changes: 1 addition & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@

rootProject.name = "EClean"

rootProject.name = "EClean"
59 changes: 59 additions & 0 deletions src/main/kotlin/com/e404/clean/Check.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.e404.clean

import com.e404.clean.Clean.info
import com.e404.clean.util.Log.color
import com.e404.clean.util.sendMsgWithPrefix
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.entity.EntityType

/**
* 检查世界实体
*/
object Check {
fun CommandSender.sendWorldStats(worldName: String) {
val world = Bukkit.getWorld(worldName)
if (world == null) {
sendMsgWithPrefix("&c不存在名为&e${world}&c的世界")
return
}
val entity = world
.entities
.groupBy { it.type }
.map { (k, v) -> k to v.size }
.sortedByDescending { it.second }
.joinToString("&7, ") { (k, v) -> "&f$k: ${v.withColor()}" }
sendMessage("""&f世界&a${worldName}&f共加载区块${world.loadedChunks.size}
|&b实体统计信息:
|$entity""".trimMargin().color())
}

fun CommandSender.sendEntityStats(worldName: String, typeName: String, min: Int = 0) {
val world = Bukkit.getWorld(worldName)
if (world == null) {
sendMsgWithPrefix("&c不存在名为&e${worldName}&c的世界")
return
}
kotlin.runCatching {
EntityType.valueOf(typeName.uppercase())
}.onFailure {
sendMsgWithPrefix("&e${typeName}&c不是有效的实体类型")
}.onSuccess { type ->
val entity = world
.loadedChunks
.map { it.info() to it.entities.count { e -> e.type == type } }
.filter { it.second > min }
.sortedByDescending { e -> e.second }
.joinToString("\n") { (k, v) -> "&f$k: ${v.withColor()}" }
.color()
.let { s -> if (s == "") "&c无结果" else s }
sendMessage("&f实体&e${typeName}&f的统计信息\n$entity".color())
}
}

private fun Int.withColor() = when {
this > 60 -> "&c$this"
this > 30 -> "&e$this"
else -> "&a$this"
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/e404/clean/Clean.kt
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,5 @@ object Clean {
}
}

private fun Chunk.info() = """区块x: $x(${x * 16} - ${(x + 1) * 16}), z: $z(${z * 16} - ${(z + 1) * 16})"""
fun Chunk.info() = """区块: ($x, $z) 坐标: (${x * 16 + 8}, ${z * 16 + 8})"""
}
72 changes: 62 additions & 10 deletions src/main/kotlin/com/e404/clean/EClean.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.e404.clean

import com.e404.clean.util.Log
import com.e404.clean.Check.sendEntityStats
import com.e404.clean.Check.sendWorldStats
import com.e404.clean.util.*
import com.e404.clean.util.Log.color
import com.e404.clean.util.checkPerm
import com.e404.clean.util.logo
import com.e404.clean.util.sendMsgWithPrefix
import org.bstats.bukkit.Metrics
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.entity.EntityType
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin

class EClean : JavaPlugin() {
Expand Down Expand Up @@ -48,6 +50,17 @@ class EClean : JavaPlugin() {
Log.info("&aEClean已启用, 作者404E".color())
}

private val help = """
&bEClean 作者404E
&a/eclean reload &f- 重载插件
&a/eclean clean &f- 马上触发一次清理
&a/eclean stats &f- 统计当前所在世界的实体和区块
&a/eclean stats <世界名> &f- 统计实体和区块
&a/eclean entity <实体名> &f- 统计当前世界的实体
&a/eclean entity <实体名> <世界名> &f- 统计实体
&a/eclean entity <实体名> <世界名> <纳入统计所需数量> &f- 统计实体
""".trimIndent().color()

override fun onCommand(
sender: CommandSender,
command: Command,
Expand All @@ -56,11 +69,7 @@ class EClean : JavaPlugin() {
): Boolean {
if (!sender.checkPerm("eclean.admin")) return true
if (args.isEmpty()) {
sender.sendMessage("""&bEClean 作者404E
|&a/eclean reload &f- 重载插件
|&a/eclean clean &f- 马上触发一次清理"""
.trimMargin()
.color())
sender.sendMessage(help)
return true
}
when (args[0].lowercase()) {
Expand All @@ -72,6 +81,42 @@ class EClean : JavaPlugin() {
load(sender)
sender.sendMsgWithPrefix("&a重载结束")
}
"stats" -> {
when (args.size) {
1 -> {
if (!sender.isPlayer()) return true
sender.sendWorldStats((sender as Player).world.name)
}
2 -> sender.sendWorldStats(args[1])
else -> sender.sendMessage("""
&a/eclean stats &f- 统计当前所在世界的实体和区块统计
&a/eclean stats <世界名> &f- 统计实体和区块统计
""".trimIndent().color())
}
}
"entity" -> {
when (args.size) {
2 -> {
if (!sender.isPlayer()) return true
sender.sendEntityStats((sender as Player).world.name, args[1])
}
3 -> sender.sendEntityStats(args[2], args[1])
4 -> {
val min = args[3].toIntOrNull()
if (min == null) {
sender.sendMsgWithPrefix("${args[3]}不是有效数字")
return true
}
sender.sendEntityStats(args[2], args[1], min)
}
else -> sender.sendMessage("""
&a/eclean entity <实体名> &f- 统计当前世界的实体统计
&a/eclean entity <实体名> <世界名> &f- 统计实体统计
&a/eclean entity <实体名> <世界名> <纳入统计所需数量> &f- 统计实体统计
""".trimIndent().color())
}
}
else -> sender.sendUnknow()
}
return true
}
Expand All @@ -83,7 +128,14 @@ class EClean : JavaPlugin() {
args: Array<out String>,
): MutableList<String>? {
if (!sender.hasPermission("eclean.admin")) return null
if (args.size == 1) return arrayListOf("reload", "clean")
when (args.size) {
1 -> return arrayListOf("reload", "clean", "stats", "entity")
2 -> when (args[0].lowercase()) {
"stats" -> return Bukkit.getWorlds().map { it.name }.toMutableList()
"entity" -> return EntityType.values().map { it.name }.toMutableList()
}
3 -> if (args[0].equals("entity", true)) return Bukkit.getWorlds().map { it.name }.toMutableList()
}
return null
}
}
27 changes: 25 additions & 2 deletions src/main/kotlin/com/e404/clean/util/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.e404.clean.EClean
import com.e404.clean.util.Log.color
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player

/**
* 发送带前缀并且替换&的消息
Expand Down Expand Up @@ -46,12 +47,34 @@ fun CommandSender.checkPerm(perm: String): Boolean {
return false
}

/**
* 检查sender是否是玩家, 若不是则发送提醒消息
*
* @return 若sender是player则返回true
*/
fun CommandSender.isPlayer(): Boolean {
if (this is Player) return true
sendNonPlayer()
return false
}

/**
* 无权限
*/
fun CommandSender.sendNoperm() {
fun CommandSender.sendNoperm() =
sendMsgWithPrefix("&c无权限")
}

/**
* 非玩家
*/
fun CommandSender.sendNonPlayer() =
sendMsgWithPrefix("&c此指令仅玩家可用")

/**
* 未知指令
*/
fun CommandSender.sendUnknow() =
sendMsgWithPrefix("&c未知指令")

/**
* 文本匹配正则列表
Expand Down

0 comments on commit 2f65ddf

Please sign in to comment.