Skip to content

Commit

Permalink
Add hover message to global chats showing ranks
Browse files Browse the repository at this point in the history
  • Loading branch information
chatasma committed Jul 16, 2024
1 parent f7d1bde commit 1c73505
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package network.warzone.mars.player.listeners

import github.scarsz.discordsrv.DiscordSRV
import kotlinx.coroutines.runBlocking
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.Component.space
import net.kyori.adventure.text.Component.text
import net.kyori.adventure.text.format.NamedTextColor
Expand All @@ -13,7 +14,9 @@ import network.warzone.mars.api.socket.models.PlayerChatEvent
import network.warzone.mars.player.PlayerContext
import network.warzone.mars.player.PlayerManager
import network.warzone.mars.player.feature.LevelColorService
import network.warzone.mars.player.models.PlayerProfile
import network.warzone.mars.punishment.models.PunishmentKind
import network.warzone.mars.rank.RankFeature
import network.warzone.mars.utils.*
import org.bukkit.Bukkit
import org.bukkit.ChatColor.*
Expand Down Expand Up @@ -181,9 +184,9 @@ class ChatListener : Listener {

messageBuilder.append { getPlayerLevelAsComponent(profile) }.append(space())

if (prefix != null) messageBuilder.append { text("$prefix ") }
if (prefix != null) messageBuilder.append { getRanksHoverComponent(text("$prefix "), profile) }

messageBuilder.append { text(username, TextColor.color(teamColor.red, teamColor.green, teamColor.blue)) }
messageBuilder.append { getRanksHoverComponent(text(username, TextColor.color(teamColor.red, teamColor.green, teamColor.blue)), profile) }

if (tag != null) messageBuilder.append { text(" $GRAY[${tag.display.color()}$GRAY]") }

Expand Down Expand Up @@ -231,4 +234,16 @@ class ChatListener : Listener {
)
}
}

private fun getRanksHoverComponent(component: Component, profile: PlayerProfile) : Component {
val cachedRanks = profile.rankIds.mapNotNull { RankFeature.getCached(it) }
if (cachedRanks.isEmpty()) return component
val hoverComponent = text()
hoverComponent.append(Component.text("Ranks:", NamedTextColor.GRAY))
cachedRanks.forEach { cachedRank ->
val name = cachedRank.prefix?.color() ?: cachedRank.displayName?.color() ?: cachedRank.name
hoverComponent.appendNewline().append(text(name))
}
return component.hoverEvent(hoverComponent.build())
}
}

0 comments on commit 1c73505

Please sign in to comment.