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.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()
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,15 @@ 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 {
|
||||
@@ -28,8 +32,10 @@ class GameManager(private val plugin: JavaPlugin) {
|
||||
|
||||
fun getCurrentState(): GameState? = currentState
|
||||
|
||||
fun shutdown() {
|
||||
fun shutdown()
|
||||
{
|
||||
currentState?.onExit( null )
|
||||
currentState = null
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,50 +1,56 @@
|
||||
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) {
|
||||
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) {
|
||||
private fun startTicking()
|
||||
{
|
||||
tickTask = schedulerManager.runRepeating( 20L, 20L )
|
||||
{
|
||||
if ( !isActive )
|
||||
{
|
||||
stopTicking()
|
||||
return@Runnable
|
||||
return@runRepeating
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -54,10 +60,11 @@ open class GameState(
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
if (durationSeconds != null && remainingSeconds > 0) {
|
||||
if ( durationSeconds != null && remainingSeconds > 0 )
|
||||
{
|
||||
remainingSeconds--
|
||||
|
||||
if (remainingSeconds <= 0) {
|
||||
if ( remainingSeconds <= 0)
|
||||
try {
|
||||
onEndOfDuration()
|
||||
} catch ( e: Exception ) {
|
||||
@@ -66,7 +73,6 @@ open class GameState(
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 20L, 20L)
|
||||
}
|
||||
|
||||
private fun stopTicking() {
|
||||
@@ -74,27 +80,35 @@ open class GameState(
|
||||
tickTask = null
|
||||
}
|
||||
|
||||
protected fun registerListener(listener: Listener) {
|
||||
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) {
|
||||
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 ->
|
||||
@@ -107,4 +121,5 @@ open class GameState(
|
||||
}
|
||||
|
||||
fun getRemainingSeconds(): Int = remainingSeconds
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user