fix placing rule

This commit is contained in:
TONY_All 2023-06-16 02:07:14 +08:00
parent 6f1a727691
commit f6e34d8efc
Signed by: tony_all
GPG Key ID: 08A2261D5D6F746A
2 changed files with 21 additions and 14 deletions

View File

@ -1,10 +1,10 @@
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.game.GamePlaceBreakRule.ActionType.PLACE
import cc.maxmc.blastingcrisis.misc.debug import cc.maxmc.blastingcrisis.misc.debug
import org.bukkit.Location import org.bukkit.Location
import org.bukkit.entity.Player import org.bukkit.entity.Player
import taboolib.library.xseries.XMaterial
class GamePlaceBreakRule(val game: Game) { class GamePlaceBreakRule(val game: Game) {
enum class ActionType { enum class ActionType {
@ -12,11 +12,11 @@ class GamePlaceBreakRule(val game: Game) {
BREAK BREAK
} }
private val rules = HashMap<String, (Player, ActionType, Location) -> Boolean>() private val rules = HashMap<String, (Player, ActionType, Location, XMaterial) -> Boolean>()
fun matchRule(player: Player, actionType: ActionType, location: Location): Boolean { fun matchRule(player: Player, actionType: ActionType, location: Location, type: XMaterial): Boolean {
// match rules // match rules
rules.forEach { (name, func) -> rules.forEach { (name, func) ->
if (func(player, actionType, location)) { if (func(player, actionType, location, type)) {
debug("Hit rule $name") debug("Hit rule $name")
return true return true
} }
@ -26,7 +26,10 @@ class GamePlaceBreakRule(val game: Game) {
return false 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)) { if (rules.containsKey(name)) {
throw IllegalArgumentException("Rule $name already exists.") throw IllegalArgumentException("Rule $name already exists.")
} }
@ -36,7 +39,7 @@ class GamePlaceBreakRule(val game: Game) {
fun loadDefaultRule() { fun loadDefaultRule() {
// allow mine // allow mine
addRule("allow_mine") { _, _, loc -> addRule("allow_mine") { _, _, loc, _ ->
game.map.teams.flatMap { game.map.teams.flatMap {
it.sides + it.mine it.sides + it.mine
}.map { }.map {
@ -45,24 +48,27 @@ class GamePlaceBreakRule(val game: Game) {
} }
// allow enemy wall // 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 team = player.team ?: return@addRule false
val isWall = game.teams val isWall = game.teams
.map { .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 } .reduce { a, b -> a || b }
if (!isWall) return@addRule false if (!isWall) return@addRule false
val isHome = team.teamInfo.wall.containsBlock(loc) val isHome = team.teamInfo.wall.containsBlock(loc)
(type == BREAK) xor isHome (actionType == BREAK) xor isHome
} }
addRule("allow_tnt_conditional") { player, type, loc -> addRule("allow_tnt_conditional") { player, actionType, loc, type ->
if (type != PLACE) return@addRule true if (actionType == BREAK) return@addRule false
if (type != XMaterial.TNT) return@addRule false
val team = player.team ?: return@addRule false val team = player.team ?: return@addRule false
val isHome = game.teams val isHome = game.teams
.map { .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 } .reduce { a, b -> a || b }
if (!isHome) return@addRule false if (!isHome) return@addRule false

View File

@ -17,6 +17,7 @@ import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.event.player.PlayerMoveEvent import org.bukkit.event.player.PlayerMoveEvent
import org.bukkit.event.player.PlayerRespawnEvent import org.bukkit.event.player.PlayerRespawnEvent
import taboolib.common.platform.event.SubscribeEvent import taboolib.common.platform.event.SubscribeEvent
import taboolib.library.xseries.XMaterial
import taboolib.platform.util.sendLang import taboolib.platform.util.sendLang
object GameListener { object GameListener {
@ -54,7 +55,7 @@ object GameListener {
fun onBreak(breakEvent: BlockBreakEvent) { fun onBreak(breakEvent: BlockBreakEvent) {
val loc = breakEvent.block.location ?: return val loc = breakEvent.block.location ?: return
val player = breakEvent.player ?: 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 breakEvent.isCancelled = true
player.sendLang("game_cant_break_block") player.sendLang("game_cant_break_block")
} }
@ -63,7 +64,7 @@ object GameListener {
fun onPlace(placeEvent: BlockPlaceEvent) { fun onPlace(placeEvent: BlockPlaceEvent) {
val loc = placeEvent.block.location ?: return val loc = placeEvent.block.location ?: return
val player = placeEvent.player ?: 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 placeEvent.isCancelled = true
player.sendLang("game_cant_place_block") player.sendLang("game_cant_place_block")
} }