From 84baa309e39cdd54a707883407163af6bb4934d4 Mon Sep 17 00:00:00 2001 From: tony_all Date: Wed, 7 Jun 2023 15:54:37 +0800 Subject: [PATCH] optimize rule structure --- .../cc/maxmc/blastingcrisis/game/Game.kt | 2 +- .../blastingcrisis/game/GamePlaceBreakRule.kt | 24 ++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/cc/maxmc/blastingcrisis/game/Game.kt b/src/main/kotlin/cc/maxmc/blastingcrisis/game/Game.kt index a27d44b..a75816b 100644 --- a/src/main/kotlin/cc/maxmc/blastingcrisis/game/Game.kt +++ b/src/main/kotlin/cc/maxmc/blastingcrisis/game/Game.kt @@ -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) } } diff --git a/src/main/kotlin/cc/maxmc/blastingcrisis/game/GamePlaceBreakRule.kt b/src/main/kotlin/cc/maxmc/blastingcrisis/game/GamePlaceBreakRule.kt index 9fe6f4f..3fced52 100644 --- a/src/main/kotlin/cc/maxmc/blastingcrisis/game/GamePlaceBreakRule.kt +++ b/src/main/kotlin/cc/maxmc/blastingcrisis/game/GamePlaceBreakRule.kt @@ -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 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 }