aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/modules
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-12-08 00:42:01 +0100
committerLinnea Gräf <nea@nea.moe>2024-12-08 00:42:01 +0100
commitfa72dd4ce107190cda7bc56234fed650f20e3aa9 (patch)
tree57a5fa0c224db5bb670e26813c958a768d6bdf90 /src/main/kotlin/moe/nea/ledger/modules
parent0a2342c015b310172f933df765d8651013c26f16 (diff)
downloadLocalTransactionLedger-fa72dd4ce107190cda7bc56234fed650f20e3aa9.tar.gz
LocalTransactionLedger-fa72dd4ce107190cda7bc56234fed650f20e3aa9.tar.bz2
LocalTransactionLedger-fa72dd4ce107190cda7bc56234fed650f20e3aa9.zip
feat: Add visitor trading detection
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/modules')
-rw-r--r--src/main/kotlin/moe/nea/ledger/modules/VisitorDetection.kt109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/modules/VisitorDetection.kt b/src/main/kotlin/moe/nea/ledger/modules/VisitorDetection.kt
new file mode 100644
index 0000000..1df0618
--- /dev/null
+++ b/src/main/kotlin/moe/nea/ledger/modules/VisitorDetection.kt
@@ -0,0 +1,109 @@
+package moe.nea.ledger.modules
+
+import moe.nea.ledger.ItemChange
+import moe.nea.ledger.ItemId
+import moe.nea.ledger.ItemIdProvider
+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.ExtraSupplyIdEvent
+import moe.nea.ledger.events.GuiClickEvent
+import moe.nea.ledger.getDisplayNameU
+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.Inject
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import java.time.Instant
+
+class VisitorDetection {
+ @Inject
+ lateinit var logger: LedgerLogger
+
+ @Inject
+ lateinit var idProvider: ItemIdProvider
+
+ @SubscribeEvent
+ fun parseFromItem(event: GuiClickEvent) {
+ val stack = event.slotIn?.stack ?: return
+
+ val displayName = stack.getDisplayNameU()
+ if (displayName != "§aAccept Offer") return
+ val lore = stack.getLore()
+ if (!lore.contains("§eClick to give!")) return
+
+ val rewards = lore
+ .asSequence()
+ .dropWhile { it != "§7Rewards:" }.drop(1)
+ .takeWhile { it != "" }
+ .mapNotNull { parseGardenLoreLine(it) }
+ .map { ItemChange.gain(it.first, it.second) }
+ .toList()
+
+ val cost = lore
+ .asSequence()
+ .dropWhile { it != "§7Items Required:" }.drop(1)
+ .takeWhile { it != "" }
+ .mapNotNull { parseGardenLoreLine(it) }
+ .map { ItemChange.lose(it.first, it.second) }
+ .toList()
+
+ logger.logEntry(LedgerEntry(
+ TransactionType.VISITOR_BARGAIN,
+ Instant.now(),
+ cost + rewards
+ ))
+ }
+
+ private fun parseGardenLoreLine(rewardLine: String): Pair<ItemId, Double>? {
+ val f = rewardLine.unformattedString().trim()
+ return parseSpecialReward(f)
+ ?: idProvider.findStackableItemByName(f, true)
+ }
+
+ private val specialRewardRegex = "\\+(?<amount>${SHORT_NUMBER_PATTERN})x? (?<what>.*)".toPattern()
+
+ private fun parseSpecialReward(specialLine: String): Pair<ItemId, Double>? {
+ specialRewardRegex.useMatcher(specialLine) {
+ val id = when (group("what")) {
+ "Copper" -> ItemId.COPPER
+ "Bits" -> ItemId.BITS
+ "Garden Experience" -> ItemId.GARDEN
+ "Farming XP" -> ItemId.FARMING
+ "Gold Essence" -> ItemId.GOLD_ESSENCE
+ "Gemstone Powder" -> ItemId.GEMSTONE_POWDER
+ "Mithril Powder" -> ItemId.MITHRIL_POWDER
+ "Pelts" -> ItemId.PELT
+ "Fine Flour" -> ItemId.FINE_FLOUR
+ else -> ItemId.NIL
+ }
+ return Pair(id, parseShortNumber(group("amount")))
+ }
+ return null
+ }
+
+
+ @SubscribeEvent
+ fun supplyNames(event: ExtraSupplyIdEvent) {
+ event.store("Carrot", ItemId("CARROT_ITEM"))
+ event.store("Potato", ItemId("POTATO_ITEM"))
+ event.store("Jacob's Ticket", ItemId("JACOBS_TICKET"))
+ event.store("Cocoa Beans", ItemId("INK_SACK:3"))
+ event.store("Enchanted Cocoa Beans", ItemId("ENCHANTED_COCOA"))
+ event.store("Enchanted Red Mushroom Block", ItemId("ENCHANTED_HUGE_MUSHROOM_2"))
+ event.store("Enchanted Brown Mushroom Block", ItemId("ENCHANTED_HUGE_MUSHROOM_1"))
+ event.store("Nether Wart", ItemId("NETHER_STALK"))
+ event.store("Enchanted Nether Wart", ItemId("ENCHANTED_NETHER_STALK"))
+ event.store("Mutant Nether Wart", ItemId("MUTANT_NETHER_STALK"))
+ event.store("Jack o' Lantern", ItemId("JACK_O_LANTERN"))
+ event.store("Cactus Green", ItemId("INK_SACK:2"))
+ event.store("Hay Bale", ItemId("HAY_BLOCK"))
+ event.store("Rabbit's Foot", ItemId("RABBIT_FOOT"))
+ event.store("Raw Porkchop", ItemId("PORK"))
+ event.store("Raw Rabbit", ItemId("RABBIT"))
+ event.store("White Wool", ItemId("WOOL"))
+ event.store("Copper Dye", ItemId("DYE_COPPER"))
+ }
+} \ No newline at end of file