aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/kotlin/moe/nea/ledger/ExpiringValue.kt28
-rw-r--r--src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt74
-rw-r--r--src/main/kotlin/moe/nea/ledger/Ledger.kt1
-rw-r--r--src/main/kotlin/moe/nea/ledger/MinionDetection.kt42
-rw-r--r--src/main/kotlin/moe/nea/ledger/NumberUtil.kt68
5 files changed, 161 insertions, 52 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/ExpiringValue.kt b/src/main/kotlin/moe/nea/ledger/ExpiringValue.kt
new file mode 100644
index 0000000..dac4751
--- /dev/null
+++ b/src/main/kotlin/moe/nea/ledger/ExpiringValue.kt
@@ -0,0 +1,28 @@
+package moe.nea.ledger
+
+import kotlin.time.Duration
+import kotlin.time.Duration.Companion.nanoseconds
+
+class ExpiringValue<T>(private val value: T) {
+ val lastSeenAt: Long = System.nanoTime()
+ val age get() = (System.nanoTime() - lastSeenAt).nanoseconds
+ var taken = false
+ fun get(expiry: Duration): T? {
+ return if (!taken && expiry > age) value
+ else null
+ }
+
+ companion object {
+ fun <T> empty(): ExpiringValue<T> {
+ val value = ExpiringValue(Unit)
+ value.take()
+ @Suppress("UNCHECKED_CAST")
+ return value as ExpiringValue<T>
+ }
+ }
+
+ fun consume(expiry: Duration): T? = get(expiry).also { take() }
+ fun take() {
+ taken = true
+ }
+} \ No newline at end of file
diff --git a/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt b/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
index 3da0a07..fa0d8fa 100644
--- a/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
+++ b/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
@@ -6,44 +6,46 @@ import net.minecraft.nbt.NBTTagCompound
import net.minecraftforge.client.event.GuiScreenEvent
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import org.lwjgl.input.Mouse
class ItemIdProvider {
- @SubscribeEvent
- fun onMouseInput(event: GuiScreenEvent.MouseInputEvent.Pre) {
- MinecraftForge.EVENT_BUS.post(BeforeGuiAction(event.gui))
- }
-
- @SubscribeEvent
- fun onKeyInput(event: GuiScreenEvent.KeyboardInputEvent.Pre) {
- MinecraftForge.EVENT_BUS.post(BeforeGuiAction(event.gui))
- }
-
- private val knownNames = mutableMapOf<String, String>()
-
- @SubscribeEvent
- fun saveInventoryIds(event: BeforeGuiAction) {
- val chest = (event.gui as? GuiChest) ?: return
- val slots = chest.inventorySlots as ContainerChest
- val chestName = slots.lowerChestInventory.name.unformattedString()
- val isOrderMenu = chestName == "Your Bazaar Orders" || chestName == "Co-op Bazaar Orders"
- slots.inventorySlots.forEach {
- val stack = it.stack ?: return@forEach
- val nbt = stack.tagCompound ?: NBTTagCompound()
- val display = nbt.getCompoundTag("display")
- var name = display.getString("Name").unformattedString()
- if (isOrderMenu)
- name = name.removePrefix("BUY ").removePrefix("SELL ")
- name = name.trim()
- val id = stack.getInternalId()
- if (id != null && name.isNotBlank()) {
- knownNames[name] = id
- }
- }
- }
-
- fun findForName(name: String): String? {
- return knownNames[name]
- }
+ @SubscribeEvent
+ fun onMouseInput(event: GuiScreenEvent.MouseInputEvent.Pre) {
+ if (Mouse.getEventButton() == -1) return
+ MinecraftForge.EVENT_BUS.post(BeforeGuiAction(event.gui))
+ }
+
+ @SubscribeEvent
+ fun onKeyInput(event: GuiScreenEvent.KeyboardInputEvent.Pre) {
+ MinecraftForge.EVENT_BUS.post(BeforeGuiAction(event.gui))
+ }
+
+ private val knownNames = mutableMapOf<String, String>()
+
+ @SubscribeEvent
+ fun saveInventoryIds(event: BeforeGuiAction) {
+ val chest = (event.gui as? GuiChest) ?: return
+ val slots = chest.inventorySlots as ContainerChest
+ val chestName = slots.lowerChestInventory.name.unformattedString()
+ val isOrderMenu = chestName == "Your Bazaar Orders" || chestName == "Co-op Bazaar Orders"
+ slots.inventorySlots.forEach {
+ val stack = it.stack ?: return@forEach
+ val nbt = stack.tagCompound ?: NBTTagCompound()
+ val display = nbt.getCompoundTag("display")
+ var name = display.getString("Name").unformattedString()
+ if (isOrderMenu)
+ name = name.removePrefix("BUY ").removePrefix("SELL ")
+ name = name.trim()
+ val id = stack.getInternalId()
+ if (id != null && name.isNotBlank()) {
+ knownNames[name] = id
+ }
+ }
+ }
+
+ fun findForName(name: String): String? {
+ return knownNames[name]
+ }
} \ No newline at end of file
diff --git a/src/main/kotlin/moe/nea/ledger/Ledger.kt b/src/main/kotlin/moe/nea/ledger/Ledger.kt
index 96cddab..784c111 100644
--- a/src/main/kotlin/moe/nea/ledger/Ledger.kt
+++ b/src/main/kotlin/moe/nea/ledger/Ledger.kt
@@ -106,6 +106,7 @@ class Ledger {
AuctionHouseDetection(ledger, ids),
BitsDetection(ledger),
BitsShop(ledger),
+ MinionDetection(ledger),
).forEach(MinecraftForge.EVENT_BUS::register)
}
diff --git a/src/main/kotlin/moe/nea/ledger/MinionDetection.kt b/src/main/kotlin/moe/nea/ledger/MinionDetection.kt
new file mode 100644
index 0000000..bb2a187
--- /dev/null
+++ b/src/main/kotlin/moe/nea/ledger/MinionDetection.kt
@@ -0,0 +1,42 @@
+package moe.nea.ledger
+
+import net.minecraft.client.gui.inventory.GuiChest
+import net.minecraft.inventory.ContainerChest
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import java.time.Instant
+import kotlin.time.Duration.Companion.seconds
+
+class MinionDetection(val ledger: LedgerLogger) {
+ // §aYou received §r§6367,516.8 coins§r§a!
+ val hopperCollectPattern = "You received (?<amount>$SHORT_NUMBER_PATTERN) coins?!".toPattern()
+ val minionNamePattern = "(?<name>.*) Minion (?<level>$ROMAN_NUMBER_PATTERN)".toPattern()
+
+ var lastOpenedMinion = ExpiringValue.empty<String>()
+
+ @SubscribeEvent
+ fun onBeforeClaim(event: BeforeGuiAction) {
+ val container = event.gui as? GuiChest ?: return
+ val inv = (container.inventorySlots as ContainerChest).lowerChestInventory
+ val invName = inv.displayName.unformattedText.unformattedString()
+ minionNamePattern.useMatcher(invName) {
+ val name = group("name")
+ val level = parseRomanNumber(group("level"))
+ lastOpenedMinion = ExpiringValue(name.uppercase().replace(" ", "_") + "_" + level)
+ }
+ }
+
+
+ @SubscribeEvent
+ fun onChat(event: ChatReceived) {
+ hopperCollectPattern.useMatcher(event.message) {
+ val minionName = lastOpenedMinion.consume(3.seconds)
+ ledger.logEntry(LedgerEntry(
+ "AUTOMERCHANT_PROFIT_COLLECT",
+ Instant.now(),
+ parseShortNumber(group("amount")),
+ minionName, // TODO: switch to its own column idk
+ ))
+ }
+ }
+
+} \ No newline at end of file
diff --git a/src/main/kotlin/moe/nea/ledger/NumberUtil.kt b/src/main/kotlin/moe/nea/ledger/NumberUtil.kt
index d26b047..b0e47db 100644
--- a/src/main/kotlin/moe/nea/ledger/NumberUtil.kt
+++ b/src/main/kotlin/moe/nea/ledger/NumberUtil.kt
@@ -6,28 +6,64 @@ import java.util.regex.Pattern
// language=regexp
val SHORT_NUMBER_PATTERN = "[0-9]+(?:,[0-9]+)*(?:\\.[0-9]+)?[kKmMbB]?"
+// language=regexp
+val ROMAN_NUMBER_PATTERN = "[IVXLCDM]+"
+
+val romanNumbers = mapOf(
+ 'I' to 1,
+ 'V' to 5,
+ 'X' to 10,
+ 'L' to 50,
+ 'C' to 100,
+ 'D' to 500,
+ 'M' to 1000
+)
+
+fun parseRomanNumber(string: String): Int {
+ var smallestSeenSoFar = Int.MAX_VALUE
+ var lastSeenOfSmallest = 0
+ var amount = 0
+ for (c in string) {
+ val cV = romanNumbers[c]!!
+ if (cV == smallestSeenSoFar) {
+ lastSeenOfSmallest++
+ amount += cV
+ } else if (cV < smallestSeenSoFar) {
+ smallestSeenSoFar = cV
+ amount += cV
+ lastSeenOfSmallest = 1
+ } else {
+ amount -= lastSeenOfSmallest * smallestSeenSoFar * 2
+ smallestSeenSoFar = cV
+ amount += cV
+ lastSeenOfSmallest = 1
+ }
+ }
+ return amount
+}
+
val siScalars = mapOf(
- 'k' to 1_000.0,
- 'K' to 1_000.0,
- 'm' to 1_000_000.0,
- 'M' to 1_000_000.0,
- 'b' to 1_000_000_000.0,
- 'B' to 1_000_000_000.0,
+ 'k' to 1_000.0,
+ 'K' to 1_000.0,
+ 'm' to 1_000_000.0,
+ 'M' to 1_000_000.0,
+ 'b' to 1_000_000_000.0,
+ 'B' to 1_000_000_000.0,
)
fun parseShortNumber(string: String): Double {
- var k = string.replace(",", "")
- val scalar = k.last()
- var scalarMultiplier = siScalars[scalar]
- if (scalarMultiplier == null) {
- scalarMultiplier = 1.0
- } else {
- k = k.dropLast(1)
- }
- return k.toDouble() * scalarMultiplier
+ var k = string.replace(",", "")
+ val scalar = k.last()
+ var scalarMultiplier = siScalars[scalar]
+ if (scalarMultiplier == null) {
+ scalarMultiplier = 1.0
+ } else {
+ k = k.dropLast(1)
+ }
+ return k.toDouble() * scalarMultiplier
}
inline fun <T> Pattern.useMatcher(string: String, block: Matcher.() -> T): T? =
- matcher(string).takeIf { it.matches() }?.let(block)
+ matcher(string).takeIf { it.matches() }?.let(block)
fun String.unformattedString(): String = replace("§.".toRegex(), "") \ No newline at end of file