Skip to content

Commit

Permalink
V1.0.5 修复玩家退出区域清除Tick任务
Browse files Browse the repository at this point in the history
  • Loading branch information
YsGqHY committed Feb 19, 2024
1 parent cc1ac84 commit fc66438
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ taboolib {
install("common-5")
install("module-chat")
install("module-configuration")
install("module-navigation")
install("module-kether")
install("module-effect")
install("module-lang")
Expand Down
27 changes: 14 additions & 13 deletions src/main/kotlin/kim/hhhhhy/regions/data/AreaSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import kim.hhhhhy.regions.listeners.AreaListener
import kim.hhhhhy.regions.utils.evalKether
import org.bukkit.Location
import org.bukkit.entity.Player
import org.bukkit.util.BoundingBox
import taboolib.module.navigation.BoundingBox
import taboolib.common.platform.function.console
import taboolib.common.platform.function.info
import taboolib.common.platform.function.submit
import taboolib.common.platform.service.PlatformExecutor
import taboolib.common5.mirrorNow
Expand All @@ -36,7 +37,7 @@ data class AreaSettings(
/**
* player.name to id -> task
*/
private val playerAreas = ConcurrentHashMap<Pair<String, String>, PlatformExecutor.PlatformTask>()
private val playerAreas = ConcurrentHashMap<String, MutableMap<String, PlatformExecutor.PlatformTask>>()

fun reloadArea() {
playerAreas.clear()
Expand Down Expand Up @@ -99,26 +100,26 @@ data class AreaSettings(

private fun startTick(player: Player, id: String) {
val period = areasData[id]!!.tickPeriod
playerAreas[player.name to id] = submit(period = period) {
if (!playerAreas.contains(player.name to id)) {
cancel()
val tasks = playerAreas.computeIfAbsent(player.name) { ConcurrentHashMap() }
tasks[id] = submit(period = period) {
if (playerAreas[player.name]?.contains(id) == true) {
runTickAction(player, id)
} else {
return@submit
}
runTickAction(player, id)
}
}

fun stopTick(player: Player, id: String? = null) {
if (id.isNullOrBlank()) {
playerAreas.filterKeys { it.first == player.name }
.forEach { (_, task) ->
task.cancel()
}
playerAreas.keys.removeAll { it.first == player.name }
playerAreas[player.name]?.forEach { (_, task) ->
task.cancel()
}
playerAreas.remove(player.name)
return
}
playerAreas[player.name to id]?.cancel()
playerAreas.remove(player.name to id)
playerAreas[player.name]?.get(id)?.cancel()
playerAreas[player.name]?.remove(id)
}

/**
Expand Down

0 comments on commit fc66438

Please sign in to comment.