diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/ImperiumPlugin.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/ImperiumPlugin.kt index 22a8836b..737d5c1a 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/ImperiumPlugin.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/ImperiumPlugin.kt @@ -58,15 +58,16 @@ import com.xpdustry.imperium.mindustry.game.ChangelogCommand import com.xpdustry.imperium.mindustry.game.GameListener import com.xpdustry.imperium.mindustry.game.ImperiumLogicListener import com.xpdustry.imperium.mindustry.game.LogicListener +import com.xpdustry.imperium.mindustry.game.PauseListener import com.xpdustry.imperium.mindustry.game.RatingListener import com.xpdustry.imperium.mindustry.game.TeamCommand import com.xpdustry.imperium.mindustry.game.TipListener -import com.xpdustry.imperium.mindustry.game.UnpauseListener import com.xpdustry.imperium.mindustry.game.formation.FormationListener import com.xpdustry.imperium.mindustry.history.HistoryCommand import com.xpdustry.imperium.mindustry.metrics.MetricsListener import com.xpdustry.imperium.mindustry.misc.ImperiumMetadataChunkReader import com.xpdustry.imperium.mindustry.misc.getMindustryVersion +import com.xpdustry.imperium.mindustry.misc.onEvent import com.xpdustry.imperium.mindustry.permission.ImperiumRankPermissionSource import com.xpdustry.imperium.mindustry.permission.ImperiumRankProvider import com.xpdustry.imperium.mindustry.security.AdminRequestListener @@ -95,20 +96,20 @@ import com.xpdustry.imperium.mindustry.world.WorldEditCommand import kotlin.reflect.KClass import kotlin.system.exitProcess import kotlinx.coroutines.runBlocking +import mindustry.Vars +import mindustry.core.GameState +import mindustry.game.EventType.SaveLoadEvent +import mindustry.gen.Groups import mindustry.io.SaveVersion +import mindustry.net.Administration +import mindustry.server.ServerControl class ImperiumPlugin : AbstractMindustryPlugin() { private val application = MindustryImperiumApplication() override fun onInit() { - // https://github.com/Anuken/Arc/pull/158 if (getMindustryVersion().build < 147) { - Core.app = - object : Application by Core.app { - override fun removeListener(listener: ApplicationListener) { - post { synchronized(listeners) { listeners.remove(listener) } } - } - } + applyBackportFixes() } } @@ -185,7 +186,7 @@ class ImperiumPlugin : AbstractMindustryPlugin() { TeamCommand::class, FormationListener::class, ControlListener::class, - UnpauseListener::class, + PauseListener::class, AchievementCommand::class, LogicListener::class, SaveCommand::class, @@ -235,6 +236,28 @@ class ImperiumPlugin : AbstractMindustryPlugin() { .register(this@ImperiumPlugin, klass.java, instance, Priority.NORMAL) } + private fun applyBackportFixes() { + // https://github.com/Anuken/Arc/pull/158 + Core.app = + object : Application by Core.app { + override fun removeListener(listener: ApplicationListener) { + post { synchronized(listeners) { listeners.remove(listener) } } + } + } + + // https://github.com/Anuken/Mindustry/issues/9422 + onEvent { _ -> + Core.app.post { + if (Administration.Config.autoPause.bool() && Groups.player.size() == 0) { + Vars.state.set(GameState.State.paused) + val autoPaused = ServerControl::class.java.getField("autoPaused") + autoPaused.isAccessible = true + autoPaused.set(ServerControl.instance, true) + } + } + } + } + private inner class MindustryImperiumApplication : BaseImperiumApplication(logger) { private var exited = false diff --git a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/game/UnpauseListener.kt b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/game/PauseListener.kt similarity index 52% rename from imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/game/UnpauseListener.kt rename to imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/game/PauseListener.kt index 793b6e48..7bec1436 100644 --- a/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/game/UnpauseListener.kt +++ b/imperium-mindustry/src/main/kotlin/com/xpdustry/imperium/mindustry/game/PauseListener.kt @@ -18,42 +18,30 @@ package com.xpdustry.imperium.mindustry.game import com.xpdustry.distributor.api.command.CommandSender +import com.xpdustry.imperium.common.account.Rank import com.xpdustry.imperium.common.application.ImperiumApplication import com.xpdustry.imperium.common.command.ImperiumCommand import com.xpdustry.imperium.mindustry.command.annotation.ClientSide -import com.xpdustry.imperium.mindustry.command.annotation.ServerSide -import com.xpdustry.imperium.mindustry.misc.onEvent import mindustry.Vars import mindustry.core.GameState -import mindustry.game.EventType.PlayerJoin -import mindustry.game.EventType.StateChangeEvent -import mindustry.gen.Call -class UnpauseListener : ImperiumApplication.Listener { +class PauseListener : ImperiumApplication.Listener { - override fun onImperiumInit() { - onEvent { - if (Vars.state.isPaused) { - Call.sendMessage( - "[lightgray]The server is paused, type [orange]/unpause[lightgray] to unpause the server") - } - } - - onEvent { - if (it.from == GameState.State.paused && it.to == GameState.State.playing) { - Call.sendMessage("[lightgray]The server has been unpaused") - } - } - } - - @ImperiumCommand(["unpause"]) + @ImperiumCommand(["pause"], rank = Rank.MODERATOR) @ClientSide - @ServerSide - fun onUnpauseCommand(sender: CommandSender) { + fun onPauseCommand(sender: CommandSender) { when (Vars.state.state!!) { - GameState.State.playing -> sender.error("The server is already unpaused") - GameState.State.paused -> Vars.state.set(GameState.State.playing) - GameState.State.menu -> sender.error("The server is not running") + GameState.State.paused -> { + Vars.state.set(GameState.State.playing) + sender.reply("The server has been un-paused") + } + GameState.State.playing -> { + Vars.state.set(GameState.State.paused) + sender.reply("The server has been paused") + } + GameState.State.menu -> { + sender.error("The server is not running") + } } } }