aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2
diff options
context:
space:
mode:
authorhannibal2 <24389977+hannibal002@users.noreply.github.com>2024-05-04 03:29:34 +0200
committerGitHub <noreply@github.com>2024-05-04 03:29:34 +0200
commite52d0de329e07ad168bda60f9386fa9e96f38ea7 (patch)
treefb49e4e79ab65ca710a146c11d9c7acdec6a76ac /src/main/java/at/hannibal2
parent5dd1ed897c27938eafbdf9e08d1be360184fe686 (diff)
downloadskyhanni-e52d0de329e07ad168bda60f9386fa9e96f38ea7.tar.gz
skyhanni-e52d0de329e07ad168bda60f9386fa9e96f38ea7.tar.bz2
skyhanni-e52d0de329e07ad168bda60f9386fa9e96f38ea7.zip
Fix: chat click and hover (#1680)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java/at/hannibal2')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt47
1 files changed, 44 insertions, 3 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt
index 2fbb9630e..e2a4f779c 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatModifier.kt
@@ -4,8 +4,10 @@ import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.data.hypixel.chat.event.SystemMessageEvent
import at.hannibal2.skyhanni.features.misc.MarkedPlayerManager
-import at.hannibal2.skyhanni.utils.StringUtils
+import net.minecraft.event.ClickEvent
+import net.minecraft.event.HoverEvent
import net.minecraft.util.ChatComponentText
+import net.minecraft.util.IChatComponent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
class PlayerChatModifier {
@@ -20,9 +22,48 @@ class PlayerChatModifier {
@SubscribeEvent
fun onChat(event: SystemMessageEvent) {
- val newMessage = cutMessage(event.chatComponent.formattedText)
+ val original = event.chatComponent.formattedText
+ val new = cutMessage(original)
+ if (new == original) return
- event.chatComponent = StringUtils.replaceIfNeeded(event.chatComponent, ChatComponentText(newMessage)) ?: return
+ val clickEvents = mutableListOf<ClickEvent>()
+ val hoverEvents = mutableListOf<HoverEvent>()
+ findClickableTexts(event.chatComponent, clickEvents)
+ findHoverTexts(event.chatComponent, hoverEvents)
+ val clickSize = clickEvents.size
+ val hoverSize = hoverEvents.size
+
+ // do not change the message if more than one hover or click is found
+ if (clickSize > 1 || hoverSize > 1) return
+
+ val text = ChatComponentText(new)
+ if (clickSize == 1) {
+ text.chatStyle.chatClickEvent = clickEvents.first()
+ }
+ if (hoverSize == 1) {
+ text.chatStyle.chatHoverEvent = hoverEvents.first()
+ }
+ event.chatComponent = text
+ }
+
+ private fun findClickableTexts(chatComponent: IChatComponent, clickEvents: MutableList<ClickEvent>) {
+ for (sibling in chatComponent.siblings) {
+ findClickableTexts(sibling, clickEvents)
+ }
+ val clickEvent = chatComponent.chatStyle.chatClickEvent ?: return
+ clickEvent.action ?: return
+ if (clickEvents.any { it.value == clickEvent.value }) return
+ clickEvents.add(clickEvent)
+ }
+
+ private fun findHoverTexts(chatComponent: IChatComponent, hoverEvents: MutableList<HoverEvent>) {
+ for (sibling in chatComponent.siblings) {
+ findHoverTexts(sibling, hoverEvents)
+ }
+ val hoverEvent = chatComponent.chatStyle.chatHoverEvent ?: return
+ hoverEvent.action ?: return
+ if (hoverEvents.any { it.value == hoverEvent.value }) return
+ hoverEvents.add(hoverEvent)
}
private fun cutMessage(input: String): String {