diff --git a/surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/event/player/afk/AfkStateChangeEvent.kt b/surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/event/player/afk/AfkStateChangeEvent.kt new file mode 100644 index 00000000..be50a9fb --- /dev/null +++ b/surf-cloud-api/surf-cloud-api-common/src/main/kotlin/dev/slne/surf/cloud/api/common/event/player/afk/AfkStateChangeEvent.kt @@ -0,0 +1,7 @@ +package dev.slne.surf.cloud.api.common.event.player.afk + +import dev.slne.surf.cloud.api.common.event.player.CloudPlayerEvent +import dev.slne.surf.cloud.api.common.player.CloudPlayer + +class AfkStateChangeEvent(val isAfk: Boolean, source: Any, player: CloudPlayer) : + CloudPlayerEvent(source, player) \ No newline at end of file diff --git a/surf-cloud-core/surf-cloud-core-client/src/main/kotlin/dev/slne/surf/cloud/core/client/netty/network/ClientRunningPacketListenerImpl.kt b/surf-cloud-core/surf-cloud-core-client/src/main/kotlin/dev/slne/surf/cloud/core/client/netty/network/ClientRunningPacketListenerImpl.kt index 46c21dba..32bc925e 100644 --- a/surf-cloud-core/surf-cloud-core-client/src/main/kotlin/dev/slne/surf/cloud/core/client/netty/network/ClientRunningPacketListenerImpl.kt +++ b/surf-cloud-core/surf-cloud-core-client/src/main/kotlin/dev/slne/surf/cloud/core/client/netty/network/ClientRunningPacketListenerImpl.kt @@ -3,6 +3,7 @@ package dev.slne.surf.cloud.core.client.netty.network import com.google.common.flogger.StackSize import dev.slne.surf.cloud.api.common.event.offlineplayer.punishment.CloudPlayerPunishEvent import dev.slne.surf.cloud.api.common.event.offlineplayer.punishment.CloudPlayerPunishmentUpdatedEvent +import dev.slne.surf.cloud.api.common.event.player.afk.AfkStateChangeEvent import dev.slne.surf.cloud.api.common.netty.network.ConnectionProtocol import dev.slne.surf.cloud.api.common.netty.network.protocol.respond import dev.slne.surf.cloud.api.common.netty.packet.NettyPacket @@ -274,6 +275,7 @@ class ClientRunningPacketListenerImpl( playerManagerImpl.getPlayer(packet.uuid)?.let { player -> require(player is ClientCloudPlayerImpl<*>) { "Player $player is not a client player" } player.afk = packet.isAfk + AfkStateChangeEvent(packet.isAfk, this, player).postAndForget() } } diff --git a/surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/player/CloudPlayerPlaytimeManager.kt b/surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/player/CloudPlayerPlaytimeManager.kt index 93064a67..495892ff 100644 --- a/surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/player/CloudPlayerPlaytimeManager.kt +++ b/surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/player/CloudPlayerPlaytimeManager.kt @@ -64,7 +64,7 @@ class CloudPlayerPlaytimeManager(private val service: CloudPlayerService) : Disp sessionsCache.put(uuid, newSession) toCreate += uuid to newSession } else { - if (player.afk) return@forEach + if (player.isAfk()) return@forEach // Just increment current session currentSession.accumulatedSeconds++ } diff --git a/surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/player/StandaloneCloudPlayerImpl.kt b/surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/player/StandaloneCloudPlayerImpl.kt index 0afa4f58..86ecc0af 100644 --- a/surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/player/StandaloneCloudPlayerImpl.kt +++ b/surf-cloud-standalone/src/main/kotlin/dev/slne/surf/cloud/standalone/player/StandaloneCloudPlayerImpl.kt @@ -1,5 +1,6 @@ package dev.slne.surf.cloud.standalone.player +import dev.slne.surf.cloud.api.common.event.player.afk.AfkStateChangeEvent import dev.slne.surf.cloud.api.common.netty.network.protocol.await import dev.slne.surf.cloud.api.common.netty.network.protocol.awaitOrThrow import dev.slne.surf.cloud.api.common.netty.packet.NettyPacket @@ -51,6 +52,7 @@ import java.time.ZonedDateTime import java.time.temporal.ChronoUnit import java.util.* import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicBoolean import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds @@ -88,8 +90,7 @@ class StandaloneCloudPlayerImpl(uuid: UUID, name: String, val ip: Inet4Address) private val ppdcMutex = Mutex() private var firstSeenCache: ZonedDateTime? = null - var afk = false - private set + private val afk = AtomicBoolean(false) var sessionStartTime: ZonedDateTime = ZonedDateTime.now() @@ -105,7 +106,7 @@ class StandaloneCloudPlayerImpl(uuid: UUID, name: String, val ip: Inet4Address) } override fun isAfk(): Boolean { - return afk + return afk.get() } override suspend fun currentSessionDuration(): Duration { @@ -114,15 +115,15 @@ class StandaloneCloudPlayerImpl(uuid: UUID, name: String, val ip: Inet4Address) } fun updateAfkStatus(newValue: Boolean) { - if (newValue == afk) return - afk = newValue + if (!afk.compareAndSet(!newValue, newValue)) return - nettyServer.connection.broadcast(UpdateAFKStatePacket(uuid, afk)) + nettyServer.connection.broadcast(UpdateAFKStatePacket(uuid, newValue)) + AfkStateChangeEvent(newValue, this, this).postAndForget() sendText { appendPrefix() info("Du bist nun ") - if (afk) { + if (newValue) { info("AFK und erhältst keine weiteren Paychecks.") } else { info("nicht mehr AFK.")