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 17, 2024
1 parent 2d0ece3 commit 7a39c99
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 49 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group=kim.hhhhhy.regions
version=1.0.4
version=1.0.5
56 changes: 52 additions & 4 deletions src/main/kotlin/kim/hhhhhy/regions/data/AreaSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -87,24 +88,35 @@ 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)
}
}

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)
}

/**
Expand All @@ -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)
}
}
}
}
}
}
}
53 changes: 9 additions & 44 deletions src/main/kotlin/kim/hhhhhy/regions/listeners/AreaListener.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,15 +9,20 @@ import org.bukkit.event.player.PlayerTeleportEvent
import taboolib.common.platform.event.SubscribeEvent

object AreaListener {
private val playerSet = mutableSetOf<Pair<String, String>>()
val playerSet = mutableSetOf<Pair<String, String>>()

@SubscribeEvent
fun onPlayerMove(e: PlayerMoveEvent) {
val from = e.from
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
Expand All @@ -29,54 +31,17 @@ 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) {
playerSet.removeAll { it.first == e.player.name }
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)
}
}
}
}
}

}

0 comments on commit 7a39c99

Please sign in to comment.