optimize rule structure

This commit is contained in:
tony_all 2023-06-07 15:54:37 +08:00
parent ed6f40b462
commit 84baa309e3
2 changed files with 17 additions and 9 deletions

View File

@ -58,7 +58,7 @@ class Game(
map.wall.forBlocksInArea().forEach {
it.block.type = Material.AIR
}
placeBreakRule.addRule { _, _, loc ->
placeBreakRule.addRule("allow_center_wall") { _, _, loc ->
map.wall.contains(loc)
}
}

View File

@ -1,6 +1,7 @@
package cc.maxmc.blastingcrisis.game
import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule.ActionType.BREAK
import cc.maxmc.blastingcrisis.misc.debug
import org.bukkit.Location
import org.bukkit.entity.Player
@ -10,24 +11,31 @@ class GamePlaceBreakRule(val game: Game) {
BREAK
}
private val rules = ArrayList<(Player, ActionType, Location) -> Boolean>()
private val rules = HashMap<String, (Player, ActionType, Location) -> Boolean>()
fun matchRule(player: Player, actionType: ActionType, location: Location): Boolean {
// match rules
rules.forEach {
if (it(player, actionType, location)) return true
rules.forEach { (name, func) ->
if (func(player, actionType, location)) {
debug("Hit rule $name")
return true
}
}
// fallback strategy
return false
}
fun addRule(rule: (player: Player, type: ActionType, loc: Location) -> Boolean) {
rules.add(rule)
fun addRule(name: String, rule: (player: Player, type: ActionType, loc: Location) -> Boolean) {
if (rules.containsKey(name)) {
throw IllegalArgumentException("Rule $name already exists.")
}
debug("rule $name added")
rules[name] = rule
}
fun defaultRule() {
// allow mine
rules.add { _, _, loc ->
addRule("allow_mine") { _, _, loc ->
game.map.teams.flatMap {
it.sides + it.mine
}.map {
@ -36,8 +44,8 @@ class GamePlaceBreakRule(val game: Game) {
}
// allow enemy wall
rules.add { player, type, loc ->
val team = player.team ?: return@add false
addRule("allow_enemy_wall") { player, type, loc ->
val team = player.team ?: return@addRule false
val result = game.teams.filterNot { it == team }
.map { team.teamInfo.wall.containsBlock(loc) }
.reduce { a, b -> a || b }