From f6e34d8efce39358e5ec6edfb7f884bc30a495b9 Mon Sep 17 00:00:00 2001 From: TONY_All Date: Fri, 16 Jun 2023 02:07:14 +0800 Subject: [PATCH] fix placing rule --- .../blastingcrisis/game/GamePlaceBreakRule.kt | 30 +++++++++++-------- .../blastingcrisis/listener/GameListener.kt | 5 ++-- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/cc/maxmc/blastingcrisis/game/GamePlaceBreakRule.kt b/src/main/kotlin/cc/maxmc/blastingcrisis/game/GamePlaceBreakRule.kt index c2faf4e..8e67f6c 100644 --- a/src/main/kotlin/cc/maxmc/blastingcrisis/game/GamePlaceBreakRule.kt +++ b/src/main/kotlin/cc/maxmc/blastingcrisis/game/GamePlaceBreakRule.kt @@ -1,10 +1,10 @@ package cc.maxmc.blastingcrisis.game import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule.ActionType.BREAK -import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule.ActionType.PLACE import cc.maxmc.blastingcrisis.misc.debug import org.bukkit.Location import org.bukkit.entity.Player +import taboolib.library.xseries.XMaterial class GamePlaceBreakRule(val game: Game) { enum class ActionType { @@ -12,11 +12,11 @@ class GamePlaceBreakRule(val game: Game) { BREAK } - private val rules = HashMap Boolean>() - fun matchRule(player: Player, actionType: ActionType, location: Location): Boolean { + private val rules = HashMap Boolean>() + fun matchRule(player: Player, actionType: ActionType, location: Location, type: XMaterial): Boolean { // match rules rules.forEach { (name, func) -> - if (func(player, actionType, location)) { + if (func(player, actionType, location, type)) { debug("Hit rule $name") return true } @@ -26,7 +26,10 @@ class GamePlaceBreakRule(val game: Game) { return false } - fun addRule(name: String, rule: (player: Player, type: ActionType, loc: Location) -> Boolean) { + fun addRule( + name: String, + rule: (player: Player, actionType: ActionType, loc: Location, type: XMaterial) -> Boolean + ) { if (rules.containsKey(name)) { throw IllegalArgumentException("Rule $name already exists.") } @@ -36,7 +39,7 @@ class GamePlaceBreakRule(val game: Game) { fun loadDefaultRule() { // allow mine - addRule("allow_mine") { _, _, loc -> + addRule("allow_mine") { _, _, loc, _ -> game.map.teams.flatMap { it.sides + it.mine }.map { @@ -45,24 +48,27 @@ class GamePlaceBreakRule(val game: Game) { } // allow enemy wall - addRule("allow_wall_conditional") { player, type, loc -> + addRule("allow_wall_conditional") { player, actionType, loc, _ -> val team = player.team ?: return@addRule false val isWall = game.teams .map { - it.teamInfo.wall.containsBlock(loc).also { result -> debug("area ${it.teamInfo.wall} contains $loc is $result") } + it.teamInfo.wall.containsBlock(loc) + .also { result -> debug("area ${it.teamInfo.wall} contains $loc is $result") } } .reduce { a, b -> a || b } if (!isWall) return@addRule false val isHome = team.teamInfo.wall.containsBlock(loc) - (type == BREAK) xor isHome + (actionType == BREAK) xor isHome } - addRule("allow_tnt_conditional") { player, type, loc -> - if (type != PLACE) return@addRule true + addRule("allow_tnt_conditional") { player, actionType, loc, type -> + if (actionType == BREAK) return@addRule false + if (type != XMaterial.TNT) return@addRule false val team = player.team ?: return@addRule false val isHome = game.teams .map { - it.teamInfo.home.containsBlock(loc).also { result -> debug("area ${it.teamInfo.home} contains $loc is $result") } + it.teamInfo.home.containsBlock(loc) + .also { result -> debug("area ${it.teamInfo.home} contains $loc is $result") } } .reduce { a, b -> a || b } if (!isHome) return@addRule false diff --git a/src/main/kotlin/cc/maxmc/blastingcrisis/listener/GameListener.kt b/src/main/kotlin/cc/maxmc/blastingcrisis/listener/GameListener.kt index 8178cfd..cf1a86d 100644 --- a/src/main/kotlin/cc/maxmc/blastingcrisis/listener/GameListener.kt +++ b/src/main/kotlin/cc/maxmc/blastingcrisis/listener/GameListener.kt @@ -17,6 +17,7 @@ import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.event.player.PlayerMoveEvent import org.bukkit.event.player.PlayerRespawnEvent import taboolib.common.platform.event.SubscribeEvent +import taboolib.library.xseries.XMaterial import taboolib.platform.util.sendLang object GameListener { @@ -54,7 +55,7 @@ object GameListener { fun onBreak(breakEvent: BlockBreakEvent) { val loc = breakEvent.block.location ?: return val player = breakEvent.player ?: return - if (GameManager.currentGame.placeBreakRule.matchRule(player, GamePlaceBreakRule.ActionType.BREAK, loc)) return + if (GameManager.currentGame.placeBreakRule.matchRule(player, GamePlaceBreakRule.ActionType.BREAK, loc, XMaterial.matchXMaterial(breakEvent.block.type))) return breakEvent.isCancelled = true player.sendLang("game_cant_break_block") } @@ -63,7 +64,7 @@ object GameListener { fun onPlace(placeEvent: BlockPlaceEvent) { val loc = placeEvent.block.location ?: return val player = placeEvent.player ?: return - if (GameManager.currentGame.placeBreakRule.matchRule(player, GamePlaceBreakRule.ActionType.PLACE, loc)) return + if (GameManager.currentGame.placeBreakRule.matchRule(player, GamePlaceBreakRule.ActionType.PLACE, loc, XMaterial.matchXMaterial(placeEvent.blockPlaced.type))) return placeEvent.isCancelled = true player.sendLang("game_cant_place_block") }