🐛 fix: don't allow migrating to current proxy

This commit is contained in:
2025-01-02 22:47:07 +07:00
parent bf439cf3f3
commit b3405cd6ad
2 changed files with 15 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import cafe.kirameki.minikuraVelocity.utils.createWebSocketClient
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.google.inject.Inject
import com.imaginarycode.minecraft.redisbungee.RedisBungeeAPI
import com.velocitypowered.api.command.CommandManager
import com.velocitypowered.api.command.CommandMeta
import com.velocitypowered.api.command.SimpleCommand
@@ -43,6 +44,7 @@ class Main @Inject constructor(private val logger: Logger, private val server: P
private val apiUrl: String = System.getenv("MINIKURA_API_URL") ?: "http://localhost:3000"
private val websocketUrl: String = System.getenv("MINIKURA_WEBSOCKET_URL") ?: "ws://localhost:3000/ws"
private var acceptingTransfers = AtomicBoolean(false)
private val redisBungeeApi = RedisBungeeAPI.getRedisBungeeApi()
@Subscribe
fun onProxyInitialization(event: ProxyInitializeEvent?) {
@@ -85,6 +87,7 @@ class Main @Inject constructor(private val logger: Logger, private val server: P
.plugin(this)
.build()
// TODO: Rework this command and support <origin> and <destination> arguments
val migrateCommand = SimpleCommand { p ->
val source = p.source()
val args = p.arguments()
@@ -102,6 +105,11 @@ class Main @Inject constructor(private val logger: Logger, private val server: P
return@SimpleCommand
}
if (targetServer.name == redisBungeeApi.proxyId) {
source.sendMessage(Component.text("Target server cannot be the current proxy server."))
return@SimpleCommand
}
ProxyTransferUtils.migratePlayersToServer(targetServer)
source.sendMessage(Component.text("Migrating players to server '$targetServerName'..."))
}

View File

@@ -31,7 +31,13 @@ object ProxyTransferUtils {
val targetAddress = InetSocketAddress(targetServer.address, targetServer.port)
val currentProxyName = redisBungeeApi.proxyId
val players = server.allPlayers.toList()
val playerOnThisProxy = redisBungeeApi.getPlayersOnProxy(currentProxyName)
.map { it.toString() }
val players = server.allPlayers
.toList()
.filter { playerOnThisProxy.contains(it.uniqueId.toString()) }
val batchSize = (players.size * 0.05).coerceAtLeast(1.0).toInt() // 5% of players per batch to avoid overloading the server
var currentIndex = 0