aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/repo/SBItemStack.kt
blob: 4d078017b36918e35d2a5e72baec876c4c9ed8d6 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package moe.nea.firmament.repo

import com.mojang.serialization.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder
import io.github.moulberry.repo.constants.PetNumbers
import io.github.moulberry.repo.data.NEUIngredient
import io.github.moulberry.repo.data.NEUItem
import net.minecraft.item.ItemStack
import net.minecraft.network.RegistryByteBuf
import net.minecraft.network.codec.PacketCodec
import net.minecraft.network.codec.PacketCodecs
import net.minecraft.text.Style
import net.minecraft.text.Text
import net.minecraft.text.TextColor
import net.minecraft.util.Formatting
import moe.nea.firmament.repo.ItemCache.asItemStack
import moe.nea.firmament.repo.ItemCache.withFallback
import moe.nea.firmament.util.FirmFormatters
import moe.nea.firmament.util.LegacyFormattingCode
import moe.nea.firmament.util.ReforgeId
import moe.nea.firmament.util.SkyblockId
import moe.nea.firmament.util.directLiteralStringContent
import moe.nea.firmament.util.extraAttributes
import moe.nea.firmament.util.getReforgeId
import moe.nea.firmament.util.getUpgradeStars
import moe.nea.firmament.util.grey
import moe.nea.firmament.util.mc.appendLore
import moe.nea.firmament.util.mc.displayNameAccordingToNbt
import moe.nea.firmament.util.mc.loreAccordingToNbt
import moe.nea.firmament.util.petData
import moe.nea.firmament.util.prepend
import moe.nea.firmament.util.skyBlockId
import moe.nea.firmament.util.skyblock.ItemType
import moe.nea.firmament.util.skyblock.Rarity
import moe.nea.firmament.util.skyblockId
import moe.nea.firmament.util.useMatch
import moe.nea.firmament.util.withColor

data class SBItemStack constructor(
	val skyblockId: SkyblockId,
	val neuItem: NEUItem?,
	private var stackSize: Int,
	private var petData: PetData?,
	val extraLore: List<Text> = emptyList(),
	val stars: Int = 0,
	val fallback: ItemStack? = null,
	val reforge: ReforgeId? = null,
) {

	fun getStackSize() = stackSize
	fun setStackSize(newSize: Int) {
		this.stackSize = newSize
		this.itemStack_ = null
	}

	fun getPetData() = petData
	fun setPetData(petData: PetData?) {
		this.petData = petData
		this.itemStack_ = null
	}

	companion object {
		val PACKET_CODEC: PacketCodec<in RegistryByteBuf, SBItemStack> = PacketCodec.tuple(
			SkyblockId.PACKET_CODEC, { it.skyblockId },
			PacketCodecs.VAR_INT, { it.stackSize },
			{ id, count -> SBItemStack(id, count) }
		)
		val CODEC: Codec<SBItemStack> = RecordCodecBuilder.create {
			it.group(
				SkyblockId.CODEC.fieldOf("skyblockId").forGetter { it.skyblockId },
				Codec.INT.fieldOf("count").forGetter { it.stackSize },
			).apply(it) { id, count ->
				SBItemStack(id, count)
			}
		}
		val EMPTY = SBItemStack(SkyblockId.NULL, 0)

		operator fun invoke(itemStack: ItemStack): SBItemStack {
			val skyblockId = itemStack.skyBlockId ?: SkyblockId.NULL
			return SBItemStack(
				skyblockId,
				RepoManager.getNEUItem(skyblockId),
				itemStack.count,
				petData = itemStack.petData?.let { PetData.fromHypixel(it) },
				stars = itemStack.getUpgradeStars(),
				reforge = itemStack.getReforgeId()
			)
		}

		operator fun invoke(neuIngredient: NEUIngredient): SBItemStack? {
			if (neuIngredient.skyblockId == SkyblockId.SENTINEL_EMPTY) return null // TODO: better fallback, maybe?
			if (neuIngredient.skyblockId == SkyblockId.COINS) {
				// TODO: specially handle coins to include the decimals
			}
			return SBItemStack(neuIngredient.skyblockId, neuIngredient.amount.toInt())
		}

		fun passthrough(itemStack: ItemStack): SBItemStack {
			return SBItemStack(SkyblockId.NULL, null, itemStack.count, null, fallback = itemStack)
		}

		fun appendEnhancedStats(
			itemStack: ItemStack,
			reforgeStats: Map<String, Double>,
			buffKind: BuffKind,
		) {
			val namedReforgeStats = reforgeStats
				.mapKeysTo(mutableMapOf()) { statIdToName(it.key) }
			val loreMut = itemStack.loreAccordingToNbt.toMutableList()
			var statBlockLastIndex = -1
			for (i in loreMut.indices) {
				val statLine = parseStatLine(loreMut[i])
				if (statLine == null && statBlockLastIndex >= 0) {
					break
				}
				if (statLine == null) {
					continue
				}
				statBlockLastIndex = i
				val statBuff = namedReforgeStats.remove(statLine.statName) ?: continue
				loreMut[i] = statLine.addStat(statBuff, buffKind).reconstitute()
			}
			if (namedReforgeStats.isNotEmpty() && statBlockLastIndex == -1) {
				loreMut.add(0, Text.literal(""))
			}
			// If there is no stat block the statBlockLastIndex falls through to -1
			// TODO: this is good enough for some items. some other items might have their stats at a different place.
			for ((statName, statBuff) in namedReforgeStats) {
				val statLine = StatLine(statName, null).addStat(statBuff, buffKind)
				loreMut.add(statBlockLastIndex + 1, statLine.reconstitute())
			}
			itemStack.loreAccordingToNbt = loreMut
		}

		data class StatFormatting(
			val postFix: String,
			val color: Formatting,
		)

		val formattingOverrides = mapOf(
			"Sea Creature Chance" to StatFormatting("%", Formatting.RED),
			"Strength" to StatFormatting("", Formatting.RED),
			"Damage" to StatFormatting("", Formatting.RED),
			"Bonus Attack Speed" to StatFormatting("%", Formatting.RED),
			"Shot Cooldown" to StatFormatting("s", Formatting.RED),
			"Ability Damage" to StatFormatting("%", Formatting.RED),
			"Crit Damage" to StatFormatting("%", Formatting.RED),
			"Crit Chance" to StatFormatting("%", Formatting.RED),
			"Ability Damage" to StatFormatting("%", Formatting.RED),
			"Trophy Fish Chance" to StatFormatting("%", Formatting.GREEN),
			"Health" to StatFormatting("", Formatting.GREEN),
			"Defense" to StatFormatting("", Formatting.GREEN),
			"Fishing Speed" to StatFormatting("", Formatting.GREEN),
			"Double Hook Chance" to StatFormatting("%", Formatting.GREEN),
			"Mining Speed" to StatFormatting("", Formatting.GREEN),
			"Mining Fortune" to StatFormatting("", Formatting.GREEN),
			"Heat Resistance" to StatFormatting("", Formatting.GREEN),
			"Swing Range" to StatFormatting("", Formatting.GREEN),
			"Rift Time" to StatFormatting("", Formatting.GREEN),
			"Speed" to StatFormatting("", Formatting.GREEN),
			"Farming Fortune" to StatFormatting("", Formatting.GREEN),
			"True Defense" to StatFormatting("", Formatting.GREEN),
			"Mending" to StatFormatting("", Formatting.GREEN),
			"Foraging Wisdom" to StatFormatting("", Formatting.GREEN),
			"Farming Wisdom" to StatFormatting("", Formatting.GREEN),
			"Foraging Fortune" to StatFormatting("", Formatting.GREEN),
			"Magic Find" to StatFormatting("", Formatting.GREEN),
			"Ferocity" to StatFormatting("", Formatting.GREEN),
			"Bonus Pest Chance" to StatFormatting("%", Formatting.GREEN),
			"Cold Resistance" to StatFormatting("", Formatting.GREEN),
			"Pet Luck" to StatFormatting("", Formatting.GREEN),
			"Fear" to StatFormatting("", Formatting.GREEN),
			"Mana Regen" to StatFormatting("%", Formatting.GREEN),
			"Rift Damage" to StatFormatting("", Formatting.GREEN),
			"Hearts" to StatFormatting("", Formatting.GREEN),
			"Vitality" to StatFormatting("", Formatting.GREEN),
			// TODO: make this a repo json
		)


		private val statLabelRegex = "(?<statName>.*): ".toPattern()

		enum class BuffKind(
			val color: Formatting,
			val prefix: String,
			val postFix: String,
		) {
			REFORGE(Formatting.BLUE, "(", ")"),

			;
		}

		data class StatLine(
			val statName: String,
			val value: Text?,
			val rest: List<Text> = listOf(),
			val valueNum: Double? = value?.directLiteralStringContent?.trim(' ', '%', '+')?.toDoubleOrNull()
		) {
			fun addStat(amount: Double, buffKind: BuffKind): StatLine {
				val formattedAmount = FirmFormatters.formatCommas(amount, 1, includeSign = true)
				return copy(
					valueNum = (valueNum ?: 0.0) + amount,
					value = null,
					rest = rest +
						listOf(
							Text.literal(
								buffKind.prefix + formattedAmount +
									statFormatting.postFix +
									buffKind.postFix + " ")
								.withColor(buffKind.color)))
			}

			fun formatValue() =
				Text.literal(FirmFormatters.formatCommas(valueNum ?: 0.0,
				                                         1,
				                                         includeSign = true) + statFormatting.postFix + " ")
					.setStyle(Style.EMPTY.withColor(statFormatting.color))

			val statFormatting = formattingOverrides[statName] ?: StatFormatting("", Formatting.GREEN)
			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))
				}
			}

			fun reconstitute(abbreviateTo: Int = Int.MAX_VALUE): Text =
				Text.literal("").setStyle(Style.EMPTY.withItalic(false))
					.append(Text.literal("${abbreviate(abbreviateTo)}: ").grey())
					.append(value ?: formatValue())
					.also { rest.forEach(it::append) }
		}

		fun statIdToName(statId: String): String {
			val segments = statId.split("_")
			return segments.joinToString(" ") { it.replaceFirstChar { it.uppercaseChar() } }
		}

		private fun parseStatLine(line: Text): StatLine? {
			val sibs = line.siblings
			val stat = sibs.firstOrNull() ?: 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(statName, sibs[1], sibs.subList(2, sibs.size))
		}
	}

	constructor(skyblockId: SkyblockId, petData: PetData) : this(
		skyblockId,
		RepoManager.getNEUItem(skyblockId),
		1,
		petData
	)

	constructor(skyblockId: SkyblockId, stackSize: Int = 1) : this(
		skyblockId,
		RepoManager.getNEUItem(skyblockId),
		stackSize,
		RepoManager.getPotentialStubPetData(skyblockId)
	)

	private fun injectReplacementDataForPetLevel(
		petInfo: PetNumbers,
		level: Int,
		replacementData: MutableMap<String, String>
	) {
		val stats = petInfo.interpolatedStatsAtLevel(level) ?: return
		stats.otherNumbers.forEachIndexed { index, it ->
			replacementData[index.toString()] = FirmFormatters.formatCommas(it, 1)
		}
		stats.statNumbers.forEach { (t, u) ->
			replacementData[t] = FirmFormatters.formatCommas(u, 1)
		}
	}

	private fun injectReplacementDataForPets(replacementData: MutableMap<String, String>) {
		val petData = this.petData ?: return
		val petInfo = RepoManager.neuRepo.constants.petNumbers[petData.petId]?.get(petData.rarity) ?: return
		if (petData.isStub) {
			val mapLow = mutableMapOf<String, String>()
			injectReplacementDataForPetLevel(petInfo, petInfo.lowLevel, mapLow)
			val mapHigh = mutableMapOf<String, String>()
			injectReplacementDataForPetLevel(petInfo, petInfo.highLevel, mapHigh)
			mapHigh.forEach { (key, highValue) ->
				mapLow.merge(key, highValue) { a, b -> "$a$b" }
			}
			replacementData.putAll(mapLow)
			replacementData["LVL"] = "${petInfo.lowLevel}${petInfo.highLevel}"
		} else {
			injectReplacementDataForPetLevel(petInfo, petData.levelData.currentLevel, replacementData)
			replacementData["LVL"] = petData.levelData.currentLevel.toString()
		}
	}


	private fun appendReforgeInfo(
		itemStack: ItemStack,
	) {
		val rarity = Rarity.fromItem(itemStack) ?: return
		val reforgeId = this.reforge ?: return
		val reforge = ReforgeStore.modifierLut[reforgeId] ?: return
		val reforgeStats = reforge.reforgeStats?.get(rarity) ?: mapOf()
		itemStack.displayNameAccordingToNbt = itemStack.displayNameAccordingToNbt.copy()
			.prepend(Text.literal(reforge.reforgeName + " ").formatted(Rarity.colourMap[rarity] ?: Formatting.WHITE))
		val data = itemStack.extraAttributes.copy()
		data.putString("modifier", reforgeId.id)
		itemStack.extraAttributes = data
		appendEnhancedStats(itemStack, reforgeStats, BuffKind.REFORGE)
	}

	// TODO: avoid instantiating the item stack here
	val itemType: ItemType? get() = ItemType.fromItemStack(asImmutableItemStack())
	val rarity: Rarity? get() = Rarity.fromItem(asImmutableItemStack())

	private var itemStack_: ItemStack? = null

	private val itemStack: ItemStack
		get() {
			val itemStack = itemStack_ ?: run {
				if (skyblockId == SkyblockId.COINS)
					return@run ItemCache.coinItem(stackSize).also { it.appendLore(extraLore) }
				if (stackSize == 0)
					return@run ItemStack.EMPTY
				val replacementData = mutableMapOf<String, String>()
				injectReplacementDataForPets(replacementData)
				return@run neuItem.asItemStack(idHint = skyblockId, replacementData)
					.withFallback(fallback)
					.copyWithCount(stackSize)
					.also { appendReforgeInfo(it) }
					.also { it.appendLore(extraLore) }
					.also { enhanceStatsByStars(it, stars) }
			}
			if (itemStack_ == null)
				itemStack_ = itemStack
			return itemStack
		}


	private fun starString(stars: Int): Text {
		if (stars <= 0) return Text.empty()
		val tiers = listOf(
			LegacyFormattingCode.GOLD,
			LegacyFormattingCode.LIGHT_PURPLE,
			LegacyFormattingCode.AQUA,
		)
		val maxStars = 5
		if (stars > tiers.size * maxStars) return Text.literal(" ${stars}✪").withColor(Formatting.RED)
		val starBaseTier = (stars - 1) / maxStars
		val starBaseColor = tiers[starBaseTier]
		val starsInCurrentTier = stars - starBaseTier * maxStars
		val starString = Text.literal(" " + "✪".repeat(starsInCurrentTier)).withColor(starBaseColor.modern)
		if (starBaseTier > 0) {
			val starLastTier = tiers[starBaseTier - 1]
			val starsInLastTier = 5 - starsInCurrentTier
			starString.append(Text.literal("✪".repeat(starsInLastTier)).withColor(starLastTier.modern))
		}
		return starString
	}

	private fun enhanceStatsByStars(itemStack: ItemStack, stars: Int) {
		if (stars == 0) return
		// TODO: increase stats and add the star level into the nbt data so star displays work
		itemStack.displayNameAccordingToNbt = itemStack.displayNameAccordingToNbt.copy()
			.append(starString(stars))
	}

	fun asImmutableItemStack(): ItemStack {
		return itemStack
	}

	fun asCopiedItemStack(): ItemStack {
		return itemStack.copy()
	}
}