diff options
author | Linnea Gräf <nea@nea.moe> | 2024-04-18 13:25:38 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-04-18 13:25:38 +0200 |
commit | c405a779bb12dd8e07d85623ce5e1e2ad903afe1 (patch) | |
tree | 1ed2f5ff65dbe996c58f57e64ee99c4216714246 | |
parent | e1e67eb83e2ae35e770fb0612a4b99dd29f869da (diff) | |
download | money-ledger-c405a779bb12dd8e07d85623ce5e1e2ad903afe1.tar.gz money-ledger-c405a779bb12dd8e07d85623ce5e1e2ad903afe1.tar.bz2 money-ledger-c405a779bb12dd8e07d85623ce5e1e2ad903afe1.zip |
Add dungeon chest opening event
-rw-r--r-- | src/main/java/moe/nea/ledger/DungeonChestDetection.kt | 75 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/ItemUtil.kt | 7 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/Ledger.kt | 1 |
3 files changed, 83 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/ledger/DungeonChestDetection.kt b/src/main/java/moe/nea/ledger/DungeonChestDetection.kt new file mode 100644 index 0000000..1cdc2d9 --- /dev/null +++ b/src/main/java/moe/nea/ledger/DungeonChestDetection.kt @@ -0,0 +1,75 @@ +package moe.nea.ledger + +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import net.minecraftforge.fml.common.gameevent.TickEvent +import java.time.Instant +import java.util.regex.Pattern + +class DungeonChestDetection(val logger: LedgerLogger) { + + /*{ + id: "minecraft:chest", + Count: 1b, + tag: { + display: { + Lore: ["§7Purchase this chest to receive the", "§7rewards above. You can only open", "§7one chest per Dungeons run -", "§7choose wisely!", "", "§7Cost", "§625,000 Coins", "§9Dungeon Chest Key", "", "§7§cNOTE: Coins are withdrawn from your", "§cbank if you don't have enough in", "§cyour purse."], + Name: "§aOpen Reward Chest" + } + }, + Damage: 0s + }*/ + val costPattern = Pattern.compile("(?<cost>$SHORT_NUMBER_PATTERN) Coins") + + + data class ChestCost( + val cost: Double, + val openTimestamp: Long, + val hasKey: Boolean, + ) + + var lastOpenedChest: ChestCost? = null + + @SubscribeEvent + fun onSlotClick(event: GuiClickEvent) { + val slot = event.slotIn ?: return + if (!slot.inventory.displayName.unformattedText.unformattedString().endsWith(" Chest")) return + val stack = slot.stack ?: return + val name = stack.getDisplayNameU() + if (name != "§aOpen Reward Chest") return + val lore = stack.getLore() + val costIndex = lore.indexOf("§7Cost") + if (costIndex < 0 || costIndex + 1 !in lore.indices) return + val cost = costPattern.useMatcher(lore[costIndex + 1].unformattedString()) { + parseShortNumber(group("cost")) + } ?: 0.0 // Free chest! + val hasKey = lore.contains("§9Dungeon Chest Key") + lastOpenedChest?.let(::completeTransaction) + lastOpenedChest = ChestCost(cost, System.currentTimeMillis(), hasKey) + } + + @SubscribeEvent + fun onChatMessage(event: ChatReceived) { + if (event.message == "You don't have that many coins in the bank!") + lastOpenedChest = null + } + + fun completeTransaction(toOpen: ChestCost) { + lastOpenedChest = null + logger.logEntry( + LedgerEntry( + "DUNGEON_CHEST_OPEN", + Instant.ofEpochMilli(toOpen.openTimestamp), + toOpen.cost, + itemId = if (toOpen.hasKey) "DUNGEON_CHEST_KEY" else null + ) + ) + } + + @SubscribeEvent + fun onTick(event: TickEvent) { + val toOpen = lastOpenedChest + if (toOpen != null && toOpen.openTimestamp + 1000L < System.currentTimeMillis()) { + completeTransaction(toOpen) + } + } +} diff --git a/src/main/kotlin/moe/nea/ledger/ItemUtil.kt b/src/main/kotlin/moe/nea/ledger/ItemUtil.kt index 7157386..892127d 100644 --- a/src/main/kotlin/moe/nea/ledger/ItemUtil.kt +++ b/src/main/kotlin/moe/nea/ledger/ItemUtil.kt @@ -19,3 +19,10 @@ fun ItemStack.getLore(): List<String> { return (0 until lore.tagCount()).map { lore.getStringTagAt(it) } } + +fun ItemStack.getDisplayNameU(): String { + val nbt = this.tagCompound ?: NBTTagCompound() + val extraAttributes = nbt.getCompoundTag("display") + return extraAttributes.getString("Name") +} + diff --git a/src/main/kotlin/moe/nea/ledger/Ledger.kt b/src/main/kotlin/moe/nea/ledger/Ledger.kt index bf3ac9e..3745f36 100644 --- a/src/main/kotlin/moe/nea/ledger/Ledger.kt +++ b/src/main/kotlin/moe/nea/ledger/Ledger.kt @@ -56,6 +56,7 @@ class Ledger { ledger, BankDetection(ledger), BazaarDetection(ledger, ids), + DungeonChestDetection(ledger), BazaarOrderDetection(ledger, ids), AuctionHouseDetection(ledger, ids), BitsDetection(ledger), |