aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/chat
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features/chat')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFilter.kt89
-rw-r--r--src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFormatter.kt52
2 files changed, 123 insertions, 18 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFilter.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFilter.kt
new file mode 100644
index 000000000..fe0cc52b7
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFilter.kt
@@ -0,0 +1,89 @@
+package at.hannibal2.skyhanni.features.chat.playerchat
+
+import at.hannibal2.skyhanni.SkyHanniMod
+import at.hannibal2.skyhanni.events.PlayerSendChatEvent
+import at.hannibal2.skyhanni.events.RepositoryReloadEvent
+import at.hannibal2.skyhanni.utils.LorenzUtils
+import at.hannibal2.skyhanni.utils.MultiFilter
+import net.minecraft.util.ChatComponentText
+import net.minecraft.util.EnumChatFormatting
+import net.minecraftforge.fml.common.eventhandler.EventPriority
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import java.util.regex.Pattern
+
+class PlayerChatFilter {
+
+ private val filters = mutableMapOf<String, MultiFilter>()
+
+ @SubscribeEvent
+ fun onRepoReload(event: RepositoryReloadEvent) {
+ filters.clear()
+ var countCategories = 0
+ var countFilters = 0
+
+ try {
+ val data = event.getConstant("PlayerChatFilter")!!
+
+ for (category in data["filters"].asJsonArray) {
+ val jsonObject = category.asJsonObject
+ val description = jsonObject["description"].asString
+ val filter = MultiFilter()
+ filter.load(jsonObject)
+ filters[description] = filter
+
+ countCategories++
+ countFilters += filter.count()
+ }
+
+ LorenzUtils.debug("Loaded $countFilters filters in $countCategories categories from repo")
+
+ } catch (e: Exception) {
+ e.printStackTrace()
+ LorenzUtils.error("error in RepositoryReloadEvent")
+ }
+ }
+
+ @SubscribeEvent(priority = EventPriority.HIGH, receiveCanceled = true)
+ fun onEvent(event: PlayerSendChatEvent) {
+ if (!SkyHanniMod.feature.chat.chatFilter) return
+ if (event.channel != PlayerMessageChannel.ALL) return
+
+ val message = event.message.lowercase()
+ for (filter in filters) {
+ filter.value.matchResult(message)?.let {
+ filter(event, it)
+ return
+ }
+ }
+ }
+
+ private fun filter(event: PlayerSendChatEvent, filter: String) {
+ val pattern = Pattern.compile("(.*)?$filter(.*)?", Pattern.CASE_INSENSITIVE)
+ val matcher = pattern.matcher(event.message)
+ matcher.matches()
+ val beginning = matcher.group(1)
+ val end = matcher.group(2)
+
+ event.chatComponents.clear()
+
+ val endSplit = end.split(" ")
+
+ for (word in beginning.split(" ")) {
+ if (word.isEmpty()) continue
+ event.chatComponents.add(coloredChat(word, EnumChatFormatting.GRAY))
+ }
+
+ event.chatComponents.add(coloredChat(filter.trim(), EnumChatFormatting.WHITE))
+
+ for (word in endSplit) {
+ if (word.isEmpty()) continue
+ event.chatComponents.add(coloredChat(word, EnumChatFormatting.GRAY))
+ }
+ }
+
+ private fun coloredChat(string: String, color: EnumChatFormatting): ChatComponentText {
+ val text = ChatComponentText(string)
+ text.chatStyle.color = color
+ return text
+ }
+} \ No newline at end of file
diff --git a/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFormatter.kt b/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFormatter.kt
index 39b8458f2..16b4698cb 100644
--- a/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFormatter.kt
+++ b/src/main/java/at/hannibal2/skyhanni/features/chat/playerchat/PlayerChatFormatter.kt
@@ -31,6 +31,9 @@ class PlayerChatFormatter {
//"§6Ⓑ" // bingo rank 4
)
+ private val patternUrl =
+ Pattern.compile("^https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$")
+
//§6[⌬57] §r§8[§r§b235§r§8] §r§6[MVP§r§c++§r§6] hannibal2§r§f: Hello World!
private val patternElitePrefix = Pattern.compile("§6\\[⌬(\\d+)] (.+)")
@@ -41,9 +44,6 @@ class PlayerChatFormatter {
//§dTo §r§b[MVP§r§3+§r§b] Skyfall55§r§7: §r§7hello :)
private var patternPrivateMessage: Pattern = Pattern.compile("(§d)+(To|From) §r(.+)§r§7: §r§7(.+)")
- private val patternUrl =
- Pattern.compile("^https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$")
-
@SubscribeEvent
fun onChatMessage(event: LorenzChatEvent) {
if (!LorenzUtils.isOnHypixel) return
@@ -53,6 +53,18 @@ class PlayerChatFormatter {
}
}
+ @SubscribeEvent(receiveCanceled = true)
+ fun onChatWithUrl(event: PlayerSendChatEvent) {
+ if (!LorenzUtils.inSkyblock) return
+
+ for (chatComponent in event.chatComponents) {
+ val text = chatComponent.unformattedText
+ if (patternUrl.matcher(text).matches()) {
+ chatComponent.chatStyle.chatClickEvent = ClickEvent(ClickEvent.Action.OPEN_URL, text)
+ }
+ }
+ }
+
private fun shouldBlock(originalMessage: String): Boolean {
if (handlePrivateMessage(originalMessage)) return true
@@ -208,7 +220,14 @@ class PlayerChatFormatter {
) {
val cleanName = grabName(formattedName, true)!!
loggerPlayerChat.log("[$channel] $cleanName: $message")
- val event = PlayerSendChatEvent(channel, cleanName, message)
+
+
+ val chatComponents = mutableListOf<ChatComponentText>()
+ for (line in message.split(" ")) {
+ chatComponents.add(ChatComponentText(line))
+ }
+
+ val event = PlayerSendChatEvent(channel, cleanName, message, chatComponents)
event.postAndCatch()
if (event.cancelledReason != "") {
@@ -229,24 +248,21 @@ class PlayerChatFormatter {
)
}
- addChat(nameText, event.message)
+ addChat(nameText, chatComponents)
}
- private fun addChat(text: ChatComponentText, message: String) {
- val fullText = ChatComponentText("")
- fullText.appendSibling(text)
- for (word in message.split(" ")) {
- fullText.appendSibling(ChatComponentText(" "))
- if (patternUrl.matcher(word).matches()) {
- val oneWord = ChatComponentText(word)
- oneWord.chatStyle.chatClickEvent = ClickEvent(ClickEvent.Action.OPEN_URL, word)
- fullText.appendSibling(oneWord)
- } else {
- fullText.appendSibling(ChatComponentText(word))
- }
+ private fun addChat(prefixComponent: ChatComponentText, chatComponents: MutableList<ChatComponentText>) {
+ val empty = ChatComponentText(" ")
+
+ val result = ChatComponentText("")
+ result.appendSibling(prefixComponent)
+
+ for (chatComponent in chatComponents) {
+ result.appendSibling(empty)
+ result.appendSibling(chatComponent)
}
- Minecraft.getMinecraft().thePlayer.addChatMessage(fullText)
+ Minecraft.getMinecraft().thePlayer.addChatMessage(result)
}
private fun getLevelFormat(name: String, level: Int, levelColor: String): String {