Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 5918860

Browse files
committed
feat: introduce AfkStateChangeEvent for AFK state tracking
- Added `AfkStateChangeEvent` to track player AFK state changes. - Updated player implementation to use `AtomicBoolean` for thread-safe AFK state management. - Integrated `AfkStateChangeEvent` posting in AFK state update logic across client and standalone modules.
1 parent b6dfb02 commit 5918860

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package dev.slne.surf.cloud.api.common.event.player.afk
2+
3+
import dev.slne.surf.cloud.api.common.event.player.CloudPlayerEvent
4+
import dev.slne.surf.cloud.api.common.player.CloudPlayer
5+
6+
class AfkStateChangeEvent(val isAfk: Boolean, source: Any, player: CloudPlayer) :
7+
CloudPlayerEvent(source, player)

surf-cloud-core/surf-cloud-core-client/src/main/kotlin/dev/slne/surf/cloud/core/client/netty/network/ClientRunningPacketListenerImpl.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dev.slne.surf.cloud.core.client.netty.network
33
import com.google.common.flogger.StackSize
44
import dev.slne.surf.cloud.api.common.event.offlineplayer.punishment.CloudPlayerPunishEvent
55
import dev.slne.surf.cloud.api.common.event.offlineplayer.punishment.CloudPlayerPunishmentUpdatedEvent
6+
import dev.slne.surf.cloud.api.common.event.player.afk.AfkStateChangeEvent
67
import dev.slne.surf.cloud.api.common.netty.network.ConnectionProtocol
78
import dev.slne.surf.cloud.api.common.netty.network.protocol.respond
89
import dev.slne.surf.cloud.api.common.netty.packet.NettyPacket
@@ -274,6 +275,7 @@ class ClientRunningPacketListenerImpl(
274275
playerManagerImpl.getPlayer(packet.uuid)?.let { player ->
275276
require(player is ClientCloudPlayerImpl<*>) { "Player $player is not a client player" }
276277
player.afk = packet.isAfk
278+
AfkStateChangeEvent(packet.isAfk, this, player).postAndForget()
277279
}
278280
}
279281

surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/player/StandaloneCloudPlayerImpl.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.slne.surf.cloud.standalone.player
22

3+
import dev.slne.surf.cloud.api.common.event.player.afk.AfkStateChangeEvent
34
import dev.slne.surf.cloud.api.common.netty.network.protocol.await
45
import dev.slne.surf.cloud.api.common.netty.network.protocol.awaitOrThrow
56
import dev.slne.surf.cloud.api.common.netty.packet.NettyPacket
@@ -51,6 +52,7 @@ import java.time.ZonedDateTime
5152
import java.time.temporal.ChronoUnit
5253
import java.util.*
5354
import java.util.concurrent.TimeUnit
55+
import java.util.concurrent.atomic.AtomicBoolean
5456
import kotlin.time.Duration
5557
import kotlin.time.Duration.Companion.seconds
5658

@@ -88,8 +90,7 @@ class StandaloneCloudPlayerImpl(uuid: UUID, name: String, val ip: Inet4Address)
8890
private val ppdcMutex = Mutex()
8991
private var firstSeenCache: ZonedDateTime? = null
9092

91-
var afk = false
92-
private set
93+
private val afk = AtomicBoolean(false)
9394

9495
var sessionStartTime: ZonedDateTime = ZonedDateTime.now()
9596

@@ -105,7 +106,7 @@ class StandaloneCloudPlayerImpl(uuid: UUID, name: String, val ip: Inet4Address)
105106
}
106107

107108
override fun isAfk(): Boolean {
108-
return afk
109+
return afk.get()
109110
}
110111

111112
override suspend fun currentSessionDuration(): Duration {
@@ -114,15 +115,15 @@ class StandaloneCloudPlayerImpl(uuid: UUID, name: String, val ip: Inet4Address)
114115
}
115116

116117
fun updateAfkStatus(newValue: Boolean) {
117-
if (newValue == afk) return
118-
afk = newValue
118+
if (!afk.compareAndSet(!newValue, newValue)) return
119119

120-
nettyServer.connection.broadcast(UpdateAFKStatePacket(uuid, afk))
120+
nettyServer.connection.broadcast(UpdateAFKStatePacket(uuid, newValue))
121+
AfkStateChangeEvent(newValue, this, this).postAndForget()
121122

122123
sendText {
123124
appendPrefix()
124125
info("Du bist nun ")
125-
if (afk) {
126+
if (newValue) {
126127
info("AFK und erhältst keine weiteren Paychecks.")
127128
} else {
128129
info("nicht mehr AFK.")

0 commit comments

Comments
 (0)