Skip to content

Commit

Permalink
fix(mindustry): Fixed autopause not applying on server start (#323)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason <[email protected]>
Co-authored-by: phinner <[email protected]>
  • Loading branch information
3 people authored Jan 14, 2025
1 parent 8b29deb commit 07674e8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -185,7 +186,7 @@ class ImperiumPlugin : AbstractMindustryPlugin() {
TeamCommand::class,
FormationListener::class,
ControlListener::class,
UnpauseListener::class,
PauseListener::class,
AchievementCommand::class,
LogicListener::class,
SaveCommand::class,
Expand Down Expand Up @@ -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<SaveLoadEvent> { _ ->
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayerJoin> {
if (Vars.state.isPaused) {
Call.sendMessage(
"[lightgray]The server is paused, type [orange]/unpause[lightgray] to unpause the server")
}
}

onEvent<StateChangeEvent> {
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")
}
}
}
}

0 comments on commit 07674e8

Please sign in to comment.