diff --git a/src/main/kotlin/fr/sunderia/bomberman/Bomberman.kt b/src/main/kotlin/fr/sunderia/bomberman/Bomberman.kt index 0f808e9..49eedf1 100644 --- a/src/main/kotlin/fr/sunderia/bomberman/Bomberman.kt +++ b/src/main/kotlin/fr/sunderia/bomberman/Bomberman.kt @@ -111,8 +111,11 @@ class Bomberman { Party.removePlayerFromParty(it.player) val instance = it.instance if(!it.instance.hasTag(Tag.Boolean("game"))) return@addListener - if(instance.players.none { p -> p.uuid != it.player.uuid }) { + val filter = instance.players.filter { p -> p.uuid != it.player.uuid } + if(filter.isEmpty()) { Game.removeGame(instance) + } else if(filter.filter { p -> p.gameMode == GameMode.ADVENTURE }.size == 1 ) { + Game.playerLeft(instance) } } @@ -180,17 +183,7 @@ class Bomberman { winner.sendTitlePart(TitlePart.TITLE, Component.text("You won", NamedTextColor.GREEN)) winner.teleport(spawn) val game = Game.getGame(event.instance)!! - game.gameStatus = GameStatus.ENDING - MinecraftServer.getSchedulerManager().submitTask { - if (game.getTimeLeft() == 0) { - resetGame(event.instance) - game.endGame() - return@submitTask TaskSchedule.stop() - } - game.instance.players.forEach { it.sendMessage(Component.text("Closing in ").append(Component.text(game.getTimeLeft()).color(NamedTextColor.RED))) } - game.decreaseTime() - return@submitTask TaskSchedule.seconds(1) - } + game.endGame() } gameNode.addListener(PlayerBlockPlaceEvent::class.java) { @@ -224,25 +217,6 @@ class Bomberman { extensionNode.addChild(gameNode) } - private fun resetGame(instance: Instance) { - powerMap.replaceAll { _: UUID, _: Int -> 2 } - instance.players.forEach(Consumer { p: Player -> - p.clearEffects() - val uuids = - p.getAttribute(Attribute.MOVEMENT_SPEED).modifiers.stream() - .map { obj: AttributeModifier -> obj.id } - .toList().toTypedArray() - for (uuid in uuids) { - p.getAttribute(Attribute.MOVEMENT_SPEED).removeModifier(uuid!!) - } - }) - instance.entities.stream() - .filter { e: Entity -> - e.entityType.id() == EntityType.TNT.id() || e.entityType.id() == EntityType.ITEM.id() - } - .forEach { obj: Entity -> obj.remove() } - } - fun terminate() { } diff --git a/src/main/kotlin/fr/sunderia/bomberman/party/Game.kt b/src/main/kotlin/fr/sunderia/bomberman/party/Game.kt index 02d7d14..f6c039a 100644 --- a/src/main/kotlin/fr/sunderia/bomberman/party/Game.kt +++ b/src/main/kotlin/fr/sunderia/bomberman/party/Game.kt @@ -1,34 +1,90 @@ package fr.sunderia.bomberman.party import fr.sunderia.bomberman.Bomberman +import fr.sunderia.bomberman.Bomberman.Companion.powerMap import fr.sunderia.bomberman.InstanceCreator.Companion.createInstanceContainer import fr.sunderia.bomberman.InstanceCreator.Companion.generateStructure import net.kyori.adventure.text.Component +import net.kyori.adventure.text.Component.text +import net.kyori.adventure.text.format.NamedTextColor import net.minestom.server.MinecraftServer +import net.minestom.server.attribute.Attribute +import net.minestom.server.attribute.AttributeModifier +import net.minestom.server.entity.Entity +import net.minestom.server.entity.EntityType +import net.minestom.server.entity.Player import net.minestom.server.instance.Instance import net.minestom.server.instance.InstanceContainer import net.minestom.server.instance.InstanceManager import net.minestom.server.tag.Tag import net.minestom.server.timer.SchedulerManager +import net.minestom.server.timer.TaskSchedule +import java.util.* data class Game(val instance: InstanceContainer, val scheduler: SchedulerManager = MinecraftServer.getSchedulerManager(), var gameStatus: GameStatus = GameStatus.WAITING) { private var timeLeftBeforeClose = 5 + private var timeLeftBeforeStart = 10 - fun decreaseTime() { - timeLeftBeforeClose-- + init { + val manager = MinecraftServer.getSchedulerManager() + manager.submitTask { + if(timeLeftBeforeStart == 0) { + instance.sendMessage(text("Starting game")) + gameStatus = GameStatus.RUNNING + return@submitTask TaskSchedule.stop() + } + if(instance.players.size >= 2) { + timeLeftBeforeStart-- + instance.sendMessage(text("Starting in $timeLeftBeforeStart seconds")) + } else { + timeLeftBeforeStart = 10 + } + return@submitTask TaskSchedule.seconds(1) + } } - fun getTimeLeft() = timeLeftBeforeClose - - fun endGame() { + private fun closeGame() { this.instance.players.forEach { - it.sendMessage(Component.text("Game closed")) + it.sendMessage(text("Game closed")) it.instance = Bomberman.getLobbyInstance() it.inventory.clear() } removeGame(this.instance) } + fun endGame() { + this.gameStatus = GameStatus.ENDING + MinecraftServer.getSchedulerManager().submitTask { + if (this.timeLeftBeforeClose == 0) { + resetGame(this.instance) + this.closeGame() + return@submitTask TaskSchedule.stop() + } + this.instance.sendMessage(text("Closing in ").append(text(this.timeLeftBeforeClose).color(NamedTextColor.RED))) + this.timeLeftBeforeClose-- + return@submitTask TaskSchedule.seconds(1) + } + } + + private fun resetGame(instance: Instance) { + powerMap.replaceAll { _: UUID, _: Int -> 2 } + instance.players.forEach { p: Player -> + p.clearEffects() + val uuids = + p.getAttribute(Attribute.MOVEMENT_SPEED).modifiers.stream() + .map { obj: AttributeModifier -> obj.id } + .toList().toTypedArray() + for (uuid in uuids) { + p.getAttribute(Attribute.MOVEMENT_SPEED).removeModifier(uuid!!) + } + } + instance.entities.stream() + .filter { e: Entity -> + e.entityType.id() == EntityType.TNT.id() || e.entityType.id() == EntityType.ITEM.id() + } + .forEach { obj: Entity -> obj.remove() } + } + companion object { // Can be bypassed by having a party with more than 4 players private const val MAX_PLAYERS_PER_GAME = 4 @@ -50,5 +106,11 @@ data class Game(val instance: InstanceContainer, val scheduler: SchedulerManager games[container] = game return container } + + fun playerLeft(instance: Instance) { + val game = games[instance]!! + game.instance.players.first().sendMessage(text("You won!")) + game.endGame() + } } }