aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-10-24 14:49:21 +0200
committerLinnea Gräf <nea@nea.moe>2024-10-24 14:49:21 +0200
commit0357245e88be4b8cd3d0681340d9a7065361ea74 (patch)
treecb52c479bc029295075ceeaa04251fae63ec5e50 /src/main/kotlin
parent6100b8c9a0fe42be52a759b429f532c12cc72a32 (diff)
downloadFirmament-0357245e88be4b8cd3d0681340d9a7065361ea74.tar.gz
Firmament-0357245e88be4b8cd3d0681340d9a7065361ea74.tar.bz2
Firmament-0357245e88be4b8cd3d0681340d9a7065361ea74.zip
Add getLegacyFormatString method
[no changelog]
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/commands/rome.kt15
-rw-r--r--src/main/kotlin/util/textutil.kt25
2 files changed, 39 insertions, 1 deletions
diff --git a/src/main/kotlin/commands/rome.kt b/src/main/kotlin/commands/rome.kt
index 4f54872..8fcbd77 100644
--- a/src/main/kotlin/commands/rome.kt
+++ b/src/main/kotlin/commands/rome.kt
@@ -30,6 +30,8 @@ import moe.nea.firmament.util.SkyblockId
import moe.nea.firmament.util.accessors.messages
import moe.nea.firmament.util.collections.InstanceList
import moe.nea.firmament.util.collections.WeakCache
+import moe.nea.firmament.util.mc.SNbtFormatter
+import moe.nea.firmament.util.unformattedString
fun firmamentCommand() = literal("firmament") {
@@ -44,7 +46,8 @@ fun firmamentCommand() = literal("firmament") {
}
thenArgument("property", string()) { property ->
suggestsList {
- (ManagedConfig.allManagedConfigs.getAll().find { it.name == this[config] } ?: return@suggestsList listOf())
+ (ManagedConfig.allManagedConfigs.getAll().find { it.name == this[config] }
+ ?: return@suggestsList listOf())
.allOptions.entries.asSequence().filter { it.value.handler is BooleanHandler }
.map { it.key }
.asIterable()
@@ -216,6 +219,16 @@ fun firmamentCommand() = literal("firmament") {
println(nbt)
}
}
+ thenArgument("search", string()) { search ->
+ thenExecute {
+ MC.inGameHud.chatHud.messages
+ .filter { this[search] in it.content.unformattedString }
+ .forEach {
+ val nbt = TextCodecs.CODEC.encodeStart(NbtOps.INSTANCE, it.content).orThrow
+ println(SNbtFormatter.prettify(nbt))
+ }
+ }
+ }
}
thenLiteral("sbdata") {
thenExecute {
diff --git a/src/main/kotlin/util/textutil.kt b/src/main/kotlin/util/textutil.kt
index 1cef5d4..252f708 100644
--- a/src/main/kotlin/util/textutil.kt
+++ b/src/main/kotlin/util/textutil.kt
@@ -3,6 +3,7 @@ package moe.nea.firmament.util
import net.minecraft.text.MutableText
import net.minecraft.text.PlainTextContent
import net.minecraft.text.Text
+import net.minecraft.text.TextColor
import net.minecraft.text.TranslatableTextContent
import net.minecraft.util.Formatting
import moe.nea.firmament.Firmament
@@ -92,6 +93,30 @@ val Text.unformattedString: String
val Text.directLiteralStringContent: String? get() = (this.content as? PlainTextContent)?.string()
+fun Text.getLegacyFormatString() =
+ run {
+ val sb = StringBuilder()
+ for (component in iterator()) {
+ sb.append(component.style.color?.toChatFormatting()?.toString() ?: "§r")
+ sb.append(component.directLiteralStringContent)
+ sb.append("§r")
+ }
+ sb.toString()
+ }
+
+private val textColorLUT = Formatting.entries
+ .mapNotNull { formatting -> formatting.colorValue?.let { it to formatting } }
+ .toMap()
+
+fun TextColor.toChatFormatting(): Formatting? {
+ return textColorLUT[this.rgb]
+}
+
+fun Text.iterator(): Sequence<Text> {
+ return sequenceOf(this) + siblings.asSequence()
+ .flatMap { it.iterator() } // TODO: in theory we want to properly inherit styles here
+}
+
fun Text.allSiblings(): List<Text> = listOf(this) + siblings.flatMap { it.allSiblings() }
fun MutableText.withColor(formatting: Formatting) = this.styled { it.withColor(formatting).withItalic(false) }