From 7a39c99c806f42fd8ac04166636c19e72c6bcea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=98=BF=E9=B9=B0?= Date: Sat, 17 Feb 2024 23:04:41 +0800 Subject: [PATCH] =?UTF-8?q?V1.0.5=20=E4=BF=AE=E5=A4=8D=E7=8E=A9=E5=AE=B6?= =?UTF-8?q?=E9=80=80=E5=87=BA=E5=8C=BA=E5=9F=9F=E6=B8=85=E9=99=A4Tick?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../kim/hhhhhy/regions/data/AreaSettings.kt | 56 +++++++++++++++++-- .../hhhhhy/regions/listeners/AreaListener.kt | 53 +++--------------- 3 files changed, 62 insertions(+), 49 deletions(-) diff --git a/gradle.properties b/gradle.properties index ce764f9..847003a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ group=kim.hhhhhy.regions -version=1.0.4 \ No newline at end of file +version=1.0.5 \ No newline at end of file diff --git a/src/main/kotlin/kim/hhhhhy/regions/data/AreaSettings.kt b/src/main/kotlin/kim/hhhhhy/regions/data/AreaSettings.kt index e7f5d56..3d156ef 100644 --- a/src/main/kotlin/kim/hhhhhy/regions/data/AreaSettings.kt +++ b/src/main/kotlin/kim/hhhhhy/regions/data/AreaSettings.kt @@ -4,6 +4,7 @@ import kim.hhhhhy.regions.data.area.AreaActions import kim.hhhhhy.regions.data.area.AreaPosition import kim.hhhhhy.regions.data.area.AreaType import kim.hhhhhy.regions.data.area.AreaType.* +import kim.hhhhhy.regions.listeners.AreaListener import kim.hhhhhy.regions.utils.evalKether import org.bukkit.Location import org.bukkit.entity.Player @@ -67,7 +68,7 @@ data class AreaSettings( private fun runEnterAction(player: Player, id: String) { mirrorNow("RegionActionsLite:Actions:Enter") { - val actions = areasData[id]!!.actions.enter + val actions = areasData[id]?.actions?.enter if (ConfigSettings.baffleCache.hasNext("${player.name}-Enter-$id").not()) { return@mirrorNow } @@ -77,7 +78,7 @@ data class AreaSettings( } private fun runLeaveAction(player: Player, id: String) { mirrorNow("RegionActionsLite:Actions:Leave") { - val actions = areasData[id]!!.actions.leave + val actions = areasData[id]?.actions?.leave if (ConfigSettings.baffleCache.hasNext("${player.name}-Leave-$id").not()) { return@mirrorNow } @@ -87,7 +88,7 @@ data class AreaSettings( } private fun runTickAction(player: Player, id: String) { mirrorNow("RegionActionsLite:Actions:Tick") { - val actions = areasData[id]!!.actions.tick + val actions = areasData[id]?.actions?.tick actions?.evalKether(player) } } @@ -95,16 +96,27 @@ data class AreaSettings( 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() + return@submit + } runTickAction(player, id) } } fun stopTick(player: Player, id: String? = null) { if (id.isNullOrBlank()) { - playerAreas.filter { it.key.first == player.name }.forEach { it.value.cancel() } + playerAreas.forEach { (k, task) -> + if (k.first != player.name) { + return@forEach + } + task.cancel() + playerAreas.remove(k) + } return } playerAreas[player.name to id]?.cancel() + playerAreas.remove(player.name to id) } /** @@ -128,5 +140,41 @@ data class AreaSettings( } } } + + fun check(player: Player, location: Location) { + val areas = getAreas(location) + + if (areas.isNotEmpty()) { + + // 退出某个区域 + val filter = AreaListener.playerSet.filter { it.first == player.name && it !in areas.map { id -> player.name to id }.toSet() } + if (filter.isNotEmpty()) { + filter.forEach { (name, id) -> + if (AreaListener.playerSet.remove(name to id)) { + runActions(player, id, LEAVE) + } + } + } + + // 进入区域 + areas.forEach { + if (AreaListener.playerSet.add(player.name to it)) { + runActions(player, it, ENTER) + } + } + } else { + + // 退出所有区域 + val filter = AreaListener.playerSet.filter { it.first == player.name } + + if (filter.isNotEmpty()) { + filter.forEach { (name, id) -> + if (AreaListener.playerSet.remove(name to id)) { + runActions(player, id, LEAVE) + } + } + } + } + } } } diff --git a/src/main/kotlin/kim/hhhhhy/regions/listeners/AreaListener.kt b/src/main/kotlin/kim/hhhhhy/regions/listeners/AreaListener.kt index b39daf8..4b0189c 100644 --- a/src/main/kotlin/kim/hhhhhy/regions/listeners/AreaListener.kt +++ b/src/main/kotlin/kim/hhhhhy/regions/listeners/AreaListener.kt @@ -1,9 +1,6 @@ package kim.hhhhhy.regions.listeners import kim.hhhhhy.regions.data.AreaSettings -import kim.hhhhhy.regions.data.area.AreaType -import org.bukkit.Location -import org.bukkit.entity.Player import org.bukkit.event.player.PlayerChangedWorldEvent import org.bukkit.event.player.PlayerJoinEvent import org.bukkit.event.player.PlayerMoveEvent @@ -12,7 +9,7 @@ import org.bukkit.event.player.PlayerTeleportEvent import taboolib.common.platform.event.SubscribeEvent object AreaListener { - private val playerSet = mutableSetOf>() + val playerSet = mutableSetOf>() @SubscribeEvent fun onPlayerMove(e: PlayerMoveEvent) { @@ -20,7 +17,12 @@ object AreaListener { val to = e.to ?: return if (from.x == to.x && from.y == to.y && from.z == to.z) return val player = e.player - check(player, to) + AreaSettings.check(player, to) + } + + @SubscribeEvent + fun onPlayerChangeWorld(e: PlayerChangedWorldEvent) { + AreaSettings.check(e.player, e.player.location) } @SubscribeEvent @@ -29,12 +31,12 @@ object AreaListener { val to = e.to ?: return if (from.world?.name == to.world?.name && from.x == to.x && from.y == to.y && from.z == to.z) return val player = e.player - check(player, to) + AreaSettings.check(player, to) } @SubscribeEvent fun onPlayerJoin(e: PlayerJoinEvent) { - check(e.player, e.player.location) + AreaSettings.check(e.player, e.player.location) } @SubscribeEvent fun onPlayerQuit(e: PlayerQuitEvent) { @@ -42,41 +44,4 @@ object AreaListener { AreaSettings.stopTick(e.player) } - - private fun check(player: Player, location: Location) { - val areas = AreaSettings.getAreas(location) - - if (areas.isNotEmpty()) { - - // 退出某个区域 - val filter = playerSet.filter { it.first == player.name && it !in areas.map { id -> player.name to id }.toSet() } - if (filter.isNotEmpty()) { - filter.forEach { (name, id) -> - if (playerSet.remove(name to id)) { - AreaSettings.runActions(player, id, AreaType.LEAVE) - } - } - } - - // 进入区域 - areas.forEach { - if (playerSet.add(player.name to it)) { - AreaSettings.runActions(player, it, AreaType.ENTER) - } - } - } else { - - // 退出所有区域 - val filter = playerSet.filter { it.first == player.name } - - if (filter.isNotEmpty()) { - filter.forEach { (name, id) -> - if (playerSet.remove(name to id)) { - AreaSettings.runActions(player, id, AreaType.LEAVE) - } - } - } - } - } - } \ No newline at end of file