rule change start

This commit is contained in:
TONY_All 2023-06-21 00:44:44 +08:00
parent b462eaa518
commit 45629b0d1b
Signed by: tony_all
GPG Key ID: 08A2261D5D6F746A
2 changed files with 55 additions and 22 deletions

View File

@ -1,6 +1,7 @@
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
@ -12,6 +13,11 @@ class GamePlaceBreakRule(val game: Game) {
BREAK
}
enum class PlacePosition {
FIRST,
LAST
}
private val rules = HashMap<String, (Player, ActionType, Location, XMaterial) -> Boolean>()
fun matchRule(player: Player, actionType: ActionType, location: Location, type: XMaterial): Boolean {
// match rules
@ -38,6 +44,21 @@ class GamePlaceBreakRule(val game: Game) {
}
fun loadDefaultRule() {
// allow tnt in enemy's home
addRule("allow_tnt_conditional") { player, actionType, loc, type ->
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") }
}
.reduce { a, b -> a || b }
if (!isHome) return@addRule false
val isSelf = team.teamInfo.home.containsBlock(loc)
(actionType == PLACE) xor isSelf
}
// allow mine
addRule("allow_mine") { _, _, loc, _ ->
game.map.teams.flatMap {
@ -60,19 +81,5 @@ class GamePlaceBreakRule(val game: Game) {
val isHome = team.teamInfo.wall.containsBlock(loc)
(actionType == BREAK) xor isHome
}
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") }
}
.reduce { a, b -> a || b }
if (!isHome) return@addRule false
return@addRule !team.teamInfo.home.containsBlock(loc)
}
}
}

View File

@ -7,15 +7,18 @@ import cc.maxmc.blastingcrisis.misc.GameManager
import cc.maxmc.blastingcrisis.misc.debug
import cc.maxmc.blastingcrisis.misc.toPlayerLocation
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.entity.EntityType
import org.bukkit.entity.Player
import org.bukkit.event.block.BlockBreakEvent
import org.bukkit.event.block.BlockPlaceEvent
import org.bukkit.event.entity.EntityDamageEvent
import org.bukkit.event.entity.EntityExplodeEvent
import org.bukkit.event.entity.ExplosionPrimeEvent
import org.bukkit.event.entity.FoodLevelChangeEvent
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.event.player.PlayerMoveEvent
import org.bukkit.event.player.PlayerRespawnEvent
import taboolib.common.platform.event.EventPriority
import taboolib.common.platform.event.SubscribeEvent
import taboolib.library.xseries.XMaterial
import taboolib.platform.util.sendLang
@ -39,23 +42,32 @@ object GameListener {
}
@SubscribeEvent
fun onTNT(tntExplode: EntityExplodeEvent) {
debug("${tntExplode.entityType} exploded at ${tntExplode.location}")
tntExplode.blockList().clear()
fun onTNT(tntExplode: ExplosionPrimeEvent) {
debug("${tntExplode.entityType} exploded at ${tntExplode.entity.location}")
tntExplode.radius = 0.0f
if (!GameManager.currentGame.state.isStarted()) return
GameManager.currentGame.teams.findLast {
it.teamSurvive && it.teamInfo.home.contains(tntExplode.location)
it.teamSurvive && it.teamInfo.home.contains(tntExplode.entity.location)
}?.apply {
villager.damage()
debug("team ${teamInfo.name}'s villager damaged")
} ?: throw IllegalStateException("TNT Exploded at no team, which shouldn't happen")
} ?: return run {
debug("§cTNT Exploded at no team, which shouldn't happen")
tntExplode.isCancelled = true
}
}
@SubscribeEvent
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, XMaterial.matchXMaterial(breakEvent.block.type))) 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")
}
@ -64,7 +76,13 @@ 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, XMaterial.matchXMaterial(placeEvent.blockPlaced.type))) 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")
}
@ -90,4 +108,12 @@ object GameListener {
saturation = 20f
}
}
@SubscribeEvent(EventPriority.HIGH, true)
fun tntIgnite(tntPlace: BlockPlaceEvent) {
if (tntPlace.blockPlaced.type != Material.TNT) return
val loc = tntPlace.blockPlaced.location
tntPlace.block.type = Material.AIR
loc.world.spawnEntity(loc.clone().apply { y += 0.5 }, EntityType.PRIMED_TNT)
}
}