Cleanup game state

This commit is contained in:
Laurin
2025-12-01 03:58:51 +01:00
parent 486931b72f
commit 5829770fea
3 changed files with 85 additions and 59 deletions

View File

@@ -8,6 +8,7 @@ import club.mcscrims.core.database.mongodb.MongoManager
import club.mcscrims.speedhg.config.MessageConfig
import club.mcscrims.speedhg.config.PluginConfig
import club.mcscrims.speedhg.database.StatsRepository
import club.mcscrims.speedhg.game.GameManager
import club.mcscrims.spigot.chat.ChatFormatter
import club.mcscrims.spigot.chat.ChatManager
import club.mcscrims.spigot.network.SpigotNetworkManager
@@ -35,6 +36,8 @@ class SpeedHG : JavaPlugin() {
internal lateinit var networkManager: SpigotNetworkManager
internal lateinit var gameManager: GameManager
internal lateinit var luckPerms: LuckPerms
override fun onEnable()
@@ -54,6 +57,8 @@ class SpeedHG : JavaPlugin() {
chatManager = ChatManager.withCustomConfig( this, chatFormatter )
chatManager.initialize()
gameManager = GameManager( this )
setupLuckPerms()
}

View File

@@ -2,16 +2,20 @@ package club.mcscrims.speedhg.game
import org.bukkit.plugin.java.JavaPlugin
class GameManager(private val plugin: JavaPlugin) {
class GameManager(
private val plugin: JavaPlugin
) {
private var currentState: GameState? = null
fun transitionTo(nextState: GameState) {
fun transitionTo(
nextState: GameState
) {
val previousState = currentState
try {
currentState?.onExit(nextState)
} catch (e: Exception) {
currentState?.onExit( nextState )
} catch ( e: Exception ) {
plugin.logger.severe("Error during onExit for state ${currentState?.name}: ${e.message}")
e.printStackTrace()
}
@@ -19,8 +23,8 @@ class GameManager(private val plugin: JavaPlugin) {
currentState = nextState
try {
nextState.onEnter(previousState)
} catch (e: Exception) {
nextState.onEnter( previousState )
} catch ( e: Exception ) {
plugin.logger.severe("Error during onEnter for state ${nextState.name}: ${e.message}")
e.printStackTrace()
}
@@ -28,8 +32,10 @@ class GameManager(private val plugin: JavaPlugin) {
fun getCurrentState(): GameState? = currentState
fun shutdown() {
currentState?.onExit(null)
fun shutdown()
{
currentState?.onExit( null )
currentState = null
}
}

View File

@@ -1,72 +1,78 @@
package club.mcscrims.speedhg.game
import club.mcscrims.speedhg.SpeedHG
import club.mcscrims.spigot.scheduler.SchedulerManager
import club.mcscrims.spigot.scheduler.TaskRegistration
import org.bukkit.Bukkit
import org.bukkit.entity.Player
import org.bukkit.event.HandlerList
import org.bukkit.event.Listener
import org.bukkit.plugin.java.JavaPlugin
import org.bukkit.scheduler.BukkitTask
open class GameState(
val name: String,
protected val gameManager: GameManager,
protected val plugin: JavaPlugin,
protected val plugin: SpeedHG,
protected val schedulerManager: SchedulerManager,
protected val durationSeconds: Int? = null
) {
private val listeners = mutableListOf<Listener>()
private var tickTask: BukkitTask? = null
private var tickTask: TaskRegistration? = null
private var remainingSeconds: Int = durationSeconds ?: 0
private var isActive: Boolean = false
open fun onEnter(previous: GameState?) {
open fun onEnter(
previous: GameState?
) {
isActive = true
remainingSeconds = durationSeconds ?: 0
if (durationSeconds != null && durationSeconds > 0) {
startTicking()
}
if ( durationSeconds != null && durationSeconds > 0 )
startTicking()
}
open fun onTick() {
}
open fun onTick() {}
open fun onEndOfDuration() {
}
open fun onEndOfDuration() {}
open fun onExit(next: GameState?) {
open fun onExit(
next: GameState?
) {
isActive = false
stopTicking()
unregisterAllListeners()
}
private fun startTicking() {
tickTask = Bukkit.getScheduler().runTaskTimer(plugin, Runnable {
if (!isActive) {
stopTicking()
return@Runnable
}
private fun startTicking()
{
tickTask = schedulerManager.runRepeating( 20L, 20L )
{
if ( !isActive )
{
stopTicking()
return@runRepeating
}
try {
onTick()
} catch (e: Exception) {
plugin.logger.severe("Error during onTick for state $name: ${e.message}")
e.printStackTrace()
}
try {
onTick()
} catch ( e: Exception ) {
plugin.logger.severe("Error during onTick for state $name: ${e.message}")
e.printStackTrace()
}
if (durationSeconds != null && remainingSeconds > 0) {
remainingSeconds--
if ( durationSeconds != null && remainingSeconds > 0 )
{
remainingSeconds--
if (remainingSeconds <= 0) {
try {
onEndOfDuration()
} catch (e: Exception) {
plugin.logger.severe("Error during onEndOfDuration for state $name: ${e.message}")
e.printStackTrace()
}
}
}
}, 20L, 20L)
if ( remainingSeconds <= 0)
try {
onEndOfDuration()
} catch ( e: Exception ) {
plugin.logger.severe("Error during onEndOfDuration for state $name: ${e.message}")
e.printStackTrace()
}
}
}
}
private fun stopTicking() {
@@ -74,37 +80,46 @@ open class GameState(
tickTask = null
}
protected fun registerListener(listener: Listener) {
listeners.add(listener)
Bukkit.getPluginManager().registerEvents(listener, plugin)
protected fun registerListener(
listener: Listener
) {
listeners.add( listener )
Bukkit.getPluginManager().registerEvents( listener, plugin )
}
private fun unregisterAllListeners() {
listeners.forEach { listener ->
HandlerList.unregisterAll(listener)
}
private fun unregisterAllListeners()
{
listeners.forEach { listener -> HandlerList.unregisterAll( listener ) }
listeners.clear()
}
protected fun transitionTo(next: GameState) {
gameManager.transitionTo(next)
protected fun transitionTo(
next: GameState
) {
gameManager.transitionTo( next )
}
protected fun broadcast(message: String) {
Bukkit.broadcastMessage(message)
protected fun broadcast(
messageKey: String,
vararg placeholders: Pair<String, String>
) {
plugin.chatManager.broadcast( messageKey, *placeholders )
}
protected fun forAlivePlayers(action: (Player) -> Unit) {
protected fun forAlivePlayers(
action: (Player) -> Unit
) {
Bukkit.getOnlinePlayers()
.filter { !it.isDead }
.forEach { player ->
try {
action(player)
} catch (e: Exception) {
action( player )
} catch ( e: Exception ) {
plugin.logger.warning("Error executing action for player ${player.name}: ${e.message}")
}
}
}
fun getRemainingSeconds(): Int = remainingSeconds
}