aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/features/chat/ChatLinks.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/features/chat/ChatLinks.kt')
-rw-r--r--src/main/kotlin/features/chat/ChatLinks.kt96
1 files changed, 47 insertions, 49 deletions
diff --git a/src/main/kotlin/features/chat/ChatLinks.kt b/src/main/kotlin/features/chat/ChatLinks.kt
index a084234..aca7af8 100644
--- a/src/main/kotlin/features/chat/ChatLinks.kt
+++ b/src/main/kotlin/features/chat/ChatLinks.kt
@@ -1,48 +1,48 @@
package moe.nea.firmament.features.chat
-import io.ktor.client.request.get
-import io.ktor.client.statement.bodyAsChannel
-import io.ktor.utils.io.jvm.javaio.toInputStream
import java.net.URI
-import java.net.URL
import java.util.Collections
import java.util.concurrent.atomic.AtomicInteger
-import moe.nea.jarvis.api.Point
+import org.joml.Vector2i
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.async
+import kotlinx.coroutines.future.await
import kotlin.math.min
-import net.minecraft.client.gui.screen.ChatScreen
-import net.minecraft.client.texture.NativeImage
-import net.minecraft.client.texture.NativeImageBackedTexture
-import net.minecraft.text.ClickEvent
-import net.minecraft.text.HoverEvent
-import net.minecraft.text.Style
-import net.minecraft.text.Text
-import net.minecraft.util.Formatting
-import net.minecraft.util.Identifier
+import net.minecraft.client.gui.screens.ChatScreen
+import com.mojang.blaze3d.platform.NativeImage
+import net.minecraft.client.renderer.texture.DynamicTexture
+import net.minecraft.network.chat.ClickEvent
+import net.minecraft.network.chat.HoverEvent
+import net.minecraft.network.chat.Style
+import net.minecraft.network.chat.Component
+import net.minecraft.ChatFormatting
+import net.minecraft.resources.ResourceLocation
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
-import moe.nea.firmament.gui.config.ManagedConfig
+import moe.nea.firmament.jarvis.JarvisIntegration
import moe.nea.firmament.util.MC
+import moe.nea.firmament.util.data.Config
+import moe.nea.firmament.util.data.ManagedConfig
+import moe.nea.firmament.util.net.HttpUtil
import moe.nea.firmament.util.render.drawTexture
import moe.nea.firmament.util.transformEachRecursively
import moe.nea.firmament.util.unformattedString
-object ChatLinks : FirmamentFeature {
- override val identifier: String
+object ChatLinks {
+ val identifier: String
get() = "chat-links"
+ @Config
object TConfig : ManagedConfig(identifier, Category.CHAT) {
val enableLinks by toggle("links-enabled") { true }
val imageEnabled by toggle("image-enabled") { true }
val allowAllHosts by toggle("allow-all-hosts") { false }
val allowedHosts by string("allowed-hosts") { "cdn.discordapp.com,media.discordapp.com,media.discordapp.net,i.imgur.com" }
val actualAllowedHosts get() = allowedHosts.split(",").map { it.trim() }
- val position by position("position", 16 * 20, 9 * 20) { Point(0.0, 0.0) }
+ val position by position("position", 16 * 20, 9 * 20) { Vector2i(0, 0) }
}
private fun isHostAllowed(host: String) =
@@ -50,12 +50,11 @@ object ChatLinks : FirmamentFeature {
private fun isUrlAllowed(url: String) = isHostAllowed(url.removePrefix("https://").substringBefore("/"))
- override val config get() = TConfig
- val urlRegex = "https://[^. ]+\\.[^ ]+(\\.?( |$))".toRegex()
+ val urlRegex = "https://[^. ]+\\.[^ ]+(\\.?(\\s|$))".toRegex()
val nextTexId = AtomicInteger(0)
data class Image(
- val texture: Identifier,
+ val texture: ResourceLocation,
val width: Int,
val height: Int,
)
@@ -72,18 +71,16 @@ object ChatLinks : FirmamentFeature {
}
imageCache[url] = Firmament.coroutineScope.async {
try {
- val response = Firmament.httpClient.get(URL(url))
- if (response.status.value == 200) {
- val inputStream = response.bodyAsChannel().toInputStream(Firmament.globalJob)
- val image = NativeImage.read(inputStream)
- val texId = Firmament.identifier("dynamic_image_preview${nextTexId.getAndIncrement()}")
- MC.textureManager.registerTexture(
- texId,
- NativeImageBackedTexture({ texId.path }, image)
- )
- Image(texId, image.width, image.height)
- } else
- null
+ val inputStream = HttpUtil.request(url)
+ .forInputStream()
+ .await()
+ val image = NativeImage.read(inputStream)
+ val texId = Firmament.identifier("dynamic_image_preview${nextTexId.getAndIncrement()}")
+ MC.textureManager.register(
+ texId,
+ DynamicTexture({ texId.path }, image)
+ )
+ Image(texId, image.width, image.height)
} catch (exc: Exception) {
exc.printStackTrace()
null
@@ -102,7 +99,7 @@ object ChatLinks : FirmamentFeature {
if (!TConfig.imageEnabled) return
if (it.screen !is ChatScreen) return
val hoveredComponent =
- MC.inGameHud.chatHud.getTextStyleAt(it.mouseX.toDouble(), it.mouseY.toDouble()) ?: return
+ MC.inGameHud.chat.getClickedComponentStyleAt(it.mouseX.toDouble(), it.mouseY.toDouble()) ?: return
val hoverEvent = hoveredComponent.hoverEvent as? HoverEvent.ShowText ?: return
val value = hoverEvent.value
val url = urlRegex.matchEntire(value.unformattedString)?.groupValues?.get(0) ?: return
@@ -110,11 +107,11 @@ object ChatLinks : FirmamentFeature {
val imageFuture = imageCache[url] ?: return
if (!imageFuture.isCompleted) return
val image = imageFuture.getCompleted() ?: return
- it.drawContext.matrices.push()
+ it.drawContext.pose().pushMatrix()
val pos = TConfig.position
- pos.applyTransformations(it.drawContext.matrices)
+ pos.applyTransformations(JarvisIntegration.jarvis, it.drawContext.pose())
val scale = min(1F, min((9 * 20F) / image.height, (16 * 20F) / image.width))
- it.drawContext.matrices.scale(scale, scale, 1F)
+ it.drawContext.pose().scale(scale, scale)
it.drawContext.drawTexture(
image.texture,
0,
@@ -126,7 +123,7 @@ object ChatLinks : FirmamentFeature {
image.width,
image.height,
)
- it.drawContext.matrices.pop()
+ it.drawContext.pose().popMatrix()
}
@Subscribe
@@ -135,23 +132,24 @@ object ChatLinks : FirmamentFeature {
it.replaceWith = it.replaceWith.transformEachRecursively { child ->
val text = child.string
if ("://" !in text) return@transformEachRecursively child
- val s = Text.empty().setStyle(child.style)
+ val s = Component.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)))
+ val url = nextMatch?.groupValues[0]
+ val uri = runCatching { url?.let(::URI) }.getOrNull()
+ if (nextMatch == null || url == null || uri == null) {
+ s.append(Component.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(Component.literal(text.substring(index, range.first)))
s.append(
- Text.literal(url).setStyle(
- Style.EMPTY.withUnderline(true).withColor(
- Formatting.AQUA
- ).withHoverEvent(HoverEvent.ShowText(Text.literal(url)))
- .withClickEvent(ClickEvent.OpenUrl(URI(url)))
+ Component.literal(url).setStyle(
+ Style.EMPTY.withUnderlined(true).withColor(
+ ChatFormatting.AQUA
+ ).withHoverEvent(HoverEvent.ShowText(Component.literal(url)))
+ .withClickEvent(ClickEvent.OpenUrl(uri))
)
)
if (isImageUrl(url))