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 { map.wall.forBlocksInArea().forEach {
it.block.type = Material.AIR it.block.type = Material.AIR
} }
placeBreakRule.addRule { _, _, loc -> placeBreakRule.addRule("allow_center_wall") { _, _, loc ->
map.wall.contains(loc) map.wall.contains(loc)
} }
} }

View File

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