Cleanup game state
This commit is contained in:
@@ -8,6 +8,7 @@ import club.mcscrims.core.database.mongodb.MongoManager
|
|||||||
import club.mcscrims.speedhg.config.MessageConfig
|
import club.mcscrims.speedhg.config.MessageConfig
|
||||||
import club.mcscrims.speedhg.config.PluginConfig
|
import club.mcscrims.speedhg.config.PluginConfig
|
||||||
import club.mcscrims.speedhg.database.StatsRepository
|
import club.mcscrims.speedhg.database.StatsRepository
|
||||||
|
import club.mcscrims.speedhg.game.GameManager
|
||||||
import club.mcscrims.spigot.chat.ChatFormatter
|
import club.mcscrims.spigot.chat.ChatFormatter
|
||||||
import club.mcscrims.spigot.chat.ChatManager
|
import club.mcscrims.spigot.chat.ChatManager
|
||||||
import club.mcscrims.spigot.network.SpigotNetworkManager
|
import club.mcscrims.spigot.network.SpigotNetworkManager
|
||||||
@@ -35,6 +36,8 @@ class SpeedHG : JavaPlugin() {
|
|||||||
|
|
||||||
internal lateinit var networkManager: SpigotNetworkManager
|
internal lateinit var networkManager: SpigotNetworkManager
|
||||||
|
|
||||||
|
internal lateinit var gameManager: GameManager
|
||||||
|
|
||||||
internal lateinit var luckPerms: LuckPerms
|
internal lateinit var luckPerms: LuckPerms
|
||||||
|
|
||||||
override fun onEnable()
|
override fun onEnable()
|
||||||
@@ -54,6 +57,8 @@ class SpeedHG : JavaPlugin() {
|
|||||||
chatManager = ChatManager.withCustomConfig( this, chatFormatter )
|
chatManager = ChatManager.withCustomConfig( this, chatFormatter )
|
||||||
chatManager.initialize()
|
chatManager.initialize()
|
||||||
|
|
||||||
|
gameManager = GameManager( this )
|
||||||
|
|
||||||
setupLuckPerms()
|
setupLuckPerms()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,16 +2,20 @@ package club.mcscrims.speedhg.game
|
|||||||
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin
|
import org.bukkit.plugin.java.JavaPlugin
|
||||||
|
|
||||||
class GameManager(private val plugin: JavaPlugin) {
|
class GameManager(
|
||||||
|
private val plugin: JavaPlugin
|
||||||
|
) {
|
||||||
|
|
||||||
private var currentState: GameState? = null
|
private var currentState: GameState? = null
|
||||||
|
|
||||||
fun transitionTo(nextState: GameState) {
|
fun transitionTo(
|
||||||
|
nextState: GameState
|
||||||
|
) {
|
||||||
val previousState = currentState
|
val previousState = currentState
|
||||||
|
|
||||||
try {
|
try {
|
||||||
currentState?.onExit(nextState)
|
currentState?.onExit( nextState )
|
||||||
} catch (e: Exception) {
|
} catch ( e: Exception ) {
|
||||||
plugin.logger.severe("Error during onExit for state ${currentState?.name}: ${e.message}")
|
plugin.logger.severe("Error during onExit for state ${currentState?.name}: ${e.message}")
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
@@ -19,8 +23,8 @@ class GameManager(private val plugin: JavaPlugin) {
|
|||||||
currentState = nextState
|
currentState = nextState
|
||||||
|
|
||||||
try {
|
try {
|
||||||
nextState.onEnter(previousState)
|
nextState.onEnter( previousState )
|
||||||
} catch (e: Exception) {
|
} catch ( e: Exception ) {
|
||||||
plugin.logger.severe("Error during onEnter for state ${nextState.name}: ${e.message}")
|
plugin.logger.severe("Error during onEnter for state ${nextState.name}: ${e.message}")
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
@@ -28,8 +32,10 @@ class GameManager(private val plugin: JavaPlugin) {
|
|||||||
|
|
||||||
fun getCurrentState(): GameState? = currentState
|
fun getCurrentState(): GameState? = currentState
|
||||||
|
|
||||||
fun shutdown() {
|
fun shutdown()
|
||||||
currentState?.onExit(null)
|
{
|
||||||
|
currentState?.onExit( null )
|
||||||
currentState = null
|
currentState = null
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,72 +1,78 @@
|
|||||||
package club.mcscrims.speedhg.game
|
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.Bukkit
|
||||||
import org.bukkit.entity.Player
|
import org.bukkit.entity.Player
|
||||||
import org.bukkit.event.HandlerList
|
import org.bukkit.event.HandlerList
|
||||||
import org.bukkit.event.Listener
|
import org.bukkit.event.Listener
|
||||||
import org.bukkit.plugin.java.JavaPlugin
|
|
||||||
import org.bukkit.scheduler.BukkitTask
|
|
||||||
|
|
||||||
open class GameState(
|
open class GameState(
|
||||||
val name: String,
|
val name: String,
|
||||||
protected val gameManager: GameManager,
|
protected val gameManager: GameManager,
|
||||||
protected val plugin: JavaPlugin,
|
protected val plugin: SpeedHG,
|
||||||
|
protected val schedulerManager: SchedulerManager,
|
||||||
protected val durationSeconds: Int? = null
|
protected val durationSeconds: Int? = null
|
||||||
) {
|
) {
|
||||||
|
|
||||||
private val listeners = mutableListOf<Listener>()
|
private val listeners = mutableListOf<Listener>()
|
||||||
private var tickTask: BukkitTask? = null
|
private var tickTask: TaskRegistration? = null
|
||||||
private var remainingSeconds: Int = durationSeconds ?: 0
|
private var remainingSeconds: Int = durationSeconds ?: 0
|
||||||
private var isActive: Boolean = false
|
private var isActive: Boolean = false
|
||||||
|
|
||||||
open fun onEnter(previous: GameState?) {
|
open fun onEnter(
|
||||||
|
previous: GameState?
|
||||||
|
) {
|
||||||
isActive = true
|
isActive = true
|
||||||
remainingSeconds = durationSeconds ?: 0
|
remainingSeconds = durationSeconds ?: 0
|
||||||
|
|
||||||
if (durationSeconds != null && durationSeconds > 0) {
|
if ( durationSeconds != null && durationSeconds > 0 )
|
||||||
startTicking()
|
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
|
isActive = false
|
||||||
stopTicking()
|
stopTicking()
|
||||||
unregisterAllListeners()
|
unregisterAllListeners()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startTicking() {
|
private fun startTicking()
|
||||||
tickTask = Bukkit.getScheduler().runTaskTimer(plugin, Runnable {
|
{
|
||||||
if (!isActive) {
|
tickTask = schedulerManager.runRepeating( 20L, 20L )
|
||||||
|
{
|
||||||
|
if ( !isActive )
|
||||||
|
{
|
||||||
stopTicking()
|
stopTicking()
|
||||||
return@Runnable
|
return@runRepeating
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
onTick()
|
onTick()
|
||||||
} catch (e: Exception) {
|
} catch ( e: Exception ) {
|
||||||
plugin.logger.severe("Error during onTick for state $name: ${e.message}")
|
plugin.logger.severe("Error during onTick for state $name: ${e.message}")
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (durationSeconds != null && remainingSeconds > 0) {
|
if ( durationSeconds != null && remainingSeconds > 0 )
|
||||||
|
{
|
||||||
remainingSeconds--
|
remainingSeconds--
|
||||||
|
|
||||||
if (remainingSeconds <= 0) {
|
if ( remainingSeconds <= 0)
|
||||||
try {
|
try {
|
||||||
onEndOfDuration()
|
onEndOfDuration()
|
||||||
} catch (e: Exception) {
|
} catch ( e: Exception ) {
|
||||||
plugin.logger.severe("Error during onEndOfDuration for state $name: ${e.message}")
|
plugin.logger.severe("Error during onEndOfDuration for state $name: ${e.message}")
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 20L, 20L)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun stopTicking() {
|
private fun stopTicking() {
|
||||||
@@ -74,37 +80,46 @@ open class GameState(
|
|||||||
tickTask = null
|
tickTask = null
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun registerListener(listener: Listener) {
|
protected fun registerListener(
|
||||||
listeners.add(listener)
|
listener: Listener
|
||||||
Bukkit.getPluginManager().registerEvents(listener, plugin)
|
) {
|
||||||
|
listeners.add( listener )
|
||||||
|
Bukkit.getPluginManager().registerEvents( listener, plugin )
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun unregisterAllListeners() {
|
private fun unregisterAllListeners()
|
||||||
listeners.forEach { listener ->
|
{
|
||||||
HandlerList.unregisterAll(listener)
|
listeners.forEach { listener -> HandlerList.unregisterAll( listener ) }
|
||||||
}
|
|
||||||
listeners.clear()
|
listeners.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun transitionTo(next: GameState) {
|
protected fun transitionTo(
|
||||||
gameManager.transitionTo(next)
|
next: GameState
|
||||||
|
) {
|
||||||
|
gameManager.transitionTo( next )
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun broadcast(message: String) {
|
protected fun broadcast(
|
||||||
Bukkit.broadcastMessage(message)
|
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()
|
Bukkit.getOnlinePlayers()
|
||||||
.filter { !it.isDead }
|
.filter { !it.isDead }
|
||||||
.forEach { player ->
|
.forEach { player ->
|
||||||
try {
|
try {
|
||||||
action(player)
|
action( player )
|
||||||
} catch (e: Exception) {
|
} catch ( e: Exception ) {
|
||||||
plugin.logger.warning("Error executing action for player ${player.name}: ${e.message}")
|
plugin.logger.warning("Error executing action for player ${player.name}: ${e.message}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getRemainingSeconds(): Int = remainingSeconds
|
fun getRemainingSeconds(): Int = remainingSeconds
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user