aboutsummaryrefslogtreecommitdiff
path: root/mod/src/main/kotlin/moe/nea/ledger/modules/DungeonChestDetection.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-01-08 19:25:29 +0100
committerLinnea Gräf <nea@nea.moe>2025-01-08 19:25:29 +0100
commitd1e16a47819509ed645bb93e1a173e0a97025cef (patch)
treeefbe886d9ac1ab4ea01788cb4842812fd0af9079 /mod/src/main/kotlin/moe/nea/ledger/modules/DungeonChestDetection.kt
parentf694daf322bbb4ff530a9332547c5c8337c3e0c0 (diff)
downloadLocalTransactionLedger-d1e16a47819509ed645bb93e1a173e0a97025cef.tar.gz
LocalTransactionLedger-d1e16a47819509ed645bb93e1a173e0a97025cef.tar.bz2
LocalTransactionLedger-d1e16a47819509ed645bb93e1a173e0a97025cef.zip
build: Move mod to subproject
Diffstat (limited to 'mod/src/main/kotlin/moe/nea/ledger/modules/DungeonChestDetection.kt')
-rw-r--r--mod/src/main/kotlin/moe/nea/ledger/modules/DungeonChestDetection.kt95
1 files changed, 95 insertions, 0 deletions
diff --git a/mod/src/main/kotlin/moe/nea/ledger/modules/DungeonChestDetection.kt b/mod/src/main/kotlin/moe/nea/ledger/modules/DungeonChestDetection.kt
new file mode 100644
index 0000000..e747be9
--- /dev/null
+++ b/mod/src/main/kotlin/moe/nea/ledger/modules/DungeonChestDetection.kt
@@ -0,0 +1,95 @@
+package moe.nea.ledger.modules
+
+import moe.nea.ledger.ExpiringValue
+import moe.nea.ledger.ItemChange
+import moe.nea.ledger.LedgerEntry
+import moe.nea.ledger.LedgerLogger
+import moe.nea.ledger.TransactionType
+import moe.nea.ledger.events.ChatReceived
+import moe.nea.ledger.events.ExtraSupplyIdEvent
+import moe.nea.ledger.events.GuiClickEvent
+import moe.nea.ledger.gen.ItemIds
+import moe.nea.ledger.getDisplayNameU
+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 java.time.Instant
+import java.util.concurrent.locks.ReentrantLock
+import kotlin.time.Duration.Companion.seconds
+
+class DungeonChestDetection @Inject constructor(val logger: LedgerLogger) : ChestDetection() {
+
+ @SubscribeEvent
+ fun onKismetClick(event: GuiClickEvent) {
+ val slot = event.slotIn ?: return
+ if (!slot.inventory.displayName.unformattedText.unformattedString().endsWith(" Chest")) return
+ val stack = slot.stack ?: return
+ if (stack.getDisplayNameU() == "§aReroll Chest") {
+ logger.logEntry(
+ LedgerEntry(
+ TransactionType.KISMET_REROLL,
+ Instant.now(),
+ listOf(
+ ItemChange.lose(ItemIds.KISMET_FEATHER, 1)
+ )
+ )
+ )
+ }
+ }
+
+
+ var lastOpenedChest = ExpiringValue.empty<ChestCost>()
+
+ @SubscribeEvent
+ fun supplyExtraIds(event: ExtraSupplyIdEvent) {
+ event.store("Dungeon Chest Key", ItemIds.DUNGEON_CHEST_KEY)
+ event.store("Kismet Feather", ItemIds.KISMET_FEATHER)
+ }
+
+ @SubscribeEvent
+ fun onRewardChestClick(event: GuiClickEvent) {
+ lastOpenedChest = ExpiringValue(scrapeChestReward(event.slotIn ?: return) ?: return)
+ }
+
+ class Mutex<T>(defaultValue: T) {
+ private var value: T = defaultValue
+ val lock = ReentrantLock()
+
+ fun getUnsafeLockedValue(): T {
+ if (!lock.isHeldByCurrentThread)
+ error("Accessed unsafe locked value, without holding the lock.")
+ return value
+ }
+
+ fun <R> withLock(func: (T) -> R): R {
+ lock.lockInterruptibly()
+ try {
+ val ret = func(value)
+ if (ret === value) {
+ error("Please don't smuggle out the locked value. If this is unintentional, please append a `Unit` instruction to the end of your `withLock` call: `.withLock { /* your existing code */; Unit }`.")
+ }
+ return ret
+ } finally {
+ lock.unlock()
+ }
+ }
+ }
+
+ val rewardMessage = " (WOOD|GOLD|DIAMOND|EMERALD|OBSIDIAN|BEDROCK) CHEST REWARDS".toPattern()
+
+ @SubscribeEvent
+ fun onChatMessage(event: ChatReceived) {
+ if (event.message == "You don't have that many coins in the bank!") {
+ lastOpenedChest.take()
+ }
+ rewardMessage.useMatcher(event.message) {
+ val chest = lastOpenedChest.consume(3.seconds) ?: return
+ logger.logEntry(LedgerEntry(
+ TransactionType.DUNGEON_CHEST_OPEN,
+ chest.timestamp,
+ chest.diff,
+ ))
+ }
+ }
+}