Clean up PlayerUtils (before PlayerAffix) and add support for configurable message colors using LuckPerms

This commit is contained in:
SKBotNL committed 2026-05-07 00:04:50 +02:00
1 parent d687c1e619
commit 22dd63f456
7 files changed
+35 -77

No files matched your search

+2 -2
View File
@@ -19,8 +19,8 @@ import nl.skbotnl.chatog.ChatOG.Companion.discordBridgeLock
import nl.skbotnl.chatog.ChatOG.Companion.scope
import nl.skbotnl.chatog.util.ChatUtil
import nl.skbotnl.chatog.util.ChatUtil.legacyToMm
import nl.skbotnl.chatog.util.PlayerAffix
import nl.skbotnl.chatog.util.PlayerExtensions.chatSystem
import nl.skbotnl.chatog.util.PlayerUtils
import org.bukkit.Bukkit
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
@@ -188,7 +188,7 @@ internal class Events : Listener {
return
}
var nameString = "${PlayerAffix.getPrefix(event.player.uniqueId)}${event.player.name}"
var nameString = "${PlayerUtils.getPrefix(event.player.uniqueId)}${event.player.name}"
val unionColorTag = ChatUtil.fetchUnionColorTag(event.player)
val unionPlain = ChatUtil.stripFormatting(unionColorTag)
@@ -9,7 +9,7 @@ import net.kyori.adventure.text.event.HoverEvent
import net.trueog.utilitiesog.UtilitiesOG
import nl.skbotnl.chatog.translation.command.TranslateMessage
import nl.skbotnl.chatog.util.ChatUtil
import nl.skbotnl.chatog.util.PlayerAffix
import nl.skbotnl.chatog.util.PlayerUtils
import org.bukkit.entity.Player
internal abstract class ChatSystem {
@@ -28,11 +28,12 @@ internal abstract class ChatSystem {
sendDiscordMessage(text, discordPlayerPartString, player.uniqueId)
val messageComponent = ChatUtil.processText(text, player) ?: return
val messageComponent =
ChatUtil.processText(text, player)?.color(PlayerUtils.getMessageColor(player.uniqueId)) ?: return
val chatComponent =
UtilitiesOG.trueogColorize(
ChatUtil.legacyToMm("$playerPartString<reset>${PlayerAffix.getSuffix(player.uniqueId)} &7> ")
ChatUtil.legacyToMm("$playerPartString<reset>${PlayerUtils.getSuffix(player.uniqueId)} &7> ")
)
var textComponent = Component.join(JoinConfiguration.noSeparators(), chatComponent, messageComponent)
@@ -1,7 +1,6 @@
package nl.skbotnl.chatog.config.discord
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonSetter
import com.fasterxml.jackson.annotation.Nulls
@@ -10,7 +10,7 @@ import nl.skbotnl.chatog.ChatOG.Companion.languageDatabase
import nl.skbotnl.chatog.ChatOG.Companion.translator
import nl.skbotnl.chatog.util.ChatUtil
import nl.skbotnl.chatog.util.ChatUtil.legacyToMm
import nl.skbotnl.chatog.util.PlayerAffix
import nl.skbotnl.chatog.util.PlayerUtils
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandExecutor
@@ -137,9 +137,10 @@ internal class TranslateMessage : CommandExecutor {
when (messageType) {
1 -> {
val sentChatMessage = sentMessage as SentChatMessage
// TODO: Should use ChatUtil.getPlayerPartString
var playerString =
"${PlayerAffix.getPrefix(sentChatMessage.player.uniqueId)}${sentChatMessage.player.name}${
PlayerAffix.getSuffix(sentChatMessage.player.uniqueId)
"${PlayerUtils.getPrefix(sentChatMessage.player.uniqueId)}${sentChatMessage.player.name}${
PlayerUtils.getSuffix(sentChatMessage.player.uniqueId)
}"
val unionColorTag = ChatUtil.fetchUnionColorTag(sentMessage.player)
val unionPlain = ChatUtil.stripFormatting(unionColorTag)
@@ -29,7 +29,7 @@ import org.bukkit.entity.Player
internal object ChatUtil {
fun getPlayerPartString(player: Player, includeSuffix: Boolean = false): String {
var playerPart =
"${PlayerAffix.getPrefix(player.uniqueId)}${player.name}${if (includeSuffix) PlayerAffix.getSuffix(player.uniqueId) else ""}"
"${PlayerUtils.getPrefix(player.uniqueId)}${player.name}${if (includeSuffix) PlayerUtils.getSuffix(player.uniqueId) else ""}"
val unionColorTag = fetchUnionColorTag(player)
val unionPlain = stripFormatting(unionColorTag)
@@ -1,67 +0,0 @@
package nl.skbotnl.chatog.util
import java.util.*
import nl.skbotnl.chatog.ChatOG.Companion.luckPerms
import nl.skbotnl.chatog.util.ChatUtil.legacyToMm
internal object PlayerAffix {
fun getPrefix(uuid: UUID): String {
val raw = getMetaData(uuid).prefix ?: return ""
val cleaned = stripOuterSpaces(legacyToMm(raw))
if (ChatUtil.stripFormatting(cleaned).isEmpty()) return ""
return "$cleaned "
}
fun getSuffix(uuid: UUID): String {
val raw = getMetaData(uuid).suffix ?: return ""
val cleaned = stripOuterSpaces(legacyToMm(raw))
if (ChatUtil.stripFormatting(cleaned).isEmpty()) return ""
return " $cleaned"
}
private fun stripOuterSpaces(mm: String): String = stripTrailingSpaces(stripLeadingSpaces(mm))
private fun stripLeadingSpaces(mm: String): String {
val keptTags = StringBuilder()
var i = 0
while (i < mm.length) {
val c = mm[i]
when {
c.isWhitespace() -> i++
c == '<' -> {
val close = mm.indexOf('>', i)
if (close < 0) break
keptTags.append(mm, i, close + 1)
i = close + 1
}
else -> return keptTags.toString() + mm.substring(i)
}
}
return keptTags.toString()
}
private fun stripTrailingSpaces(mm: String): String {
val keptTags = StringBuilder()
var end = mm.length
while (end > 0) {
val c = mm[end - 1]
when {
c.isWhitespace() -> end--
c == '>' -> {
val open = mm.lastIndexOf('<', end - 1)
if (open < 0) break
keptTags.insert(0, mm.substring(open, end))
end = open
}
else -> return mm.substring(0, end) + keptTags.toString()
}
}
return keptTags.toString()
}
private fun getMetaData(uuid: UUID) =
with(luckPerms.userManager.getUser(uuid) ?: luckPerms.userManager.loadUser(uuid).join()) {
val queryOptions = luckPerms.contextManager.getQueryOptions(this).orElse(queryOptions)
cachedData.getMetaData(queryOptions)
}
}
@@ -0,0 +1,24 @@
package nl.skbotnl.chatog.util
import java.util.*
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.TextColor
import nl.skbotnl.chatog.ChatOG.Companion.luckPerms
internal object PlayerUtils {
fun getPrefix(uuid: UUID): String {
val prefix = getMetaData(uuid).prefix?.trim() ?: return ""
return if (prefix.endsWith(' ')) prefix else "$prefix "
}
fun getSuffix(uuid: UUID) = getMetaData(uuid).suffix?.trim() ?: ""
fun getMessageColor(uuid: UUID): TextColor {
return getMetaData(uuid).getMetaValue("chat-og.messageColor")?.lowercase()?.let {
NamedTextColor.NAMES.value(it)
} ?: NamedTextColor.GRAY
}
private fun getMetaData(uuid: UUID) =
with(luckPerms.userManager.getUser(uuid) ?: luckPerms.userManager.loadUser(uuid).join()) { cachedData.metaData }
}