Skip to content

Commit b7deffb

Browse files
committed
feat: optional player synchronization
1 parent 28517d8 commit b7deffb

File tree

15 files changed

+210
-4
lines changed

15 files changed

+210
-4
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ plugins {
99
`maven-publish`
1010
}
1111

12-
val baseVersion = "0.0.5"
12+
val baseVersion = "0.0.6"
1313
val commitHash = System.getenv("COMMIT_HASH")
1414
val timestamp = System.currentTimeMillis() // Temporary to be able to build and publish directly out of fix branch with same commit hash
1515
val snapshotVersion = "${baseVersion}-dev.${timestamp}-${commitHash}"

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[versions]
22
kotlin = "2.1.20"
3-
kotlin-coroutines = "1.10.1"
3+
kotlin-coroutines = "1.10.2"
44
velocity = "3.4.0-SNAPSHOT"
55
bungeecord = "1.20-R0.2-SNAPSHOT"
66
shadow = "8.3.3"

platform/bungeecord/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies {
99
implementation(rootProject.libs.kotlin.reflect)
1010
implementation(rootProject.libs.kotlin.coroutines)
1111
compileOnly(rootProject.libs.bungeecord)
12+
compileOnly(project(":platform:shared"))
1213
api(rootProject.libs.simplecloud.controller) {
1314
exclude(group = "org.jetbrains.kotlin")
1415
exclude(group = "org.jetbrains.kotlinx")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
package app.simplecloud.api.platform.bungeecord
22

3+
import app.simplecloud.api.platform.shared.PlayerSynchronizer
4+
import app.simplecloud.controller.api.ControllerApi
35
import net.md_5.bungee.api.plugin.Plugin
46

57
class BungeeCordApiProvider : Plugin() {
8+
9+
private val controllerApi = ControllerApi.createCoroutineApi()
10+
private val playerSynchronizer = PlayerSynchronizer(controllerApi) { proxy.onlineCount.toLong() }
11+
612
override fun onEnable() {
713
logger.info("SimpleCloud v3 API provider initialized!")
14+
proxy.pluginManager.registerListener( this, PlayerConnectionListener(playerSynchronizer))
15+
16+
playerSynchronizer.start()
817
}
918

1019
override fun onDisable() {
1120
logger.info("SimpleCloud v3 API provider uninitialized!")
21+
playerSynchronizer.stop()
1222
}
1323

1424
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package app.simplecloud.api.platform.bungeecord
2+
3+
import app.simplecloud.api.platform.shared.PlayerSynchronizer
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.launch
7+
import net.md_5.bungee.api.event.PlayerDisconnectEvent
8+
import net.md_5.bungee.api.event.PostLoginEvent
9+
import net.md_5.bungee.api.plugin.Listener
10+
import net.md_5.bungee.event.EventHandler
11+
12+
class PlayerConnectionListener(
13+
private val playerSynchronizer: PlayerSynchronizer
14+
) : Listener {
15+
16+
@EventHandler
17+
fun onPlayerJoin(event: PostLoginEvent) {
18+
CoroutineScope(Dispatchers.Default).launch {
19+
playerSynchronizer.updatePlayerCount()
20+
}
21+
}
22+
23+
@EventHandler
24+
fun onPlayerQuit(event: PlayerDisconnectEvent) {
25+
CoroutineScope(Dispatchers.Default).launch {
26+
playerSynchronizer.updatePlayerCount()
27+
}
28+
}
29+
30+
}

platform/paper/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
33
dependencies {
44
implementation(rootProject.libs.kotlin.stdlib)
55
compileOnly(rootProject.libs.paper.api)
6+
implementation(project(":platform:shared"))
67
}
78

89
sourceSets.main {

platform/shared/build.gradle.kts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
dependencies {
2+
compileOnly(rootProject.libs.kotlin.stdlib)
3+
compileOnly(rootProject.libs.kotlin.reflect)
4+
compileOnly(rootProject.libs.kotlin.coroutines)
5+
compileOnly(rootProject.libs.simplecloud.controller) {
6+
exclude(group = "org.jetbrains.kotlin")
7+
exclude(group = "org.jetbrains.kotlinx")
8+
}
9+
compileOnly(rootProject.libs.simplecloud.player) {
10+
exclude(group = "net.kyori")
11+
exclude(group = "org.jetbrains.kotlin")
12+
exclude(group = "org.jetbrains.kotlinx")
13+
}
14+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package app.simplecloud.api.platform.shared
2+
3+
import app.simplecloud.controller.api.ControllerApi
4+
import kotlinx.coroutines.*
5+
import kotlinx.coroutines.flow.*
6+
import kotlin.time.Duration.Companion.seconds
7+
8+
class PlayerSynchronizer(
9+
private val controllerApi: ControllerApi.Coroutine,
10+
private val getCurrentOnlineCount: () -> Long,
11+
) {
12+
13+
private val currentServerId = System.getenv("SIMPLECLOUD_UNIQUE_ID")
14+
15+
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
16+
private val updateFlow = MutableSharedFlow<Unit>()
17+
18+
@OptIn(FlowPreview::class)
19+
fun start() {
20+
scope.launch {
21+
controllerApi.getServers().updateServerProperty(currentServerId, "player-count-ping", "skip")
22+
}
23+
24+
scope.launch {
25+
updateFlow
26+
.debounce(800)
27+
.collect {
28+
val currentServer = controllerApi.getServers().getCurrentServer().copy(
29+
playerCount = getCurrentOnlineCount()
30+
)
31+
controllerApi.getServers().updateServer(currentServer)
32+
println("Updated server player count to ${currentServer.playerCount}")
33+
}
34+
}
35+
36+
scope.launch {
37+
while (isActive) {
38+
updatePlayerCount()
39+
delay(15.seconds)
40+
}
41+
}
42+
}
43+
44+
suspend fun updatePlayerCount() {
45+
updateFlow.emit(Unit)
46+
}
47+
48+
fun stop() {
49+
scope.cancel()
50+
}
51+
52+
}

platform/spigot/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ plugins {
77
dependencies {
88
compileOnly(rootProject.libs.kotlin.stdlib)
99
compileOnly(rootProject.libs.kotlin.reflect)
10+
compileOnly(rootProject.libs.kotlin.coroutines)
11+
api(project(":platform:shared"))
1012
compileOnly(rootProject.libs.paper.api)
1113
api(rootProject.libs.simplecloud.controller) {
1214
exclude(group = "org.jetbrains.kotlin")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package app.simplecloud.api.platform.spigot
2+
3+
import app.simplecloud.api.platform.shared.PlayerSynchronizer
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.launch
7+
import org.bukkit.event.EventHandler
8+
import org.bukkit.event.Listener
9+
import org.bukkit.event.player.PlayerJoinEvent
10+
import org.bukkit.event.player.PlayerQuitEvent
11+
12+
class PlayerConnectionListener(
13+
private val playerSynchronizer: PlayerSynchronizer
14+
) : Listener {
15+
16+
@EventHandler
17+
fun onPlayerJoin(event: PlayerJoinEvent) {
18+
CoroutineScope(Dispatchers.Default).launch {
19+
playerSynchronizer.updatePlayerCount()
20+
}
21+
}
22+
23+
@EventHandler
24+
fun onPlayerQuit(event: PlayerQuitEvent) {
25+
CoroutineScope(Dispatchers.Default).launch {
26+
playerSynchronizer.updatePlayerCount()
27+
}
28+
}
29+
30+
}

0 commit comments

Comments
 (0)