Skip to content

Commit 0a99723

Browse files
fix: fallback server in ServerConnectionPlugin
1 parent a77fcd5 commit 0a99723

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

connection-shared/src/main/kotlin/app/simplecloud/plugin/connection/shared/ServerConnectionPlugin.kt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,27 @@ class ServerConnectionPlugin<P>(
3737

3838
private fun getConnectionAndName(player: P, targetConnections: List<TargetConnectionConfig>, fromServerName: String = ""): Pair<ConnectionAndTargetConfig, String>? {
3939
val possibleConnections = getPossibleServerConnections(player)
40-
val possibleConnectionsWithTarget = possibleConnections.mapNotNull { connection ->
41-
val targetConfig = targetConnections
42-
.filter { fromServerName.isBlank() || it.from.any { matcher -> matcher.matches(fromServerName) } }
43-
.firstOrNull { connection.name == it.name }?: return@mapNotNull null
44-
ConnectionAndTargetConfig(connection, targetConfig)
40+
val possibleConnectionsWithTarget = possibleConnections.map { possibleConnection ->
41+
val targetConfig= targetConnections
42+
.filter { fromServerName.isBlank() || matchesTargetConnection(it, fromServerName) }
43+
.firstOrNull { possibleConnection.name == it.name } ?: return null
44+
ConnectionAndTargetConfig(possibleConnection, targetConfig)
4545
}
46-
val connectionAndTargetConfig = possibleConnectionsWithTarget.maxByOrNull { it.targetConfig.priority }?: return null
4746

48-
val bestServerToConnect = getBestServerToConnect(connectionAndTargetConfig.connectionConfig)?: return null
47+
val connectionAndTargetConfig = possibleConnectionsWithTarget.maxByOrNull { it.targetConfig.priority }?: return null
48+
val bestServerToConnect = getBestServerToConnect(fromServerName, connectionAndTargetConfig.connectionConfig)?: return null
4949
return Pair(connectionAndTargetConfig, bestServerToConnect)
5050
}
5151

52+
private fun matchesTargetConnection(
53+
targetConnectionConfig: TargetConnectionConfig,
54+
fromServerName: String
55+
): Boolean {
56+
if (targetConnectionConfig.from.isEmpty())
57+
return true
58+
return targetConnectionConfig.from.any { it.matches(fromServerName) }
59+
}
60+
5261
private fun getPossibleServerConnections(
5362
player: P
5463
): List<ConnectionConfig> {
@@ -61,12 +70,12 @@ class ServerConnectionPlugin<P>(
6170
}
6271
}
6372

64-
private fun getBestServerToConnect(bestConnection: ConnectionConfig): String? {
73+
private fun getBestServerToConnect(fromServerName: String, bestConnection: ConnectionConfig): String? {
6574
val serverConnectionInfos = serverConnectionInfoGetter.get()
6675
val bestServer = serverConnectionInfos
76+
.filter { it.name != fromServerName }
6777
.sortedBy { it.onlinePlayers }
6878
.firstOrNull { bestConnection.serverNameMatcher.matches(it.name) }
69-
7079
return bestServer?.name
7180
}
7281

0 commit comments

Comments
 (0)