Skip to content

Commit

Permalink
V1.0.2 新增每个区域独立触发间隔
Browse files Browse the repository at this point in the history
  • Loading branch information
YsGqHY committed Jan 26, 2024
1 parent ad46f84 commit c0b7882
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 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.1
version=1.0.2
33 changes: 25 additions & 8 deletions src/main/kotlin/kim/hhhhhy/regions/data/AreaSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import kim.hhhhhy.regions.utils.evalKether
import org.bukkit.Location
import org.bukkit.entity.Player
import taboolib.common.platform.function.console
import taboolib.common.platform.function.submit
import taboolib.common.platform.service.PlatformExecutor
import taboolib.common5.mirrorNow
import taboolib.module.configuration.Config
import taboolib.module.configuration.Configuration
Expand All @@ -17,7 +19,8 @@ import java.util.concurrent.ConcurrentHashMap

data class AreaSettings(
val position: AreaPosition,
val actions: AreaActions
val actions: AreaActions,
val tickPeriod: Long
) {
companion object {
@Config("areas.yml", autoReload = true)
Expand All @@ -26,14 +29,21 @@ data class AreaSettings(

private val areasData = ConcurrentHashMap<String, AreaSettings>()

/**
* player.name to id -> task
*/
private val playerAreas = ConcurrentHashMap<Pair<String, String>, PlatformExecutor.PlatformTask>()

fun reloadArea() {
playerAreas.clear()
areasData.clear()
val section = areasConfig.getConfigurationSection("Areas") ?: return
section.getKeys(false).forEach { id ->
val root = section.getConfigurationSection(id)!!
val position = AreaPosition(root.getConfigurationSection("position") ?: error("缺少坐标设置"))
val actions = AreaActions(root.getConfigurationSection("actions"))
areasData[id] = AreaSettings(position, actions)
val tickPeriod = root.getLong("tickPeriod", ConfigSettings.config.getLong("AreaSettings.TickAction", 20))
areasData[id] = AreaSettings(position, actions, tickPeriod)
}
console().sendInfo("plugin-areas-reload")
}
Expand All @@ -42,9 +52,6 @@ data class AreaSettings(
val x = location.x
val y = location.y
val z = location.z
/**
* xMax=-500.0, yMax=0.0, zMax=340.0, xMin=-510.0, yMin=110.0, zMin=350.0)
*/
return areasData.filter {
it.value.position.world == location.world?.name
&& (x in it.value.position.xMax .. it.value.position.xMin || x in it.value.position.xMin .. it.value.position.xMax)
Expand All @@ -61,6 +68,7 @@ data class AreaSettings(
return@mirrorNow
}
actions?.evalKether(player)
startTick(player, id)
}
}
private fun runLeaveAction(player: Player, id: String) {
Expand All @@ -70,18 +78,27 @@ data class AreaSettings(
return@mirrorNow
}
actions?.evalKether(player)
stopTick(player, id)
}
}
private fun runTickAction(player: Player, id: String) {
mirrorNow("RegionActionsLite:Actions:Tick") {
val actions = areasData[id]!!.actions.tick
if (ConfigSettings.baffleCache.hasNext("${player.name}-Tick-$id").not()) {
return@mirrorNow
}
actions?.evalKether(player)
}
}

private fun startTick(player: Player, id: String) {
val period = areasData[id]!!.tickPeriod
playerAreas[player.name to id] = submit(period = period) {
runTickAction(player, id)
}
}

private fun stopTick(player: Player, id: String) {
playerAreas[player.name to id]?.cancel()
}

/**
* 执行一次区域类型动作
*/
Expand Down
19 changes: 1 addition & 18 deletions src/main/kotlin/kim/hhhhhy/regions/data/ConfigSettings.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package kim.hhhhhy.regions.data

import kim.hhhhhy.regions.data.area.AreaType
import kim.hhhhhy.regions.listeners.AreaListener
import org.bukkit.Bukkit
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake

import taboolib.common.platform.function.*
import taboolib.common.platform.service.PlatformExecutor
import taboolib.common5.Baffle
import taboolib.module.configuration.Config
import taboolib.module.configuration.Configuration
Expand All @@ -29,19 +24,7 @@ object ConfigSettings {
cooldown = config.getLong("CommandBaffle.time", 3000)
actionTick = config.getLong("AreaSettings.TickAction", 20)
console().sendInfo("plugin-config-reload")
runTickAction().cancel()
runTickAction()
}

@Awake(LifeCycle.ACTIVE)
fun runTickAction(): PlatformExecutor.PlatformTask {
return submit(now = false, period = actionTick) {
AreaListener.playerSet.forEach { (player, id) ->
Bukkit.getPlayerExact(player)?.let { p ->
AreaSettings.runActions(p, id, AreaType.TICK)
}
}
}
}

}
28 changes: 19 additions & 9 deletions src/main/kotlin/kim/hhhhhy/regions/listeners/AreaListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,37 @@ 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.PlayerJoinEvent
import org.bukkit.event.player.PlayerMoveEvent
import org.bukkit.event.player.PlayerQuitEvent
import taboolib.common.platform.event.SubscribeEvent
import taboolib.common.platform.function.info

object AreaListener {
val playerSet = mutableSetOf<Pair<String, String>>()
private 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
val areas = AreaSettings.getAreas(to)
check(player, to)
}

@SubscribeEvent
fun onPlayerJoin(e: PlayerJoinEvent) {
check(e.player, e.player.location)
}
@SubscribeEvent
fun onPlayerQuit(e: PlayerQuitEvent) {
playerSet.removeAll { it.first == e.player.name }
}


private fun check(player: Player, location: Location) {
val areas = AreaSettings.getAreas(location)

if (areas.isNotEmpty()) {

Expand Down Expand Up @@ -49,12 +65,6 @@ object AreaListener {
}
}
}

}

@SubscribeEvent
fun onPlayerQuit(e: PlayerQuitEvent) {
playerSet.removeAll { it.first == e.player.name }
}

}
1 change: 1 addition & 0 deletions src/main/resources/areas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Areas:
world: "world"
max: "0, 0, 0"
min: "10,10,10"
tickPeriod: 20
actions:
enter: |-
tell "进入区域"
Expand Down

0 comments on commit c0b7882

Please sign in to comment.