Skip to content

Commit

Permalink
补全垃圾桶清理的papi, 优化代码逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
4o4E committed Sep 27, 2023
1 parent 42455ea commit c96a031
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@
2023.07.21 - 1.16.0 在1.8也使用utf8作为配置文件编码, 避免转码; 修复1.8中无法正常使用的bug; 添加菜单的右键移除实体功能; 支持密集实体清理中多种实体共用一个上限; 修复1.8中不兼容的音效的问题
2023.08.31 - 1.16.1 优化代码, 修复配置文件中区块清理配置和注释相反的问题
2023.09.27 - 1.17.0 补全单元测试, 优化处理逻辑和debug信息, 现在debug信息会完整输出搜索的过程细节, 添加公共垃圾箱功能
2023.09.27 - 1.17.1 补全垃圾桶清理的papi, 优化代码逻辑
```

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "top.e404"
version = "1.17.0"
version = "1.17.1"
val epluginVer = "1.2.0"

fun eplugin(module: String, version: String = epluginVer) = "top.e404:eplugin-$module:$version"
Expand Down
27 changes: 27 additions & 0 deletions src/main/kotlin/clean/Trashcan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.bukkit.event.inventory.InventoryCloseEvent
import org.bukkit.inventory.ItemStack
import org.bukkit.scheduler.BukkitTask
import top.e404.eclean.PL
import top.e404.eclean.config.Config
import top.e404.eclean.menu.MenuManager
import top.e404.eclean.menu.trashcan.TrashInfo
import top.e404.eclean.menu.trashcan.TrashcanMenu
Expand All @@ -23,6 +24,32 @@ object Trashcan : EListener(PL) {

var task: BukkitTask? = null

/**
* 垃圾桶清空倒计时, 单位秒
*/
var countdown = 0L

fun schedule() {
task?.cancel()
val duration = Config.config.trashcan.duration
if (duration == null) {
task = null
return
}
if (!Config.config.trashcan.enable) return
countdown = duration
task = Config.plugin.runTaskTimer(20, 20) {
countdown--
if (countdown <= 0L) {
countdown = duration
Config.plugin.debug { "清空垃圾桶" }
trashData.clear()
trashValues.clear()
update()
}
}
}

fun ItemStack.sign() = ItemSign(this)
class ItemSign(val item: ItemStack) {
override fun equals(other: Any?): Boolean {
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/command/Trash.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import top.e404.eclean.PL
import top.e404.eclean.clean.Trashcan
import top.e404.eclean.config.Config
import top.e404.eclean.config.Lang
import top.e404.eplugin.command.ECommand

Expand All @@ -18,6 +19,10 @@ object Trash : ECommand(

override fun onCommand(sender: CommandSender, args: Array<out String>) {
sender as Player
if (!Config.config.trashcan.enable) {
plugin.sendMsgWithPrefix(sender, Lang["command.trash_disable"])
return
}
Trashcan.open(sender)
plugin.sendMsgWithPrefix(sender, Lang["command.trash_open"])
}
Expand Down
26 changes: 3 additions & 23 deletions src/main/kotlin/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,10 @@ object Config : KtxConfig<ConfigData>(
// 加载时处理清理task
override fun onLoad(config: ConfigData, sender: CommandSender?) {
if (Bukkit.isPrimaryThread()) {
Trashcan.task?.cancel()
val duration = config.trashcan.duration
if (duration == null) {
Trashcan.task = null
return
}
Trashcan.task = plugin.runTaskTimer(duration, duration) {
Trashcan.trashData.clear()
Trashcan.trashValues.clear()
}
Trashcan.schedule()
return
}
plugin.runTask {
Trashcan.task?.cancel()
val duration = config.trashcan.duration
if (duration == null) {
Trashcan.task = null
return@runTask
}
Trashcan.task = plugin.runTaskTimer(duration, duration) {
Trashcan.trashData.clear()
Trashcan.trashValues.clear()
}
}
plugin.runTask { Trashcan.schedule() }
}
}

Expand Down Expand Up @@ -110,5 +90,5 @@ data class ChunkConfig(
data class TrashcanConfig(
var enable: Boolean = true,
var collect: Boolean = true,
var duration: Long? = null,
var duration: Long? = 6000,
)
11 changes: 7 additions & 4 deletions src/main/kotlin/papi/Papi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package top.e404.eclean.papi
import org.bukkit.OfflinePlayer
import org.bukkit.entity.Player
import top.e404.eclean.PL
import top.e404.eclean.clean.Clean
import top.e404.eclean.clean.lastChunk
import top.e404.eclean.clean.lastDrop
import top.e404.eclean.clean.lastLiving
import top.e404.eclean.clean.*
import top.e404.eclean.config.Config
import top.e404.eplugin.hook.placeholderapi.PapiExpansion
import top.e404.eplugin.util.parseSecondAsDuration
Expand All @@ -19,6 +16,8 @@ import top.e404.eplugin.util.parseSecondAsDuration
* - `%eclean_last_drop%` - 上次清理的掉落物数量
* - `%eclean_last_living%` - 上次清理的生物数量
* - `%eclean_last_chunk%` - 上次清理的密集实体数量
* - `%eclean_trashcan_countdown%` - 垃圾桶清理倒计时, 单位秒
* - `%eclean_trashcan_countdown_formatted%` - 垃圾桶清理倒计时, 格式化的时间
*/
object Papi : PapiExpansion(PL, "eclean") {
override fun onPlaceholderRequest(player: Player?, params: String) = onRequest(player, params)
Expand All @@ -30,6 +29,8 @@ object Papi : PapiExpansion(PL, "eclean") {
"last_drop" -> lastDrop.toString()
"last_living" -> lastLiving.toString()
"last_chunk" -> lastChunk.toString()
"trashcan_countdown" -> Trashcan.countdown.toString()
"trashcan_countdown_formatted" -> Trashcan.countdown.parseSecondAsDuration()
else -> null
}
}
Expand All @@ -40,6 +41,8 @@ object Papi : PapiExpansion(PL, "eclean") {
"%eclean_last_drop%",
"%eclean_last_living%",
"%eclean_last_chunk%",
"%eclean_trashcan_countdown%",
"%eclean_trashcan_countdown_formatted%",
)

override fun getPlaceholders() = placeholders
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,5 @@ trashcan:
enable: true
# 设置为true则掉落物清理的掉落物会收集到垃圾桶中
collect: true
# 清空垃圾桶的时间间隔, 单位刻, 设置为空则不会主动清理(可能导致内存泄露占用大量内存)
# 清空垃圾桶的时间间隔, 单位秒, 设置为空则不会主动清理(可能导致内存泄露占用大量内存)
duration: 6000
1 change: 1 addition & 0 deletions src/main/resources/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ trash:

command:
reload_done: "&a重载完成"
trash_disable: "&a垃圾桶功能已被禁用"
trash_open: "&a已打开垃圾桶"
clean_done: "&a共清理&6{count}&a个实体"
teleport:
Expand Down

0 comments on commit c96a031

Please sign in to comment.