Skip to content

Commit 635a17f

Browse files
feat: add SurfCoreCommand and enhance player management
- Introduce `surfCoreCommand` with subcommands for reloading configuration and clearing player caches. - Add `clearPlayers` and `invalidateServerPlayers` methods to `SurfPlayerService` for better cache management. - Update `PermissionRegistry` with `COMMAND_CORE` for the new command. - Override `equals` and `hashCode` in `SurfPlayer` for proper equality checks.
1 parent 1920993 commit 635a17f

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

surf-core-api/surf-core-api-common/src/main/kotlin/dev/slne/surf/core/api/common/player/SurfPlayer.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,17 @@ data class SurfPlayer(
1414
) {
1515
var currentServer: String? = null
1616
fun isOnline() = surfCoreApi.getOnlinePlayers().any { it.uuid == uuid }
17+
18+
override fun equals(other: Any?): Boolean {
19+
if (this === other) return true
20+
if (other !is SurfPlayer) return false
21+
22+
if (uuid != other.uuid) return false
23+
24+
return true
25+
}
26+
27+
override fun hashCode(): Int {
28+
return uuid.hashCode()
29+
}
1730
}

surf-core-backend/src/main/kotlin/dev/slne/surf/core/fallback/service/SurfPlayerServiceImpl.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class SurfPlayerServiceImpl : SurfPlayerService, Services.Fallback {
4141
)
4242

4343
override suspend fun savePlayer(player: SurfPlayer) = surfPlayerRepository.savePlayer(player)
44+
override fun clearPlayers() {
45+
globalPlayers.clear()
46+
}
47+
4448
override fun cachePlayer(player: SurfPlayer) {
4549
if (players.any { it.uuid == player.uuid }) {
4650
return
@@ -52,4 +56,8 @@ class SurfPlayerServiceImpl : SurfPlayerService, Services.Fallback {
5256
override fun invalidatePlayer(uuid: UUID) {
5357
globalPlayers.removeIf { it.uuid == uuid }
5458
}
59+
60+
override fun invalidateServerPlayers(server: String) {
61+
globalPlayers.removeIf { it.currentServer == server }
62+
}
5563
}

surf-core-core/surf-core-core-common/src/main/kotlin/dev/slne/surf/core/core/common/player/SurfPlayerService.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ interface SurfPlayerService {
2525

2626
suspend fun savePlayer(player: SurfPlayer)
2727

28+
fun clearPlayers()
2829
fun cachePlayer(player: SurfPlayer)
2930
fun invalidatePlayer(uuid: UUID)
31+
fun invalidateServerPlayers(server: String)
3032
}

surf-core-paper/src/main/kotlin/dev/slne/surf/core/paper/PaperMain.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import dev.slne.surf.core.api.common.event.SurfServerOnlineEvent
55
import dev.slne.surf.core.api.common.event.SurfServerStoppingEvent
66
import dev.slne.surf.core.core.common.database.databaseLoader
77
import dev.slne.surf.core.core.common.event.surfEventBus
8+
import dev.slne.surf.core.core.common.player.surfPlayerService
89
import dev.slne.surf.core.core.common.redis.redisApi
910
import dev.slne.surf.core.paper.command.lastSeenCommand
1011
import dev.slne.surf.core.paper.command.networkListCommand
1112
import dev.slne.surf.core.paper.command.networkTeleportCommand
13+
import dev.slne.surf.core.paper.command.surfCoreCommand
1214
import dev.slne.surf.core.paper.event.SurfServerEventListener
1315
import dev.slne.surf.core.paper.listener.ConnectionListener
1416
import dev.slne.surf.surfapi.bukkit.api.event.register
@@ -33,11 +35,14 @@ class PaperMain : SuspendingJavaPlugin() {
3335
lastSeenCommand()
3436
networkListCommand()
3537
networkTeleportCommand()
38+
surfCoreCommand()
3639
}
3740

3841
override fun onDisable() {
3942
surfEventBus.fire(SurfServerStoppingEvent(surfServerConfig.serverName))
4043

44+
surfPlayerService.invalidateServerPlayers(surfServerConfig.serverName)
45+
4146
databaseLoader.disconnect()
4247
redisApi.disconnect()
4348
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package dev.slne.surf.core.paper.command
2+
3+
import dev.jorel.commandapi.kotlindsl.anyExecutor
4+
import dev.jorel.commandapi.kotlindsl.commandTree
5+
import dev.jorel.commandapi.kotlindsl.literalArgument
6+
import dev.slne.surf.core.core.common.player.surfPlayerService
7+
import dev.slne.surf.core.paper.PaperBootstrap
8+
import dev.slne.surf.core.paper.permission.PermissionRegistry
9+
import dev.slne.surf.surfapi.core.api.messages.adventure.sendText
10+
11+
fun surfCoreCommand() = commandTree("core") {
12+
withPermission(PermissionRegistry.COMMAND_CORE)
13+
literalArgument("reload") {
14+
anyExecutor { executor, _ ->
15+
PaperBootstrap.surfServerConfigHolder.reload()
16+
17+
executor.sendText {
18+
appendPrefix()
19+
success("Die Konfiguration wurde neu geladen.")
20+
}
21+
}
22+
}
23+
24+
literalArgument("clearinternalplayercache") {
25+
anyExecutor { executor, _ ->
26+
surfPlayerService.clearPlayers()
27+
28+
executor.sendText {
29+
appendPrefix()
30+
success("Die Spieler-Caches wurden geleert.")
31+
}
32+
}
33+
}
34+
}

surf-core-paper/src/main/kotlin/dev/slne/surf/core/paper/permission/PermissionRegistry.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ object PermissionRegistry : PermissionRegistry() {
99
val COMMAND_LAST_SEEN = create("$BASE_COMMAND.lastseen")
1010
val COMMAND_NETWORK_LIST = create("$BASE_COMMAND.networklist")
1111
val COMMAND_NETWORK_TELEPORT = create("$BASE_COMMAND.networkteleport")
12+
13+
val COMMAND_CORE = create("$BASE_COMMAND.core")
1214
}

0 commit comments

Comments
 (0)