From 66d091c7fbaba817a67cb56b344b90763f041057 Mon Sep 17 00:00:00 2001 From: Moritz Ruth Date: Fri, 19 Jun 2020 17:37:24 +0200 Subject: [PATCH] Fix the case when a player dies while another player has a Second Chance --- .../de/moritzruth/spigot_ttt/game/GameManager.kt | 2 +- .../moritzruth/spigot_ttt/game/items/TTTItem.kt | 11 ++++++++--- .../game/items/impl/MartyrdomGrenade.kt | 12 ++++++++---- .../spigot_ttt/game/items/impl/SecondChance.kt | 2 +- .../spigot_ttt/game/players/PlayerManager.kt | 16 ++++++++++------ .../spigot_ttt/game/players/TTTPlayer.kt | 7 ++++--- 6 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/de/moritzruth/spigot_ttt/game/GameManager.kt b/src/main/kotlin/de/moritzruth/spigot_ttt/game/GameManager.kt index dc4ec67..263c0dd 100644 --- a/src/main/kotlin/de/moritzruth/spigot_ttt/game/GameManager.kt +++ b/src/main/kotlin/de/moritzruth/spigot_ttt/game/GameManager.kt @@ -165,7 +165,7 @@ object GameManager { GameMessenger.combatPhaseStarted() Timers.startCombatPhaseTimer { - if (PlayerManager.stillLivingRoles.contains(Role.INNOCENT)) { + if (PlayerManager.getStillLivingRoles().contains(Role.INNOCENT)) { letRoleWin(Role.INNOCENT) } else { letRoleWin(null) diff --git a/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/TTTItem.kt b/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/TTTItem.kt index 2f38bca..4daf0de 100644 --- a/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/TTTItem.kt +++ b/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/TTTItem.kt @@ -38,9 +38,9 @@ open class TTTItem( open fun getInstance(tttPlayer: TTTPlayer) = instancesByUUID.values.find { it.carrier === tttPlayer } fun reset() { - instancesByUUID.values.forEach { + instancesByUUID.values.toList().forEach { it.carrier?.removeItem(it.tttItem, removeInstance = false) - it.reset() + it.remove() } instancesByUUID.clear() } @@ -80,6 +80,11 @@ open class TTTItem( persistentDataContainer.set(ID_KEY, PersistentDataType.STRING, uuid.toString()) } + fun remove() { + tttItem.instancesByUUID.remove(uuid) + reset() + } + private var isFirstCarrier = true open var carrier: TTTPlayer? = null set(newCarrier) { @@ -127,7 +132,7 @@ open class TTTItem( open fun onLeftClick(event: ClickEvent) { event.isCancelled = false } open fun onHandSwap() {} - open fun reset() {} + protected open fun reset() {} var isSelected = false set(value) { diff --git a/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/impl/MartyrdomGrenade.kt b/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/impl/MartyrdomGrenade.kt index 0c6692f..caf6c13 100644 --- a/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/impl/MartyrdomGrenade.kt +++ b/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/impl/MartyrdomGrenade.kt @@ -5,6 +5,7 @@ import de.moritzruth.spigot_ttt.game.GameManager import de.moritzruth.spigot_ttt.game.items.TTTItem import de.moritzruth.spigot_ttt.game.items.TTTItemListener import de.moritzruth.spigot_ttt.game.players.Role +import de.moritzruth.spigot_ttt.game.players.TTTPlayer import de.moritzruth.spigot_ttt.game.players.TTTPlayerTrueDeathEvent import de.moritzruth.spigot_ttt.game.players.roles import de.moritzruth.spigot_ttt.plugin @@ -33,10 +34,12 @@ object MartyrdomGrenade: TTTItem( shopInfo = ShopInfo( buyableBy = roles(Role.TRAITOR, Role.JACKAL), price = 1 - ) + ), + removeInstanceOnDeath = false ) { - class Instance: TTTItem.Instance(MartyrdomGrenade, true) { + class Instance: TTTItem.Instance(MartyrdomGrenade) { var explodeTask: BukkitTask? = null + var tttPlayer: TTTPlayer? = null override fun reset() { explodeTask?.cancel() @@ -48,7 +51,7 @@ object MartyrdomGrenade: TTTItem( @EventHandler fun onTTTPlayerTrueDeath(event: TTTPlayerTrueDeathEvent) { val instance = getInstance(event.tttPlayer) ?: return - event.tttPlayer.removeItem(MartyrdomGrenade, false) + instance.tttPlayer = event.tttPlayer instance.explodeTask = plugin.server.scheduler.runTaskLater(plugin, fun() { GameManager.world.playSound( @@ -59,7 +62,8 @@ object MartyrdomGrenade: TTTItem( 1F ) - createKillExplosion(event.tttPlayer, event.location, 2.5) + createKillExplosion(event.tttPlayer, event.location, 5.0) + instance.remove() }, secondsToTicks(3).toLong()) } } diff --git a/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/impl/SecondChance.kt b/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/impl/SecondChance.kt index bd188ba..3c943ec 100644 --- a/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/impl/SecondChance.kt +++ b/src/main/kotlin/de/moritzruth/spigot_ttt/game/items/impl/SecondChance.kt @@ -136,7 +136,7 @@ object SecondChance: TTTItem( fun onTTTPlayerTrueDeath(event: TTTPlayerTrueDeathEvent) { val instance = getInstance(event.tttPlayer) ?: return instance.possiblyTrigger() - if (instancesByUUID.values.find { it.preventRoundEnd } != null) event.winnerRoleGroup = null + event.winnerRoleGroup = PlayerManager.getOnlyRemainingRoleGroup() } @EventHandler diff --git a/src/main/kotlin/de/moritzruth/spigot_ttt/game/players/PlayerManager.kt b/src/main/kotlin/de/moritzruth/spigot_ttt/game/players/PlayerManager.kt index 6b6808f..abc89cb 100644 --- a/src/main/kotlin/de/moritzruth/spigot_ttt/game/players/PlayerManager.kt +++ b/src/main/kotlin/de/moritzruth/spigot_ttt/game/players/PlayerManager.kt @@ -7,6 +7,7 @@ import de.moritzruth.spigot_ttt.game.GameManager import de.moritzruth.spigot_ttt.game.GameMessenger import de.moritzruth.spigot_ttt.game.GamePhase import de.moritzruth.spigot_ttt.game.classes.TTTClassManager +import de.moritzruth.spigot_ttt.game.items.impl.SecondChance import de.moritzruth.spigot_ttt.plugin import de.moritzruth.spigot_ttt.utils.nextTick import de.moritzruth.spigot_ttt.utils.noop @@ -17,11 +18,14 @@ import org.bukkit.entity.Player import kotlin.random.Random object PlayerManager { - val tttPlayers= mutableListOf() + val tttPlayers = mutableListOf() + + private fun getAvailablePlayers() = plugin.server.onlinePlayers.filter { it.gameMode === GameMode.SURVIVAL } + private fun getStillLivingRoleGroups() = getStillLivingRoles().map { it.group }.toSet() + fun getStillLivingRoles() = tttPlayers.filter { + it.alive || SecondChance.getInstance(it)?.preventRoundEnd == true + }.map { it.role }.toSet() - val availablePlayers get() = plugin.server.onlinePlayers.filter { it.gameMode === GameMode.SURVIVAL } - val stillLivingRoles get() = tttPlayers.filter { it.alive }.map { it.role }.toSet() - private val stillLivingRoleGroups get() = stillLivingRoles.map { it.group }.toSet() private val playersJoinedDuringRound = mutableSetOf() fun getPlayersByRole() = mutableMapOf>() @@ -45,7 +49,7 @@ object PlayerManager { fun getOnlyRemainingRoleGroup(): RoleGroup? { GameManager.ensurePhase(GamePhase.COMBAT) - return if (stillLivingRoleGroups.count() == 1) stillLivingRoleGroups.first() + return if (getStillLivingRoleGroups().count() == 1) getStillLivingRoleGroups().first() else null } @@ -93,7 +97,7 @@ object PlayerManager { } fun createTTTPlayers() { - val playersWithoutRole = availablePlayers.toMutableSet() + val playersWithoutRole = getAvailablePlayers().toMutableSet() val playerCount = playersWithoutRole.count() if (Settings.traitorCount < 1) throw IllegalStateException("roles.traitor.count may not be lower than 1") diff --git a/src/main/kotlin/de/moritzruth/spigot_ttt/game/players/TTTPlayer.kt b/src/main/kotlin/de/moritzruth/spigot_ttt/game/players/TTTPlayer.kt index 960ed96..0f76c12 100644 --- a/src/main/kotlin/de/moritzruth/spigot_ttt/game/players/TTTPlayer.kt +++ b/src/main/kotlin/de/moritzruth/spigot_ttt/game/players/TTTPlayer.kt @@ -108,7 +108,9 @@ class TTTPlayer(player: Player, role: Role, val tttClass: TTTClassCompanion = TT ).call() reallyScream = event.scream - event.winnerRoleGroup?.run { GameManager.letRoleWin(primaryRole) } + if (GameManager.phase == GamePhase.COMBAT) { + event.winnerRoleGroup?.run { GameManager.letRoleWin(primaryRole) } + } } clearInventory(true) @@ -232,8 +234,7 @@ class TTTPlayer(player: Player, role: Role, val tttClass: TTTClassCompanion = TT item.getInstance(this)?.let { it.carrier = null if (removeInstance && (!becauseOfDeath || it.tttItem.removeInstanceOnDeath)) { - item.instancesByUUID.remove(it.uuid) - it.reset() + it.remove() } }