diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-10-12 14:11:16 +0200 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-10-12 14:11:16 +0200 |
commit | e8025b1206fc9bc432d86753ffdf4525c5c04f38 (patch) | |
tree | 9d0c6f55904a4c5f51a5c3bd20f4bb82ca230515 /src/main/java/at/hannibal2/skyhanni | |
parent | 006d59f7c0613dd282e88ba41a21c0f74e1d1ae9 (diff) | |
download | skyhanni-e8025b1206fc9bc432d86753ffdf4525c5c04f38.tar.gz skyhanni-e8025b1206fc9bc432d86753ffdf4525c5c04f38.tar.bz2 skyhanni-e8025b1206fc9bc432d86753ffdf4525c5c04f38.zip |
code cleanup
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/features/misc/PlayerChatSymbols.kt | 95 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt | 4 |
2 files changed, 48 insertions, 51 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/misc/PlayerChatSymbols.kt b/src/main/java/at/hannibal2/skyhanni/features/misc/PlayerChatSymbols.kt index 2d411c5c3..904b43fa2 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/misc/PlayerChatSymbols.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/misc/PlayerChatSymbols.kt @@ -11,6 +11,7 @@ import at.hannibal2.skyhanni.utils.StringUtils.removeResets import at.hannibal2.skyhanni.utils.TabListData import net.minecraft.client.Minecraft import net.minecraft.util.ChatComponentText +import net.minecraft.util.IChatComponent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent // code inspired by SBA but heavily modified to be more functional and actually work @@ -23,61 +24,57 @@ class PlayerChatSymbols { if (!LorenzUtils.inSkyBlock) return if (!config.enabled) return - val message = event.message + val username = event.message.getPlayerName() ?: return - val username = message.getPlayerName() - if (username == "-") return + updateSymbolFromTabList(username) - val talkingPlayer = Minecraft.getMinecraft().theWorld.getPlayerEntityByName(username) + val usernameWithSymbols = nameSymbols[username] ?: return - if (talkingPlayer != null) { - nameSymbols[username] = talkingPlayer.displayName.siblings[0].unformattedText - } else { - val result = TabListData.getTabList() - .find { playerName -> TabStringType.usernameFromLine(playerName) == username } + val split = usernameWithSymbols.split("$username ") + var emblemText = if (split.size > 1) split[1] else "" + emblemText = emblemText.removeResets() - if (result != null) { - nameSymbols[username] = result - } + if (emblemText == "") return + event.chatComponent = StringUtils.replaceFirstChatText(event.chatComponent, "$emblemText ", "") + + StringUtils.modifyFirstChatComponent(event.chatComponent) { component -> + modify(component, username, emblemText) } + } - if (nameSymbols.contains(username)) { - val usernameWithSymbols = nameSymbols[username]!! - - val split = usernameWithSymbols.split("$username ") - var emblemText = if (split.size > 1) split[1] else "" - emblemText = emblemText.removeResets() - - if (emblemText != "") { - event.chatComponent = StringUtils.replaceFirstChatText(event.chatComponent, "$emblemText ", "") - - StringUtils.modifyFirstChatComponent(event.chatComponent) { component -> - if (component is ChatComponentText) { - component as AccessorChatComponentText - if ( component.text_skyhanni().contains(username)) { - val oldText = component.text_skyhanni() - - val newText = when (config.symbolLocation) { - 0 -> "$emblemText $oldText" - 1 -> { - // fixing it for when you type a message as the chat isn't split the same - if (oldText.contains("§f:")) { - val ownChatSplit = oldText.split("§f:") - if (ownChatSplit.size > 1) { - "${ownChatSplit[0]} $emblemText §f:${ownChatSplit[1]}" - } else oldText - } else "$oldText $emblemText " - } - else -> oldText - } - component.setText_skyhanni(component.text_skyhanni().replace(oldText, newText)) - return@modifyFirstChatComponent true - } - return@modifyFirstChatComponent false - } - return@modifyFirstChatComponent false - } - } + private fun updateSymbolFromTabList(username: String) { + val talkingPlayer = Minecraft.getMinecraft().theWorld.getPlayerEntityByName(username) + nameSymbols[username] = if (talkingPlayer != null) { + talkingPlayer.displayName.siblings[0].unformattedText + } else { + TabListData.getTabList() + .find { playerName -> TabStringType.usernameFromLine(playerName) == username } ?: return } } + + private fun modify(component: IChatComponent, username: String, emblemText: String): Boolean { + if (component !is ChatComponentText) return false + component as AccessorChatComponentText + if (!component.text_skyhanni().contains(username)) return false + val oldText = component.text_skyhanni() + + component.setText_skyhanni(component.text_skyhanni().replace(oldText, getNewText(emblemText, oldText))) + return true + } + + private fun getNewText(emblemText: String, oldText: String): String = when (config.symbolLocation) { + 0 -> "$emblemText $oldText" + 1 -> iconAfterName(oldText, emblemText) + + else -> oldText + } + + private fun iconAfterName(oldText: String, emblemText: String): String { + if (!oldText.contains("§f:")) return "$oldText $emblemText " + + // fixing it for when you type a message as the chat isn't split the same + val ownChatSplit = oldText.split("§f:") + if (ownChatSplit.size <= 1) return oldText + return "${ownChatSplit[0]} $emblemText §f:${ownChatSplit[1]}" + } }
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt index a26c0323f..0622ff568 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt @@ -200,8 +200,8 @@ object StringUtils { return chatComponent } - fun String.getPlayerName(): String { - if (!playerChatPattern.matcher(this).matches()) return "-" + fun String.getPlayerName(): String? { + if (!playerChatPattern.matcher(this).matches()) return null var username = this.removeColor().split(":")[0] |