Skip to content

Commit

Permalink
fix: tweak afk handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy0000 committed Dec 11, 2023
1 parent a351ad8 commit 25d0b85
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mineinabyss.extracommands

import com.mineinabyss.extracommands.listeners.AfkListener
import com.mineinabyss.idofront.di.DI
import com.mineinabyss.idofront.plugin.listeners
import org.bukkit.plugin.java.JavaPlugin

class ExtraCommands : JavaPlugin() {
Expand All @@ -9,6 +11,10 @@ class ExtraCommands : JavaPlugin() {
createExtraCommandsContext()
ExtraCommandExecutor()

listeners(
AfkListener()
)

ExtraPlaceholders().register()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mineinabyss.extracommands

import com.mineinabyss.extracommands.commands.AfkPlayer
import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.extracommands.commands.afkPlayers
import me.clip.placeholderapi.expansion.PlaceholderExpansion
import org.bukkit.entity.Player
import kotlin.time.toKotlinDuration
Expand All @@ -16,7 +15,7 @@ class ExtraPlaceholders : PlaceholderExpansion() {

override fun onPlaceholderRequest(player: Player, identifier: String) =
when (identifier) {
"afk" -> extraCommands.config.afk.takeIf { afk -> player.idleDuration.toKotlinDuration() >= afk.idleTime || player.toGeary().has<AfkPlayer>() }?.text ?: ""
"afk" -> extraCommands.config.afk.takeIf { afk -> player.idleDuration.toKotlinDuration() >= afk.idleTime || player.uniqueId in afkPlayers }?.text ?: ""
else -> null
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.mineinabyss.extracommands.commands

import com.mineinabyss.geary.papermc.tracking.entities.toGeary
import com.mineinabyss.idofront.commands.arguments.enumArg
import com.mineinabyss.idofront.commands.entrypoint.CommandDSLEntrypoint
import com.mineinabyss.idofront.commands.extensions.actions.playerAction
import com.mineinabyss.idofront.messaging.info
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.bukkit.GameMode
import org.bukkit.entity.Player
import java.util.*

@Serializable @SerialName("extracommands:afk") class AfkPlayer

fun Player.toggleAfk() = toGeary().let {
if (it.has<AfkPlayer>()) it.remove<AfkPlayer>()
else it.set(AfkPlayer())
it.has<AfkPlayer>()
val afkPlayers = mutableSetOf<UUID>()
fun Player.toggleAfk() = when (uniqueId in afkPlayers) {
true -> {
afkPlayers -= uniqueId
false
}
false -> {
afkPlayers += uniqueId
true
}
}

fun CommandDSLEntrypoint.afkCommand() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.mineinabyss.extracommands.listeners

import com.mineinabyss.extracommands.commands.afkPlayers
import com.mineinabyss.idofront.messaging.info
import io.papermc.paper.event.player.AsyncChatEvent
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerMoveEvent
import org.bukkit.event.player.PlayerQuitEvent

class AfkListener : Listener {

@EventHandler
fun PlayerMoveEvent.onMove() {
if (hasChangedPosition() && afkPlayers.remove(player.uniqueId))
player.info("<gray>You are no longer AFK")
}

@EventHandler
fun PlayerQuitEvent.onQuit() {
afkPlayers.remove(player.uniqueId)
}

@EventHandler
fun AsyncChatEvent.onChat() {
if (afkPlayers.remove(player.uniqueId))
player.info("<gray>You are no longer AFK")
}
}

0 comments on commit 25d0b85

Please sign in to comment.