From 03a1a00e57633ff30048d5af09fa10db9dcf423b Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Tue, 11 Mar 2025 17:58:47 +0100 Subject: feat: Add SBRecombobulated --- src/main/kotlin/util/skyblock/Rarity.kt | 43 +++++++++++++++++++++++++++++++++ src/main/kotlin/util/textutil.kt | 28 +++++++++++++-------- 2 files changed, 61 insertions(+), 10 deletions(-) (limited to 'src/main/kotlin/util') diff --git a/src/main/kotlin/util/skyblock/Rarity.kt b/src/main/kotlin/util/skyblock/Rarity.kt index f244ef6..0b26bda 100644 --- a/src/main/kotlin/util/skyblock/Rarity.kt +++ b/src/main/kotlin/util/skyblock/Rarity.kt @@ -13,10 +13,14 @@ import net.minecraft.text.Text import net.minecraft.util.Formatting import moe.nea.firmament.util.StringUtil.words import moe.nea.firmament.util.collections.lastNotNullOfOrNull +import moe.nea.firmament.util.directLiteralStringContent import moe.nea.firmament.util.mc.loreAccordingToNbt import moe.nea.firmament.util.petData +import moe.nea.firmament.util.prepend +import moe.nea.firmament.util.prependHypixelified import moe.nea.firmament.util.removeColorCodes import moe.nea.firmament.util.unformattedString +import moe.nea.firmament.util.withColor typealias RepoRarity = io.github.moulberry.repo.data.Rarity @@ -52,6 +56,10 @@ enum class Rarity(vararg altNames: String) { val text: Text get() = Text.literal(name).setStyle(Style.EMPTY.withColor(colourMap[this])) val neuRepoRarity: RepoRarity? = RepoRarity.entries.find { it.name == name } + fun recombobulate(): Rarity = Rarity.entries.getOrElse(ordinal + 1) { this } + + fun colour() = colourMap[this] ?: Formatting.WHITE + companion object { // TODO: inline those formattings as fields val colourMap = mapOf( @@ -94,11 +102,46 @@ enum class Rarity(vararg altNames: String) { } } + fun findLoreIndex(lore: List): Int { + return lore.indexOfLast { + it.unformattedString.words().any { fromString(it) != null } + } + } + fun fromLore(lore: List): Rarity? = lore.lastNotNullOfOrNull { it.unformattedString.words() .firstNotNullOfOrNull(::fromString) } + fun recombobulateLore(lore: List): List { + val before = fromLore(lore) ?: return lore + val rarityIndex = findLoreIndex(lore) + if (rarityIndex < 0) return lore + val after = before.recombobulate() + val col = after.colour() + val loreMut = lore.toMutableList() + val obfuscatedTag = Text.literal("a") + .withColor(col) + .styled { it.withObfuscated(true) } + val rarityLine = loreMut[rarityIndex].copy() + .prependHypixelified(Text.literal(" ")) + .prepend(obfuscatedTag) + .append(Text.literal(" ")) + .append(obfuscatedTag) + (rarityLine.siblings as MutableList) + .replaceAll { + var content = it.directLiteralStringContent + before.names.forEach { + content = content?.replace(it, after.name) + } + val editedText = (if (content != it.directLiteralStringContent) + Text.literal(content) else it.copy()) + editedText.withColor(col) + } + loreMut[rarityIndex] = rarityLine + return loreMut + } + } } diff --git a/src/main/kotlin/util/textutil.kt b/src/main/kotlin/util/textutil.kt index 806f61e..a3a3388 100644 --- a/src/main/kotlin/util/textutil.kt +++ b/src/main/kotlin/util/textutil.kt @@ -56,21 +56,22 @@ fun OrderedText.reconstitute(): MutableText { return base } + fun StringVisitable.reconstitute(): MutableText { val base = Text.literal("") base.setStyle(Style.EMPTY.withItalic(false)) var lastColorCode = Style.EMPTY val text = StringBuilder() this.visit({ style, string -> - if (style != lastColorCode) { - if (text.isNotEmpty()) - base.append(Text.literal(text.toString()).setStyle(lastColorCode)) - lastColorCode = style - text.clear() - } - text.append(string) - Optional.empty() - }, Style.EMPTY) + if (style != lastColorCode) { + if (text.isNotEmpty()) + base.append(Text.literal(text.toString()).setStyle(lastColorCode)) + lastColorCode = style + text.clear() + } + text.append(string) + Optional.empty() + }, Style.EMPTY) if (text.isNotEmpty()) base.append(Text.literal(text.toString()).setStyle(lastColorCode)) return base @@ -127,7 +128,8 @@ fun MutableText.darkGrey() = withColor(Formatting.DARK_GRAY) fun MutableText.red() = withColor(Formatting.RED) fun MutableText.white() = withColor(Formatting.WHITE) fun MutableText.bold(): MutableText = styled { it.withBold(true) } -fun MutableText.hover(text: Text): MutableText = styled {it.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, text))} +fun MutableText.hover(text: Text): MutableText = + styled { it.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, text)) } fun MutableText.clickCommand(command: String): MutableText { @@ -137,6 +139,12 @@ fun MutableText.clickCommand(command: String): MutableText { } } +fun MutableText.prependHypixelified(text: Text): MutableText { + if (directLiteralStringContent == "") + return prepend(text) + return Text.literal("").styled { it.withItalic(false) }.append(this).prepend(text) +} + fun MutableText.prepend(text: Text): MutableText { siblings.addFirst(text) return this -- cgit