Skip to content

Commit

Permalink
Fix UI clear, add null check
Browse files Browse the repository at this point in the history
  • Loading branch information
otcathatsya committed Jun 28, 2022
1 parent 8aacf79 commit 1172826
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 61 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/at/cath/simpletabs/gui/TabScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class TabScreen(originalChatText: String?) : ChatScreen(originalChatText) {
}

private fun clearUI() {
children().removeAll { it is TabComponent }
buttons.removeIf { it is TabComponent }
children().removeIf { it is TabComponent }
}
}
123 changes: 63 additions & 60 deletions src/main/kotlin/at/cath/simpletabs/tabs/TabMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,74 +94,77 @@ class TabMenu(var client: MinecraftClient, serialized: String? = null) : ChatHud

if (msgIndices != null) {
val (indexText, indexVisible) = msgIndices
val tab = getSelectedTab()!!
val tab = getSelectedTab()

// don't translate when tab is not set to
if (tab.language.targetLanguage.isEmpty()) {
return
}
if (tab != null) {
// don't translate when tab is not set to
if (tab.language.targetLanguage.isEmpty()) {
return
}

val incoming = localMessageHistory[indexText].text
val incoming = localMessageHistory[indexText].text

// don't translate if already translated
if (incoming is TranslatableText && incoming.key == "chat.simpletabs.translated") {
return
}
// don't translate if already translated
if (incoming is TranslatableText && incoming.key == "chat.simpletabs.translated") {
return
}

CoroutineScope(Dispatchers.IO).launch {
val response = RetrofitDeepl.api.getTranslation(incoming.string, tab.language.targetLanguage)
if (response.isSuccessful) {
val translation = LiteralText(response.body()!!.translations[0].translatedText)
val formattedTranslation =
TranslatableText(
"chat.simpletabs.translated",
tab.language.targetLanguage.uppercase(),
translation
)
.formatted(Formatting.LIGHT_PURPLE)

val chatWidth = MathHelper.floor(this@TabMenu.width.toDouble() / this@TabMenu.chatScale)

val lineBreakTranslation = Texts.wrapLines(
formattedTranslation,
chatWidth,
this@TabMenu.client.textRenderer,
false,
true
)

val lineBreakIncoming = Texts.wrapLines(
incoming,
chatWidth,
this@TabMenu.client.textRenderer,
false,
true
)

for ((idx, msgIdx) in (indexVisible downTo (indexVisible - lineBreakTranslation.size + 1).coerceAtMost(
indexVisible - lineBreakIncoming.size + 1
)).withIndex()) {
if (msgIdx < 0) {
visibleMessages.add(
0,
ChatHudLine(client.inGameHud.ticks, lineBreakTranslation[idx], 0)
CoroutineScope(Dispatchers.IO).launch {
val response = RetrofitDeepl.api.getTranslation(incoming.string, tab.language.targetLanguage)
if (response.isSuccessful) {
val translation = LiteralText(response.body()!!.translations[0].translatedText)
val formattedTranslation =
TranslatableText(
"chat.simpletabs.translated",
tab.language.targetLanguage.uppercase(),
translation
)
.formatted(Formatting.LIGHT_PURPLE)

} else if (idx >= lineBreakTranslation.size) {
visibleMessages.removeAt(msgIdx)
} else if (idx < lineBreakIncoming.size) {
visibleMessages[msgIdx] = ChatHudLine(client.inGameHud.ticks, lineBreakTranslation[idx], 0)
} else {
visibleMessages.add(
msgIdx + 1,
ChatHudLine(client.inGameHud.ticks, lineBreakTranslation[idx], 0)
)
val chatWidth = MathHelper.floor(this@TabMenu.width.toDouble() / this@TabMenu.chatScale)

val lineBreakTranslation = Texts.wrapLines(
formattedTranslation,
chatWidth,
this@TabMenu.client.textRenderer,
false,
true
)

val lineBreakIncoming = Texts.wrapLines(
incoming,
chatWidth,
this@TabMenu.client.textRenderer,
false,
true
)

for ((idx, msgIdx) in (indexVisible downTo (indexVisible - lineBreakTranslation.size + 1).coerceAtMost(
indexVisible - lineBreakIncoming.size + 1
)).withIndex()) {
if (msgIdx < 0) {
visibleMessages.add(
0,
ChatHudLine(client.inGameHud.ticks, lineBreakTranslation[idx], 0)
)

} else if (idx >= lineBreakTranslation.size) {
visibleMessages.removeAt(msgIdx)
} else if (idx < lineBreakIncoming.size) {
visibleMessages[msgIdx] =
ChatHudLine(client.inGameHud.ticks, lineBreakTranslation[idx], 0)
} else {
visibleMessages.add(
msgIdx + 1,
ChatHudLine(client.inGameHud.ticks, lineBreakTranslation[idx], 0)
)
}
}
}

localMessageHistory[indexText] = ChatHudLine(client.inGameHud.ticks, formattedTranslation, 0)
} else {
TabsMod.logger.error("Encountered error code ${response.code()} when fetching translation: ${response.message()}")
localMessageHistory[indexText] = ChatHudLine(client.inGameHud.ticks, formattedTranslation, 0)
} else {
TabsMod.logger.error("Encountered error code ${response.code()} when fetching translation: ${response.message()}")
}
}
}
}
Expand Down

0 comments on commit 1172826

Please sign in to comment.