Skip to content

Commit 76ddd48

Browse files
committed
feat: edit message function
[no ci]
1 parent 87add3d commit 76ddd48

File tree

1 file changed

+21
-0
lines changed
  • mod/src/main/kotlin/gg/skytils/skytilsmod/utils/multiplatform

1 file changed

+21
-0
lines changed

mod/src/main/kotlin/gg/skytils/skytilsmod/utils/multiplatform/chat.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package gg.skytils.skytilsmod.utils.multiplatform
2020

2121
import gg.essential.universal.UMinecraft
22+
import gg.skytils.skytilsmod.mixins.transformers.accessors.AccessorChatState
23+
import net.minecraft.client.gui.hud.ChatHudLine
2224
import net.minecraft.text.ClickEvent
2325
import net.minecraft.text.HoverEvent
2426
import net.minecraft.text.MutableText
@@ -43,6 +45,25 @@ fun MCTextComponent.map(action: Text.() -> Unit) {
4345
fun MCTextComponent.chat() =
4446
UMinecraft.getMinecraft().inGameHud.chatHud.addMessage(this)
4547

48+
fun MCTextComponent.edit(newComponent: MCTextComponent) {
49+
val oldState = UMinecraft.getMinecraft().inGameHud.chatHud.toChatState()
50+
val accessor = (oldState as AccessorChatState)
51+
val messages = accessor.messages
52+
val oldLine = messages.find { message ->
53+
this == message.content
54+
} ?: return
55+
messages.remove(oldLine)
56+
messages.add(ChatHudLine(
57+
UMinecraft.getMinecraft().inGameHud.ticks,
58+
newComponent,
59+
oldLine.signature,
60+
oldLine.indicator
61+
))
62+
UMinecraft.getMinecraft().inGameHud.chatHud.restoreChatState(
63+
oldState
64+
)
65+
}
66+
4667
fun MutableMCTextComponent.setHoverText(text: String) = apply {
4768
setStyle(style.withHoverEvent(HoverEvent.ShowText(Text.literal(text))))
4869
}

0 commit comments

Comments
 (0)