diff options
author | Linnea Gräf <nea@nea.moe> | 2025-03-17 23:38:50 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2025-03-17 23:38:50 +0100 |
commit | 9484a621afdc79800b25215908e4ee886f328993 (patch) | |
tree | eaf8544fe5b8dba07895d4c74250f1b5413821ca /src/main/kotlin/util/skyblock/stats/StatBlock.kt | |
parent | aacd527cb8e226b92e5ad28d8d075da06fd79ec4 (diff) | |
download | Firmament-experiment/itemstacks.tar.gz Firmament-experiment/itemstacks.tar.bz2 Firmament-experiment/itemstacks.zip |
refactor: Add stats and reforges to SBItemPropertyexperiment/itemstacks
Diffstat (limited to 'src/main/kotlin/util/skyblock/stats/StatBlock.kt')
-rw-r--r-- | src/main/kotlin/util/skyblock/stats/StatBlock.kt | 61 |
1 files changed, 61 insertions, 0 deletions
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) + } + } +} |