aboutsummaryrefslogtreecommitdiff
path: root/mod/src/main/kotlin/moe/nea/ledger/modules/BasicReforgeDetection.kt
blob: 17e2983654ce469f5324ec38a902db523403527e (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
package moe.nea.ledger.modules

import moe.nea.ledger.ExpiringValue
import moe.nea.ledger.ItemChange
import moe.nea.ledger.ItemId
import moe.nea.ledger.LedgerEntry
import moe.nea.ledger.LedgerLogger
import moe.nea.ledger.SHORT_NUMBER_PATTERN
import moe.nea.ledger.TransactionType
import moe.nea.ledger.events.ChatReceived
import moe.nea.ledger.events.GuiClickEvent
import moe.nea.ledger.getDisplayNameU
import moe.nea.ledger.getInternalId
import moe.nea.ledger.getLore
import moe.nea.ledger.parseShortNumber
import moe.nea.ledger.unformattedString
import moe.nea.ledger.useMatcher
import moe.nea.ledger.utils.di.Inject
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.seconds

class BasicReforgeDetection {

	var costPattern = "(?<cost>$SHORT_NUMBER_PATTERN) Coins".toPattern()

	@Inject
	lateinit var logger: LedgerLogger

	data class ReforgeInstance(
		val price: Double,
		val item: ItemId,
	)

	var lastReforge = ExpiringValue.empty<ReforgeInstance>()

	@SubscribeEvent
	fun onReforgeClick(event: GuiClickEvent) {
		val slot = event.slotIn ?: return
		val displayName = slot.inventory.displayName.unformattedText
		if (!displayName.unformattedString().contains("Reforge Item") &&
			!displayName.unformattedString().startsWith("The Hex")
		) return
		val stack = slot.stack ?: return
		val cost = stack.getLore()
			.firstNotNullOfOrNull { costPattern.useMatcher(it.unformattedString()) { parseShortNumber(group("cost")) } }
			?: return

		if (stack.getDisplayNameU() == "§aReforge Item" || stack.getDisplayNameU() == "§aRandom Basic Reforge") {
			lastReforge = ExpiringValue(ReforgeInstance(cost, ItemId.NIL /*TODO: read out item stack that is being reforged to save it as a transformed item!*/))
		}
	}

	val reforgeChatNotification = "You reforged your .* into a .*!".toPattern()

	@SubscribeEvent
	fun onReforgeChat(event: ChatReceived) {
		reforgeChatNotification.useMatcher(event.message) {
			val reforge = lastReforge.get(3.seconds) ?: return
			logger.logEntry(
				LedgerEntry(
					TransactionType.BASIC_REFORGE,
					event.timestamp,
					listOf(
						ItemChange.loseCoins(reforge.price),
						ItemChange(reforge.item, 1.0, ItemChange.ChangeDirection.TRANSFORM)
					)
				)
			)
		}
	}
}