Skip to content

Commit

Permalink
Fix #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Minemobs committed Apr 1, 2024
1 parent 853c77e commit 3370545
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 37 deletions.
36 changes: 5 additions & 31 deletions src/main/kotlin/fr/sunderia/bomberman/Bomberman.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {

}
Expand Down
74 changes: 68 additions & 6 deletions src/main/kotlin/fr/sunderia/bomberman/party/Game.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
}
}
}

0 comments on commit 3370545

Please sign in to comment.