BlastingCrisis/src/main/kotlin/cc/maxmc/blastingcrisis/listener/GameListener.kt

119 lines
4.3 KiB
Kotlin

package cc.maxmc.blastingcrisis.listener
import cc.maxmc.blastingcrisis.game.GamePlaceBreakRule
import cc.maxmc.blastingcrisis.game.team
import cc.maxmc.blastingcrisis.misc.Area
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.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
object GameListener {
val interactSubscribed = HashMap<Location, (PlayerInteractEvent) -> Unit>()
val moveSubscribed = ArrayList<Pair<Area, (PlayerMoveEvent) -> Unit>>()
@SubscribeEvent
fun portalTeleport(event: PlayerMoveEvent) {
if (!GameManager.currentGame.players.contains(event.player)) return
moveSubscribed.forEach { (area, func) ->
if (area.contains(event.to)) func(event)
}
}
@SubscribeEvent
fun onInteract(interactEvent: PlayerInteractEvent) {
val block = interactEvent.clickedBlock ?: return
interactSubscribed[block.location]?.invoke(interactEvent)
}
@SubscribeEvent
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.entity.location)
}?.apply {
villager.damage()
debug("team ${teamInfo.name}'s villager damaged")
} ?: 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
breakEvent.isCancelled = true
player.sendLang("game_cant_break_block")
}
@SubscribeEvent
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
placeEvent.isCancelled = true
player.sendLang("game_cant_place_block")
}
@SubscribeEvent
fun onRespawn(respawnEvent: PlayerRespawnEvent) {
val team = respawnEvent.player.team ?: return // ignore none game player
respawnEvent.respawnLocation = team.teamInfo.spawn.toPlayerLocation()
}
@SubscribeEvent
fun protectDamageBeforeGame(damageEvent: EntityDamageEvent) {
if (damageEvent.entity !is Player) return // ignore none player entity
if (GameManager.currentGame.state.isStarted()) return
damageEvent.isCancelled = true
}
@SubscribeEvent
fun protectHunger(hungerEvent: FoodLevelChangeEvent) {
hungerEvent.isCancelled = true
(hungerEvent.entity as Player).run {
foodLevel = 20
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)
}
}