diff options
author | Linnea Gräf <nea@nea.moe> | 2024-05-07 21:11:09 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-05-07 21:11:09 +0200 |
commit | 93e6e0ab16df0808ee7720202a32967eddcf5c10 (patch) | |
tree | 0939d44ec0c848f8712df8b03f30af1d34eabfa0 /src/main/kotlin/moe/nea/firmament/features/chat | |
parent | 8f3cc34740fcfe1572d23c8f1c1db1a309217b84 (diff) | |
download | Firmament-93e6e0ab16df0808ee7720202a32967eddcf5c10.tar.gz Firmament-93e6e0ab16df0808ee7720202a32967eddcf5c10.tar.bz2 Firmament-93e6e0ab16df0808ee7720202a32967eddcf5c10.zip |
Fix up most of the remaining event handlers
[no changelog]
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/features/chat')
3 files changed, 114 insertions, 108 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/features/chat/AutoCompletions.kt b/src/main/kotlin/moe/nea/firmament/features/chat/AutoCompletions.kt index 9912321..3351970 100644 --- a/src/main/kotlin/moe/nea/firmament/features/chat/AutoCompletions.kt +++ b/src/main/kotlin/moe/nea/firmament/features/chat/AutoCompletions.kt @@ -7,6 +7,7 @@ package moe.nea.firmament.features.chat import com.mojang.brigadier.arguments.StringArgumentType.string +import moe.nea.firmament.annotations.Subscribe import moe.nea.firmament.commands.get import moe.nea.firmament.commands.suggestsList import moe.nea.firmament.commands.thenArgument @@ -30,28 +31,28 @@ object AutoCompletions : FirmamentFeature { override val identifier: String get() = "auto-completions" - override fun onLoad() { - MaskCommands.subscribe { - if (TConfig.provideWarpTabCompletion) { - it.mask("warp") - } + @Subscribe + fun onMaskCommands(event: MaskCommands) { + if (TConfig.provideWarpTabCompletion) { + event.mask("warp") } - CommandEvent.subscribe { - if (TConfig.provideWarpTabCompletion) { - it.deleteCommand("warp") - it.register("warp") { - thenArgument("to", string()) { toArg -> - suggestsList { - RepoManager.neuRepo.constants?.islands?.warps?.flatMap { listOf(it.warp) + it.aliases } ?: listOf() - } - thenExecute { - val warpName = get(toArg) - if (warpName == "is" && TConfig.replaceWarpIsByWarpIsland) { - MC.sendServerCommand("warp island") - } else { - MC.sendServerCommand("warp ${warpName}") - } - } + } + + @Subscribe + fun onCommandEvent(event: CommandEvent) { + if (!TConfig.provideWarpTabCompletion) return + event.deleteCommand("warp") + event.register("warp") { + thenArgument("to", string()) { toArg -> + suggestsList { + RepoManager.neuRepo.constants?.islands?.warps?.flatMap { listOf(it.warp) + it.aliases } ?: listOf() + } + thenExecute { + val warpName = get(toArg) + if (warpName == "is" && TConfig.replaceWarpIsByWarpIsland) { + MC.sendServerCommand("warp island") + } else { + MC.sendServerCommand("warp $warpName") } } } diff --git a/src/main/kotlin/moe/nea/firmament/features/chat/ChatLinks.kt b/src/main/kotlin/moe/nea/firmament/features/chat/ChatLinks.kt index 6bb6f6e..717e503 100644 --- a/src/main/kotlin/moe/nea/firmament/features/chat/ChatLinks.kt +++ b/src/main/kotlin/moe/nea/firmament/features/chat/ChatLinks.kt @@ -6,11 +6,11 @@ package moe.nea.firmament.features.chat -import io.ktor.client.request.* -import io.ktor.client.statement.* -import io.ktor.utils.io.jvm.javaio.* +import io.ktor.client.request.get +import io.ktor.client.statement.bodyAsChannel +import io.ktor.utils.io.jvm.javaio.toInputStream import java.net.URL -import java.util.* +import java.util.Collections import moe.nea.jarvis.api.Point import kotlinx.coroutines.Deferred import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -26,6 +26,7 @@ import net.minecraft.text.Text import net.minecraft.util.Formatting import net.minecraft.util.Identifier import moe.nea.firmament.Firmament +import moe.nea.firmament.annotations.Subscribe import moe.nea.firmament.events.ModifyChatEvent import moe.nea.firmament.events.ScreenRenderPostEvent import moe.nea.firmament.features.FirmamentFeature @@ -96,69 +97,69 @@ object ChatLinks : FirmamentFeature { return (url.substringAfterLast('.').lowercase() in imageExtensions) } + @Subscribe @OptIn(ExperimentalCoroutinesApi::class) - override fun onLoad() { - ModifyChatEvent.subscribe { - if (TConfig.enableLinks) - it.replaceWith = it.replaceWith.transformEachRecursively { child -> - val text = child.string - if ("://" !in text) return@transformEachRecursively child - val s = Text.empty().setStyle(child.style) - var index = 0 - while (index < text.length) { - val nextMatch = urlRegex.find(text, index) - if (nextMatch == null) { - s.append(Text.literal(text.substring(index, text.length))) - break - } - val range = nextMatch.groups[0]!!.range - val url = nextMatch.groupValues[0] - s.append(Text.literal(text.substring(index, range.first))) - s.append( - Text.literal(url).setStyle( - Style.EMPTY.withUnderline(true).withColor( - Formatting.AQUA - ).withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal(url))) - .withClickEvent(ClickEvent(ClickEvent.Action.OPEN_URL, url)) - ) - ) - if (isImageUrl(url)) - tryCacheUrl(url) - index = range.last + 1 - } - s - } - } + fun onRender(it: ScreenRenderPostEvent) { + if (!TConfig.imageEnabled) return + if (it.screen !is ChatScreen) return + val hoveredComponent = + MC.inGameHud.chatHud.getTextStyleAt(it.mouseX.toDouble(), it.mouseY.toDouble()) ?: return + val hoverEvent = hoveredComponent.hoverEvent ?: return + val value = hoverEvent.getValue(HoverEvent.Action.SHOW_TEXT) ?: return + val url = urlRegex.matchEntire(value.unformattedString)?.groupValues?.get(0) ?: return + if (!isImageUrl(url)) return + val imageFuture = imageCache[url] ?: return + if (!imageFuture.isCompleted) return + val image = imageFuture.getCompleted() ?: return + it.drawContext.matrices.push() + val pos = TConfig.position + pos.applyTransformations(it.drawContext.matrices) + val scale = min(1F, min((9 * 20F) / image.height, (16 * 20F) / image.width)) + it.drawContext.matrices.scale(scale, scale, 1F) + it.drawContext.drawTexture( + image.texture, + 0, + 0, + 1F, + 1F, + image.width, + image.height, + image.width, + image.height, + ) + it.drawContext.matrices.pop() + } - ScreenRenderPostEvent.subscribe { - if (!TConfig.imageEnabled) return@subscribe - if (it.screen !is ChatScreen) return@subscribe - val hoveredComponent = - MC.inGameHud.chatHud.getTextStyleAt(it.mouseX.toDouble(), it.mouseY.toDouble()) ?: return@subscribe - val hoverEvent = hoveredComponent.hoverEvent ?: return@subscribe - val value = hoverEvent.getValue(HoverEvent.Action.SHOW_TEXT) ?: return@subscribe - val url = urlRegex.matchEntire(value.unformattedString)?.groupValues?.get(0) ?: return@subscribe - if (!isImageUrl(url)) return@subscribe - val imageFuture = imageCache[url] ?: return@subscribe - if (!imageFuture.isCompleted) return@subscribe - val image = imageFuture.getCompleted() ?: return@subscribe - it.drawContext.matrices.push() - val pos = TConfig.position - pos.applyTransformations(it.drawContext.matrices) - val scale = min(1F, min((9 * 20F) / image.height, (16 * 20F) / image.width)) - it.drawContext.matrices.scale(scale, scale, 1F) - it.drawContext.drawTexture( - image.texture, - 0, - 0, - 1F, - 1F, - image.width, - image.height, - image.width, - image.height, - ) - it.drawContext.matrices.pop() + @Subscribe + fun onModifyChat(it: ModifyChatEvent) { + if (!TConfig.enableLinks) return + it.replaceWith = it.replaceWith.transformEachRecursively { child -> + val text = child.string + if ("://" !in text) return@transformEachRecursively child + val s = Text.empty().setStyle(child.style) + var index = 0 + while (index < text.length) { + val nextMatch = urlRegex.find(text, index) + if (nextMatch == null) { + s.append(Text.literal(text.substring(index, text.length))) + break + } + val range = nextMatch.groups[0]!!.range + val url = nextMatch.groupValues[0] + s.append(Text.literal(text.substring(index, range.first))) + s.append( + Text.literal(url).setStyle( + Style.EMPTY.withUnderline(true).withColor( + Formatting.AQUA + ).withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal(url))) + .withClickEvent(ClickEvent(ClickEvent.Action.OPEN_URL, url)) + ) + ) + if (isImageUrl(url)) + tryCacheUrl(url) + index = range.last + 1 + } + s } } } diff --git a/src/main/kotlin/moe/nea/firmament/features/chat/QuickCommands.kt b/src/main/kotlin/moe/nea/firmament/features/chat/QuickCommands.kt index 47be3c1..97ec6e8 100644 --- a/src/main/kotlin/moe/nea/firmament/features/chat/QuickCommands.kt +++ b/src/main/kotlin/moe/nea/firmament/features/chat/QuickCommands.kt @@ -8,6 +8,7 @@ package moe.nea.firmament.features.chat import com.mojang.brigadier.context.CommandContext import net.minecraft.text.Text +import moe.nea.firmament.annotations.Subscribe import moe.nea.firmament.commands.DefaultSource import moe.nea.firmament.commands.RestArgumentType import moe.nea.firmament.commands.get @@ -34,29 +35,30 @@ object QuickCommands : FirmamentFeature { val kuudraLevelNames = listOf("NORMAL", "HOT", "BURNING", "FIERY", "INFERNAL") val dungeonLevelNames = listOf("ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN") - override fun onLoad() { - CommandEvent.subscribe { - it.register("join") { - thenArgument("what", RestArgumentType) { what -> - thenExecute { - val what = this[what] - if (!SBData.isOnSkyblock) { - MC.sendCommand("join $what") - return@thenExecute - } - val joinName = getNameForFloor(what.replace(" ", "").lowercase()) - if (joinName == null) { - source.sendFeedback(Text.stringifiedTranslatable("firmament.quick-commands.join.unknown", what)) - } else { - source.sendFeedback(Text.stringifiedTranslatable("firmament.quick-commands.join.success", joinName)) - MC.sendCommand("joininstance $joinName") - } - } - } + + @Subscribe + fun onCommands(it: CommandEvent) { + it.register("join") { + thenArgument("what", RestArgumentType) { what -> thenExecute { - source.sendFeedback(Text.translatable("firmament.quick-commands.join.explain")) + val what = this[what] + if (!SBData.isOnSkyblock) { + MC.sendCommand("join $what") + return@thenExecute + } + val joinName = getNameForFloor(what.replace(" ", "").lowercase()) + if (joinName == null) { + source.sendFeedback(Text.stringifiedTranslatable("firmament.quick-commands.join.unknown", what)) + } else { + source.sendFeedback(Text.stringifiedTranslatable("firmament.quick-commands.join.success", + joinName)) + MC.sendCommand("joininstance $joinName") + } } } + thenExecute { + source.sendFeedback(Text.translatable("firmament.quick-commands.join.explain")) + } } } @@ -70,7 +72,8 @@ object QuickCommands : FirmamentFeature { ) } if (l !in kuudraLevelNames.indices) { - source.sendFeedback(Text.stringifiedTranslatable("firmament.quick-commands.join.unknown-kuudra", kuudraLevel)) + source.sendFeedback(Text.stringifiedTranslatable("firmament.quick-commands.join.unknown-kuudra", + kuudraLevel)) return null } return "KUUDRA_${kuudraLevelNames[l]}" @@ -90,7 +93,8 @@ object QuickCommands : FirmamentFeature { return "CATACOMBS_ENTRANCE" } if (l !in dungeonLevelNames.indices) { - source.sendFeedback(Text.stringifiedTranslatable("firmament.quick-commands.join.unknown-catacombs", kuudraLevel)) + source.sendFeedback(Text.stringifiedTranslatable("firmament.quick-commands.join.unknown-catacombs", + kuudraLevel)) return null } return "${if (masterLevel != null) "MASTER_" else ""}CATACOMBS_FLOOR_${dungeonLevelNames[l]}" |