Skip to content

Commit

Permalink
Fix: Own player messages not being rewritten by chat formatting (#2806)
Browse files Browse the repository at this point in the history
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
  • Loading branch information
nea89o and hannibal002 authored Oct 26, 2024
1 parent 31d2b53 commit ceba80c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ object PlayerChatManager {
/**
* REGEX-TEST: [58] §7nea89o§7: haiiiii
* REGEX-TEST: [266] ♫ §b[MVP§d+§b] lrg89§f: a
* REGEX-TEST: [302] ♫ [MVP+] lrg89: problematic
*/
private val globalPattern by patternGroup.pattern(
"global",
"(?:\\[(?<level>\\d+)] )?(?<author>.+)(?<chatColor>§f|§7): (?<message>.*)"
"(?:\\[(?<level>\\d+)] )?(?<author>.+?)(?<chatColor>§f|§7|): (?<message>.*)"
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ import at.hannibal2.skyhanni.features.misc.MarkedPlayerManager
import at.hannibal2.skyhanni.features.misc.compacttablist.AdvancedPlayerList
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils.changeColor
import at.hannibal2.skyhanni.utils.ComponentMatcherUtils.intoSpan
import at.hannibal2.skyhanni.utils.ComponentMatcherUtils.matchStyledMatcher
import at.hannibal2.skyhanni.utils.ComponentSpan
import at.hannibal2.skyhanni.utils.LorenzColor
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.StringUtils
import at.hannibal2.skyhanni.utils.StringUtils.applyFormattingFrom
import at.hannibal2.skyhanni.utils.StringUtils.cleanPlayerName
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.StringUtils.toCleanChatComponent
import at.hannibal2.skyhanni.utils.chat.Text
import at.hannibal2.skyhanni.utils.chat.Text.style
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import com.google.gson.JsonArray
import com.google.gson.JsonNull
import net.minecraft.util.ChatComponentText
import net.minecraft.util.EnumChatFormatting
import net.minecraft.util.IChatComponent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

Expand All @@ -51,7 +55,7 @@ object PlayerNameFormatter {
*/
private val emblemPattern by patternGroup.pattern(
"emblem",
"(?<emblem>(?:§.){0,2}.) (?<author>.*)"
"(?<emblem>(?:§.){0,2}.) (?<author>.*)",
)

@SubscribeEvent
Expand All @@ -72,7 +76,7 @@ object PlayerNameFormatter {
levelColor?.toString(),
level = levelComponent,
privateIslandRank = privateIslandRank,
privateIslandGuest = privateIslandGuest
privateIslandGuest = privateIslandGuest,
)
val all = ChatComponentText("")
all.appendSibling(name)
Expand All @@ -91,7 +95,7 @@ object PlayerNameFormatter {
appendSibling(nameFormat(event.authorComponent))
appendText("§f: ")
appendSibling(event.messageComponent.intoComponent())
}
},
) ?: return
}

Expand All @@ -104,7 +108,7 @@ object PlayerNameFormatter {
appendSibling(nameFormat(event.authorComponent, guildRank = event.guildRank))
appendText("§f: ")
appendSibling(event.messageComponent.intoComponent())
}
},
) ?: return
}

Expand All @@ -117,23 +121,22 @@ object PlayerNameFormatter {
appendSibling(nameFormat(event.authorComponent))
appendText("§f: ")
appendSibling(event.messageComponent.intoComponent())
}
},
) ?: return
}

@SubscribeEvent
fun onPrivateChat(event: PrivateMessageChatEvent) {
if (!isEnabled()) return
event.chatComponent =
StringUtils.replaceIfNeeded(
event.chatComponent,
Text.text("§d${event.direction}") {
appendText(" ")
appendSibling(nameFormat(event.authorComponent))
appendText("§f: ")
appendSibling(event.messageComponent.intoComponent())
}
) ?: return
event.chatComponent = StringUtils.replaceIfNeeded(
event.chatComponent,
Text.text("§d${event.direction}") {
appendText(" ")
appendSibling(nameFormat(event.authorComponent))
appendText("§f: ")
appendSibling(event.messageComponent.intoComponent())
},
) ?: return
}

@SubscribeEvent
Expand All @@ -146,16 +149,16 @@ object PlayerNameFormatter {
nameFormat(
event.authorComponent,
levelColor = event.levelComponent?.sampleStyleAtStart()?.color?.toString(),
level = event.levelComponent
)
level = event.levelComponent,
),
)

appendText(" ")
appendSibling(event.action.intoComponent().changeColor(LorenzColor.GRAY))

appendText(" ")
appendSibling(event.item.intoComponent())
}
},
) ?: return
}

Expand All @@ -175,7 +178,7 @@ object PlayerNameFormatter {
cleanAuthor = groupOrThrow("author").stripHypixelMessage()
}

val name = formatAuthor(cleanAuthor.getText(), levelColor).applyFormattingFrom(cleanAuthor)
val name = formatAuthor(cleanAuthor, levelColor)
val levelFormat = formatLevel(levelColor, level)
val guildRankFormat = guildRank?.intoComponent()
val privateIslandRankFormat = privateIslandRank?.intoComponent()
Expand All @@ -192,7 +195,7 @@ object PlayerNameFormatter {
val map = mutableMapOf<PlayerMessagesConfig.MessagePart, IChatComponent?>()
map[PlayerMessagesConfig.MessagePart.SKYBLOCK_LEVEL] = levelFormat
map[PlayerMessagesConfig.MessagePart.EMBLEM] = emblemFormat
map[PlayerMessagesConfig.MessagePart.PLAYER_NAME] = name
map[PlayerMessagesConfig.MessagePart.PLAYER_NAME] = name.intoComponent()
map[PlayerMessagesConfig.MessagePart.CRIMSON_FACTION] = faction
map[PlayerMessagesConfig.MessagePart.MODE_IRONMAN] = ironman
map[PlayerMessagesConfig.MessagePart.BINGO_LEVEL] = bingo
Expand Down Expand Up @@ -229,19 +232,41 @@ object PlayerNameFormatter {
return author.stripHypixelMessage().removePrefix("§f")
}

private fun formatAuthor(author: String, levelColor: String?): String {
if (author.contains("ADMIN")) return author
if (config.ignoreYouTube && author.contains("YOUTUBE")) return author
private fun ComponentSpan.splitPlayerNameAndExtras(): Pair<ComponentSpan?, ComponentSpan> {
val space = getText().indexOf(' ')
if (space < 0) return Pair(null, this)
return Pair(slice(0, space + 1), slice(space + 1))
}

var result = author.cleanPlayerName(displayName = true)
levelColor?.let {
if (config.useLevelColorForName) {
val cleanPlayerName = author.cleanPlayerName()
result = result.replace(cleanPlayerName, it + cleanPlayerName)
}
}
private fun formatAuthor(author: ComponentSpan, levelColor: String?): ComponentSpan {
if (author.getText().contains("ADMIN")) return author
if (config.ignoreYouTube && author.getText().contains("YOUTUBE")) return author
val (rank, name) = author.splitPlayerNameAndExtras()
val coloredName = createColoredName(name, levelColor, name.getText().removeColor())
return if (config.playerRankHider || rank == null) coloredName else rank + coloredName
}

private fun createColoredName(
name: ComponentSpan,
levelColor: String?,
removeColor: String,
): ComponentSpan = when {
MarkedPlayerManager.isMarkedPlayer(removeColor) && MarkedPlayerManager.config.highlightInChat ->
ChatComponentText(MarkedPlayerManager.replaceInChat(removeColor))
.setChatStyle(name.sampleStyleAtStart()).intoSpan()

levelColor != null && config.useLevelColorForName ->
ChatComponentText(levelColor + removeColor)
.setChatStyle(name.sampleStyleAtStart())
.intoSpan()

config.playerRankHider ->
ChatComponentText(removeColor)
.setChatStyle(name.sampleStyleAtStart()?.createShallowCopy())
.style { color = EnumChatFormatting.AQUA }
.intoSpan()

return MarkedPlayerManager.replaceInChat(result)
else -> name
}

fun isEnabled() = LorenzUtils.inSkyBlock && config.enable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class ComponentSpan internal constructor(
*/
fun slice(start: Int = 0, end: Int = length): ComponentSpan {
require(0 <= start) { "start is bigger than 0: start=$start, cachedText=$cachedText" }
require(start <= end) { "start is bigger than length: start=$start, length=$length, cachedText=$cachedText" }
require(start <= end) { "start is bigger than end: start=$start, end=$end, cachedText=$cachedText" }
require(end <= length) { "end is bigger than length: end=$end, length=$length, cachedText=$cachedText" }
return ComponentSpan(textComponent, cachedText, this.start + start, this.start + end)
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ object StringUtils {

fun String.toCleanChatComponent(): IChatComponent = ChatComponentText(this)

@Deprecated("This function strips internal formatting changes like the color of the pluses of the MVP+ rank")
fun IChatComponent.cleanPlayerName(displayName: Boolean = false): IChatComponent =
formattedText.cleanPlayerName(displayName).applyFormattingFrom(this)

Expand Down

0 comments on commit ceba80c

Please sign in to comment.