Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
kotlin.code.style=official
kotlin.stdlib.default.dependency=false
org.gradle.parallel=true
version=1.21.11-1.2.5-SNAPSHOT
version=1.21.11-1.2.6-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ sealed interface CommonSurfServer {
fun isBackend() = this is SurfServer

companion object {
operator fun get(name: String) = surfCoreApi.getServerByName(name)
operator fun get(name: String) = surfCoreApi.getCommonServerByName(name)
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing CommonSurfServer[name] to call getCommonServerByName widens the operator’s return type from SurfServer? to CommonSurfServer?. There are existing call sites that relied on the old type inference (e.g. surf-core-velocity/.../ConnectionListener.kt uses CommonSurfServer[toServer] and passes it into SurfPlayer.copy(currentServer = ...), where currentServer is a SurfServer?). After this change that code will no longer type-check. Consider updating those call sites to use SurfServer[name] when a backend server is required, or otherwise explicitly narrow/cast the result before assigning to currentServer.

Suggested change
operator fun get(name: String) = surfCoreApi.getCommonServerByName(name)
operator fun get(name: String): SurfServer? =
surfCoreApi.getCommonServerByName(name) as? SurfServer

Copilot uses AI. Check for mistakes.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ data class SurfServer(

companion object {
fun current() = surfCoreApi.getCurrentServer()

operator fun get(name: String) = surfCoreApi.getServerByName(name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import dev.slne.surf.core.velocity.listener.VelocityServerListener
import dev.slne.surf.core.velocity.redis.handler.SendPlayerToProxyHandler
import dev.slne.surf.core.velocity.redis.handler.SendPlayerToServerHandler
import dev.slne.surf.core.velocity.redis.listener.VelocityRedisListener
import dev.slne.surf.core.velocity.task.surfPlayerSyncTask
import dev.slne.surf.surfapi.core.api.messages.adventure.buildText
import kotlinx.coroutines.runBlocking
import net.kyori.adventure.text.format.TextDecoration
Expand Down Expand Up @@ -89,10 +90,14 @@ class VelocityMain @Inject constructor(
eventManager.register(this, VelocityServerListener)

surfServerService.changeState(SurfProxyServer.current(), SurfServerState.RUNNING)

surfPlayerSyncTask.start()
}

@Subscribe
fun onProxyShutdown(event: ProxyShutdownEvent) {
surfPlayerSyncTask.stop()

surfEventBus.fire(SurfServerStoppingEvent(surfServerConfig.serverName))

surfCoreApi.getOnlinePlayers().forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import com.velocitypowered.api.event.player.ServerConnectedEvent
import com.velocitypowered.api.util.GameProfile
import dev.slne.surf.core.api.common.event.SurfPlayerConnectEvent
import dev.slne.surf.core.api.common.event.SurfPlayerDisconnectEvent
import dev.slne.surf.core.api.common.server.CommonSurfServer
import dev.slne.surf.core.api.common.server.SurfProxyServer
import dev.slne.surf.core.api.common.server.SurfServer
import dev.slne.surf.core.core.common.event.surfEventBus
import dev.slne.surf.core.core.common.player.history.surfPlayerIpAddressHistoryService
import dev.slne.surf.core.core.common.player.history.surfPlayerNameHistoryService
Expand Down Expand Up @@ -122,7 +122,7 @@ object ConnectionListener {
println("[connection update] $playerName was redirected from '$fromServer' to '$toServer'")


val server = CommonSurfServer[toServer] ?: error("SurfServer '$toServer' not found")
val server = SurfServer[toServer] ?: error("SurfServer '$toServer' not found")
val player =
surfPlayerService.players.firstOrNull { it.uuid == playerUuid }
?.copy(currentServer = server)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package dev.slne.surf.core.velocity.task

import com.github.shynixn.mccoroutine.velocity.launch
import com.velocitypowered.api.scheduler.ScheduledTask
import dev.slne.surf.core.api.common.server.SurfProxyServer
import dev.slne.surf.core.api.common.server.SurfServer
import dev.slne.surf.core.core.common.player.surfPlayerService
import dev.slne.surf.core.velocity.plugin
import dev.slne.surf.core.velocity.proxy
import java.util.concurrent.TimeUnit
import kotlin.jvm.optionals.getOrNull

val surfPlayerSyncTask = SurfPlayerSyncTask()

class SurfPlayerSyncTask {
private var task: ScheduledTask? = null

fun start() {
task = proxy.scheduler
.buildTask(plugin, Runnable { syncPlayers() })
.delay(1L, TimeUnit.MINUTES)
.repeat(1L, TimeUnit.MINUTES)
.schedule()
}

fun stop() {
task?.cancel()
task = null
}

private fun syncPlayers() {
plugin.pluginContainer.launch {
val onlinePlayers = proxy.allPlayers.map { velocityPlayer ->
Comment on lines +18 to +33
Comment on lines +26 to +33
val surfPlayer = surfPlayerService.findPlayerByUuid(velocityPlayer.uniqueId)
?: surfPlayerService.getOrLoadOrCreatePlayerByUuid(velocityPlayer.uniqueId)
.also {
it.currentProxy = SurfProxyServer.current()
}
val currentServerName = velocityPlayer.currentServer.getOrNull()?.serverInfo?.name
val currentServer = currentServerName?.let { SurfServer[it] }
surfPlayer.copy(currentServer = currentServer)
}
Comment on lines +34 to +42

val onlineUuids = onlinePlayers.map { it.uuid }.toHashSet()

SurfProxyServer.current().getPlayers()
.filter { it.uuid !in onlineUuids }
.forEach {
surfPlayerService.invalidatePlayer(it.uuid)
plugin.logger.info("Found invalid player ${it.uuid} (${it.username}), invalidating cache")
}

onlinePlayers.forEach { surfPlayerService.cachePlayer(it) }
}
}
}