Skip to content

Commit 03a1a00

Browse files
committed
feat: Add SBRecombobulated
1 parent 17a855a commit 03a1a00

File tree

5 files changed

+106
-13
lines changed

5 files changed

+106
-13
lines changed

src/main/kotlin/repo/item/SBItemId.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ object SBItemId : SBItemProperty.State<SkyblockId>() {
1616

1717
override fun applyToStack(stack: ItemStack, store: SBItemData, value: SkyblockId?): ItemStack {
1818
val id = value ?: SkyblockId.NULL
19-
return RepoManager.getNEUItem(id).asItemStack(idHint = id)
19+
return RepoManager.getNEUItem(id).asItemStack(idHint = id).copy()
2020
}
2121

2222
override val order: Int

src/main/kotlin/repo/item/SBItemProperty.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import moe.nea.firmament.util.compatloader.CompatLoader
88
/**
99
* A property of a skyblock item. Not every skyblock item must have this property, but some should.
1010
*
11-
* Access to this class should be limited to [State.bindWith] and [SBItemData.getData].
11+
* Access to this class should be limited to [State.bind] and [SBItemData.getData].
1212
* @see State
1313
*/
1414
abstract class SBItemProperty<T> {
@@ -36,8 +36,12 @@ abstract class SBItemProperty<T> {
3636
* to change the state of an item, including its rendering as a vanilla [ItemStack].
3737
*/
3838
abstract class State<T> : SBItemProperty<T>() {
39+
/**
40+
* Apply the stored info back to the item stack. If possible [stack] should be modified and returned directly,
41+
* instead of creating a new [ItemStack] instance. Information stored here should be recovered using [fromStack].
42+
*/
3943
abstract fun applyToStack(stack: ItemStack, store: SBItemData, value: T?): ItemStack
40-
fun bindWith(data: T) = BoundState(this, data)
44+
fun bind(data: T) = BoundState(this, data)
4145
}
4246

4347
/**
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package moe.nea.firmament.repo.item
2+
3+
import com.google.auto.service.AutoService
4+
import net.minecraft.item.ItemStack
5+
import net.minecraft.nbt.NbtInt
6+
import moe.nea.firmament.repo.set
7+
import moe.nea.firmament.util.extraAttributes
8+
import moe.nea.firmament.util.mc.modifyLore
9+
import moe.nea.firmament.util.modifyExtraAttributes
10+
import moe.nea.firmament.util.skyblock.Rarity
11+
12+
@AutoService(SBItemProperty::class)
13+
object SBRecombobulator : SBItemProperty.State<Boolean>() {
14+
override fun applyToStack(
15+
stack: ItemStack,
16+
store: SBItemData,
17+
value: Boolean?
18+
): ItemStack {
19+
if (value != true) return stack
20+
stack.modifyLore { lore ->
21+
Rarity.recombobulateLore(lore)
22+
}
23+
stack.modifyExtraAttributes {
24+
it["rarity_upgrades"] = NbtInt.of(1)
25+
}
26+
return stack
27+
}
28+
29+
override fun fromStack(
30+
stack: ItemStack,
31+
store: SBItemData
32+
): Boolean? {
33+
return stack.extraAttributes.getInt("rarity_upgrades") > 0
34+
}
35+
36+
override val order: Int
37+
get() = -100
38+
}

src/main/kotlin/util/skyblock/Rarity.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ import net.minecraft.text.Text
1313
import net.minecraft.util.Formatting
1414
import moe.nea.firmament.util.StringUtil.words
1515
import moe.nea.firmament.util.collections.lastNotNullOfOrNull
16+
import moe.nea.firmament.util.directLiteralStringContent
1617
import moe.nea.firmament.util.mc.loreAccordingToNbt
1718
import moe.nea.firmament.util.petData
19+
import moe.nea.firmament.util.prepend
20+
import moe.nea.firmament.util.prependHypixelified
1821
import moe.nea.firmament.util.removeColorCodes
1922
import moe.nea.firmament.util.unformattedString
23+
import moe.nea.firmament.util.withColor
2024

2125
typealias RepoRarity = io.github.moulberry.repo.data.Rarity
2226

@@ -52,6 +56,10 @@ enum class Rarity(vararg altNames: String) {
5256
val text: Text get() = Text.literal(name).setStyle(Style.EMPTY.withColor(colourMap[this]))
5357
val neuRepoRarity: RepoRarity? = RepoRarity.entries.find { it.name == name }
5458

59+
fun recombobulate(): Rarity = Rarity.entries.getOrElse(ordinal + 1) { this }
60+
61+
fun colour() = colourMap[this] ?: Formatting.WHITE
62+
5563
companion object {
5664
// TODO: inline those formattings as fields
5765
val colourMap = mapOf(
@@ -94,11 +102,46 @@ enum class Rarity(vararg altNames: String) {
94102
}
95103
}
96104

105+
fun findLoreIndex(lore: List<Text>): Int {
106+
return lore.indexOfLast {
107+
it.unformattedString.words().any { fromString(it) != null }
108+
}
109+
}
110+
97111
fun fromLore(lore: List<Text>): Rarity? =
98112
lore.lastNotNullOfOrNull {
99113
it.unformattedString.words()
100114
.firstNotNullOfOrNull(::fromString)
101115
}
102116

117+
fun recombobulateLore(lore: List<Text>): List<Text> {
118+
val before = fromLore(lore) ?: return lore
119+
val rarityIndex = findLoreIndex(lore)
120+
if (rarityIndex < 0) return lore
121+
val after = before.recombobulate()
122+
val col = after.colour()
123+
val loreMut = lore.toMutableList()
124+
val obfuscatedTag = Text.literal("a")
125+
.withColor(col)
126+
.styled { it.withObfuscated(true) }
127+
val rarityLine = loreMut[rarityIndex].copy()
128+
.prependHypixelified(Text.literal(" "))
129+
.prepend(obfuscatedTag)
130+
.append(Text.literal(" "))
131+
.append(obfuscatedTag)
132+
(rarityLine.siblings as MutableList<Text>)
133+
.replaceAll {
134+
var content = it.directLiteralStringContent
135+
before.names.forEach {
136+
content = content?.replace(it, after.name)
137+
}
138+
val editedText = (if (content != it.directLiteralStringContent)
139+
Text.literal(content) else it.copy())
140+
editedText.withColor(col)
141+
}
142+
loreMut[rarityIndex] = rarityLine
143+
return loreMut
144+
}
145+
103146
}
104147
}

src/main/kotlin/util/textutil.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,22 @@ fun OrderedText.reconstitute(): MutableText {
5656
return base
5757

5858
}
59+
5960
fun StringVisitable.reconstitute(): MutableText {
6061
val base = Text.literal("")
6162
base.setStyle(Style.EMPTY.withItalic(false))
6263
var lastColorCode = Style.EMPTY
6364
val text = StringBuilder()
6465
this.visit({ style, string ->
65-
if (style != lastColorCode) {
66-
if (text.isNotEmpty())
67-
base.append(Text.literal(text.toString()).setStyle(lastColorCode))
68-
lastColorCode = style
69-
text.clear()
70-
}
71-
text.append(string)
72-
Optional.empty<Unit>()
73-
}, Style.EMPTY)
66+
if (style != lastColorCode) {
67+
if (text.isNotEmpty())
68+
base.append(Text.literal(text.toString()).setStyle(lastColorCode))
69+
lastColorCode = style
70+
text.clear()
71+
}
72+
text.append(string)
73+
Optional.empty<Unit>()
74+
}, Style.EMPTY)
7475
if (text.isNotEmpty())
7576
base.append(Text.literal(text.toString()).setStyle(lastColorCode))
7677
return base
@@ -127,7 +128,8 @@ fun MutableText.darkGrey() = withColor(Formatting.DARK_GRAY)
127128
fun MutableText.red() = withColor(Formatting.RED)
128129
fun MutableText.white() = withColor(Formatting.WHITE)
129130
fun MutableText.bold(): MutableText = styled { it.withBold(true) }
130-
fun MutableText.hover(text: Text): MutableText = styled {it.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, text))}
131+
fun MutableText.hover(text: Text): MutableText =
132+
styled { it.withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, text)) }
131133

132134

133135
fun MutableText.clickCommand(command: String): MutableText {
@@ -137,6 +139,12 @@ fun MutableText.clickCommand(command: String): MutableText {
137139
}
138140
}
139141

142+
fun MutableText.prependHypixelified(text: Text): MutableText {
143+
if (directLiteralStringContent == "")
144+
return prepend(text)
145+
return Text.literal("").styled { it.withItalic(false) }.append(this).prepend(text)
146+
}
147+
140148
fun MutableText.prepend(text: Text): MutableText {
141149
siblings.addFirst(text)
142150
return this

0 commit comments

Comments
 (0)