1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
package moe.nea.firmament.util.skyblock
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import net.minecraft.item.ItemStack
import net.minecraft.text.Style
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
@Serializable(with = Rarity.Serializer::class)
enum class Rarity(vararg altNames: String) {
COMMON,
UNCOMMON,
RARE,
EPIC,
LEGENDARY("LEGENJERRY"),
MYTHIC,
DIVINE,
SUPREME,
SPECIAL,
VERY_SPECIAL,
UNKNOWN
;
object Serializer : KSerializer<Rarity> {
override val descriptor: SerialDescriptor
get() = PrimitiveSerialDescriptor(Rarity::class.java.name, PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): Rarity {
return valueOf(decoder.decodeString().replace(" ", "_"))
}
override fun serialize(encoder: Encoder, value: Rarity) {
encoder.encodeString(value.name)
}
}
val names = setOf(name) + altNames
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(
Rarity.COMMON to Formatting.WHITE,
Rarity.UNCOMMON to Formatting.GREEN,
Rarity.RARE to Formatting.BLUE,
Rarity.EPIC to Formatting.DARK_PURPLE,
Rarity.LEGENDARY to Formatting.GOLD,
Rarity.MYTHIC to Formatting.LIGHT_PURPLE,
Rarity.DIVINE to Formatting.AQUA,
Rarity.SPECIAL to Formatting.RED,
Rarity.VERY_SPECIAL to Formatting.RED,
Rarity.SUPREME to Formatting.DARK_RED,
)
val byName = entries.flatMap { en -> en.names.map { it to en } }.toMap()
val fromNeuRepo = entries.associateBy { it.neuRepoRarity }
fun fromNeuRepo(repo: RepoRarity): Rarity? {
return fromNeuRepo[repo]
}
fun fromString(name: String): Rarity? {
return byName[name]
}
fun fromTier(tier: Int): Rarity? {
return entries.getOrNull(tier)
}
fun fromItem(itemStack: ItemStack): Rarity? {
return fromLore(itemStack.loreAccordingToNbt) ?: fromPetItem(itemStack)
}
fun fromPetItem(itemStack: ItemStack): Rarity? =
itemStack.petData?.tier?.let(::fromNeuRepo)
fun fromStringLore(lore: List<String>): Rarity? {
return lore.lastNotNullOfOrNull {
it.removeColorCodes().words().firstNotNullOfOrNull(::fromString)
}
}
fun findLoreIndex(lore: List<Text>): Int {
return lore.indexOfLast {
it.unformattedString.words().any { fromString(it) != null }
}
}
fun fromLore(lore: List<Text>): Rarity? =
lore.lastNotNullOfOrNull {
it.unformattedString.words()
.firstNotNullOfOrNull(::fromString)
}
fun recombobulateLore(lore: List<Text>): List<Text> {
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<Text>)
.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
}
}
}
|