diff options
Diffstat (limited to 'src/main/kotlin/util')
-rw-r--r-- | src/main/kotlin/util/skyblock/stats/BuffKind.kt | 15 | ||||
-rw-r--r-- | src/main/kotlin/util/skyblock/stats/StatBlock.kt | 61 | ||||
-rw-r--r-- | src/main/kotlin/util/skyblock/stats/StatFormatting.kt | 63 | ||||
-rw-r--r-- | src/main/kotlin/util/skyblock/stats/StatLine.kt | 71 |
4 files changed, 210 insertions, 0 deletions
diff --git a/src/main/kotlin/util/skyblock/stats/BuffKind.kt b/src/main/kotlin/util/skyblock/stats/BuffKind.kt new file mode 100644 index 0000000..c0b45c4 --- /dev/null +++ b/src/main/kotlin/util/skyblock/stats/BuffKind.kt @@ -0,0 +1,15 @@ +package moe.nea.firmament.util.skyblock.stats + +import net.minecraft.util.Formatting + +enum class BuffKind( + val color: Formatting, + val prefix: String, + val postFix: String, + val isHidden: Boolean, +) { + REFORGE(Formatting.BLUE, "(", ")", false), + STAR_BUFF(Formatting.RESET, "", "", true), + CATA_STAR_BUFF(Formatting.DARK_GRAY, "(", ")", false), + ; +} diff --git a/src/main/kotlin/util/skyblock/stats/StatBlock.kt b/src/main/kotlin/util/skyblock/stats/StatBlock.kt new file mode 100644 index 0000000..3865e79 --- /dev/null +++ b/src/main/kotlin/util/skyblock/stats/StatBlock.kt @@ -0,0 +1,61 @@ +package moe.nea.firmament.util.skyblock.stats + +import util.skyblock.stats.StatFormatting +import moe.nea.firmament.util.directLiteralStringContent +import moe.nea.firmament.util.useMatch +import net.minecraft.text.Text +import net.minecraft.text.TextColor +import net.minecraft.util.Formatting + +data class StatBlock( + val indexedByName: Map<String, StatLine>, + val startIndex: Int, + val endIndex: Int, + private val modifiedLines: MutableMap<String, StatLine> = mutableMapOf() +) { + + /** + * Note that the returned stat line must be created by copying from the original stat line (to keep indexes in sync). + */ + fun modify(statName: String, mod: (StatLine) -> StatLine) { + val existing = + modifiedLines[statName] ?: indexedByName[statName] + ?: StatLine(StatFormatting.findForName(statName), 0.0) + modifiedLines[statName] = mod(existing) + } + + fun applyModifications(lore: MutableList<Text>) { + if (modifiedLines.isEmpty()) return + var nextAppendIndex = endIndex + if (startIndex < 0) // No existing stat block, insert the after space. + lore.add(0, Text.literal("")) + modifiedLines.values.forEach { line -> + val loreLine = if (line.loreIndex < 0) { + lore.add(nextAppendIndex, Text.literal("")) + nextAppendIndex++ + } else line.loreIndex + lore[loreLine] = line.reconstitute() + } + } + + companion object { + fun fromLore(lore: List<Text>): StatBlock { + val map = mutableMapOf<String, StatLine>() + var start = -1 + var end = 0 + for ((index, text) in lore.withIndex()) { + val statLine = StatLine.fromLoreLine(text) + ?.copy(loreIndex = index) + if (statLine == null) { + if (start < 0) continue + else break + } + map[statLine.statName] = statLine + if (start < 0) + start = index + end = index + 1 + } + return StatBlock(map, start, end) + } + } +} diff --git a/src/main/kotlin/util/skyblock/stats/StatFormatting.kt b/src/main/kotlin/util/skyblock/stats/StatFormatting.kt new file mode 100644 index 0000000..28a329e --- /dev/null +++ b/src/main/kotlin/util/skyblock/stats/StatFormatting.kt @@ -0,0 +1,63 @@ +package util.skyblock.stats + +import net.minecraft.util.Formatting + +data class StatFormatting( + val name: String, + val postFix: String, + val color: Formatting, + val isStarAffected: Boolean = true, +) { + companion object { + fun statIdToName(statId: String): String { + val segments = statId.split("_") + return segments.joinToString(" ") { it.replaceFirstChar { it.uppercaseChar() } } + } + fun findForName(name: String) = + formattingOverrides[name] ?: StatFormatting(name, "", Formatting.GREEN) + + fun findForId(id: String) = findForName(statIdToName(id)) + + val allFormattingOverrides = listOf( + StatFormatting("Sea Creature Chance", "%", Formatting.RED), + StatFormatting("Strength", "", Formatting.RED), + StatFormatting("Damage", "", Formatting.RED), + StatFormatting("Bonus Attack Speed", "%", Formatting.RED), + StatFormatting("Shot Cooldown", "s", Formatting.GREEN, false), + StatFormatting("Ability Damage", "%", Formatting.RED), + StatFormatting("Crit Damage", "%", Formatting.RED), + StatFormatting("Crit Chance", "%", Formatting.RED), + StatFormatting("Ability Damage", "%", Formatting.RED), + StatFormatting("Trophy Fish Chance", "%", Formatting.GREEN), + StatFormatting("Health", "", Formatting.GREEN), + StatFormatting("Defense", "", Formatting.GREEN), + StatFormatting("Fishing Speed", "", Formatting.GREEN), + StatFormatting("Double Hook Chance", "%", Formatting.GREEN), + StatFormatting("Mining Speed", "", Formatting.GREEN), + StatFormatting("Mining Fortune", "", Formatting.GREEN), + StatFormatting("Heat Resistance", "", Formatting.GREEN), + StatFormatting("Swing Range", "", Formatting.GREEN), + StatFormatting("Rift Time", "", Formatting.GREEN), + StatFormatting("Speed", "", Formatting.GREEN), + StatFormatting("Farming Fortune", "", Formatting.GREEN), + StatFormatting("True Defense", "", Formatting.GREEN), + StatFormatting("Mending", "", Formatting.GREEN), + StatFormatting("Foraging Wisdom", "", Formatting.GREEN), + StatFormatting("Farming Wisdom", "", Formatting.GREEN), + StatFormatting("Foraging Fortune", "", Formatting.GREEN), + StatFormatting("Magic Find", "", Formatting.GREEN), + StatFormatting("Ferocity", "", Formatting.GREEN), + StatFormatting("Bonus Pest Chance", "%", Formatting.GREEN), + StatFormatting("Cold Resistance", "", Formatting.GREEN), + StatFormatting("Pet Luck", "", Formatting.GREEN), + StatFormatting("Fear", "", Formatting.GREEN), + StatFormatting("Mana Regen", "%", Formatting.GREEN), + StatFormatting("Rift Damage", "", Formatting.GREEN), + StatFormatting("Hearts", "", Formatting.GREEN), + StatFormatting("Vitality", "", Formatting.GREEN), + // TODO: make this a repo json + ) + val formattingOverrides = allFormattingOverrides.associateBy { it.name } + + } +} diff --git a/src/main/kotlin/util/skyblock/stats/StatLine.kt b/src/main/kotlin/util/skyblock/stats/StatLine.kt new file mode 100644 index 0000000..5a1c236 --- /dev/null +++ b/src/main/kotlin/util/skyblock/stats/StatLine.kt @@ -0,0 +1,71 @@ +package moe.nea.firmament.util.skyblock.stats + +import moe.nea.firmament.util.FirmFormatters +import moe.nea.firmament.util.grey +import moe.nea.firmament.util.withColor +import net.minecraft.text.Style +import net.minecraft.text.Text +import util.skyblock.stats.StatFormatting +import net.minecraft.text.TextColor +import net.minecraft.util.Formatting +import moe.nea.firmament.util.directLiteralStringContent +import moe.nea.firmament.util.useMatch + +data class StatLine( + val stat: StatFormatting, + val value: Double, + val modifiers: List<Text> = listOf(), + val loreIndex: Int = -1, +) { + val statName get() = stat.name + + fun formatValue() = + Text.literal( + FirmFormatters.formatCommas(value, 1, includeSign = true) + stat.postFix + " " + ).setStyle(Style.EMPTY.withColor(stat.color)) + + fun reconstitute(abbreviateTo: Int = Int.MAX_VALUE): Text = + Text.literal("").setStyle(Style.EMPTY.withItalic(false)) + .append(Text.literal("${abbreviate(abbreviateTo)}: ").grey()) + .append(formatValue()) + .also { modifiers.forEach(it::append) } + + /* + TODO: fix up the formatting of these + Expected: StatBlock(indexedByName={Speed=StatLine(stat=StatFormatting(name=Speed, postFix=, color=§a, isStarAffected=true), value=5.0, modifiers=[literal{(+5)}[style={color=blue}]], loreIndex=0), Foraging Wisdom=StatLine(stat=StatFormatting(name=Foraging Wisdom, postFix=, color=§a, isStarAffected=true), value=1.5, modifiers=[literal{(+1.5)}[style={color=blue}]], loreIndex=1)}, startIndex=0, endIndex=2, modifiedLines={}) + Actual : StatBlock(indexedByName={Speed=StatLine(stat=StatFormatting(name=Speed, postFix=, color=§a, isStarAffected=true), value=5.0, modifiers=[literal{(+5) }[style={color=blue,!bold,!italic}]], loreIndex=0), Foraging Wisdom=StatLine(stat=StatFormatting(name=Foraging Wisdom, postFix=, color=§a, isStarAffected=true), value=1.2, modifiers=[literal{(+1.2) }[style={color=blue,!bold,!italic}]], loreIndex=1)}, startIndex=0, endIndex=2, modifiedLines={})*/ + fun addStat(amount: Double, buffKind: BuffKind): StatLine { + val formattedAmount = FirmFormatters.formatCommas(amount, 1, includeSign = true) + val modifierText = Text.literal( + buffKind.prefix + formattedAmount + stat.postFix + buffKind.postFix + " ") + .withColor(buffKind.color) + return copy( + value = value + amount, + modifiers = modifiers + + if (buffKind.isHidden) emptyList() + else listOf(modifierText)) + } + + private fun abbreviate(abbreviateTo: Int): String { + if (abbreviateTo >= statName.length) return statName + val segments = statName.split(" ") + return segments.joinToString(" ") { + it.substring(0, maxOf(1, abbreviateTo / segments.size)) + } + } + + companion object { + private val statLabelRegex = "(?<statName>.*): ".toPattern() + fun fromLoreLine(line: Text): StatLine? { + val sibs = line.siblings + if (sibs.size < 2) return null + val stat = sibs.first() ?: return null + if (stat.style.color != TextColor.fromFormatting(Formatting.GRAY)) return null + val statLabel = stat.directLiteralStringContent ?: return null + val statName = statLabelRegex.useMatch(statLabel) { group("statName") } ?: return null + return StatLine(StatFormatting.findForName(statName), + sibs[1]?.directLiteralStringContent?.trim(' ', 's', '%', '+')?.toDoubleOrNull() ?: 0.0, + sibs.subList(2, sibs.size)) + } + } +} |