aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-02-15 18:41:10 +0100
committerLinnea Gräf <nea@nea.moe>2024-02-15 18:41:10 +0100
commit3e19b97b52fae7422f8b0654da9341bf8c2393cb (patch)
treedc4bf85f326342a154e78aff3d5b8fdd2c14024e
parente5d2966e05fcaca533c09ab5ec5bfd825848be02 (diff)
downloadmoney-ledger-3e19b97b52fae7422f8b0654da9341bf8c2393cb.tar.gz
money-ledger-3e19b97b52fae7422f8b0654da9341bf8c2393cb.tar.bz2
money-ledger-3e19b97b52fae7422f8b0654da9341bf8c2393cb.zip
Added item ids
-rw-r--r--src/main/java/moe/nea/ledger/BazaarDetection.kt6
-rw-r--r--src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt44
-rw-r--r--src/main/kotlin/moe/nea/ledger/Ledger.kt4
-rw-r--r--src/main/kotlin/moe/nea/ledger/NumberUtil.kt1
4 files changed, 51 insertions, 4 deletions
diff --git a/src/main/java/moe/nea/ledger/BazaarDetection.kt b/src/main/java/moe/nea/ledger/BazaarDetection.kt
index aa3aeba..9bcab30 100644
--- a/src/main/java/moe/nea/ledger/BazaarDetection.kt
+++ b/src/main/java/moe/nea/ledger/BazaarDetection.kt
@@ -3,7 +3,7 @@ package moe.nea.ledger
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.regex.Pattern
-class BazaarDetection(val ledger: LedgerLogger) {
+class BazaarDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) {
val instaBuyPattern =
Pattern.compile("\\[Bazaar\\] Bought (?<count>[0-9]+)x (?<what>.*) for (?<coins>$SHORT_NUMBER_PATTERN) coins!")
@@ -19,7 +19,7 @@ class BazaarDetection(val ledger: LedgerLogger) {
"BAZAAR_BUY_INSTANT",
event.timestamp,
parseShortNumber(group("coins")),
- group("what"),
+ ids.findForName(group("what")),
group("count").toInt(),
)
)
@@ -30,7 +30,7 @@ class BazaarDetection(val ledger: LedgerLogger) {
"BAZAAR_SELL_INSTANT",
event.timestamp,
parseShortNumber(group("coins")),
- group("what"),
+ ids.findForName(group("what")),
group("count").toInt(),
)
)
diff --git a/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt b/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
new file mode 100644
index 0000000..8ad8d26
--- /dev/null
+++ b/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
@@ -0,0 +1,44 @@
+package moe.nea.ledger
+
+import net.minecraft.client.gui.GuiScreen
+import net.minecraft.client.gui.inventory.GuiChest
+import net.minecraft.inventory.ContainerChest
+import net.minecraft.nbt.NBTTagCompound
+import net.minecraftforge.client.event.GuiScreenEvent
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+
+class ItemIdProvider {
+
+ @SubscribeEvent
+ fun onMouseInput(event: GuiScreenEvent.MouseInputEvent.Pre) {
+ saveInventoryIds(event.gui)
+ }
+
+ @SubscribeEvent
+ fun onKeyInput(event: GuiScreenEvent.KeyboardInputEvent.Pre) {
+ saveInventoryIds(event.gui)
+ }
+
+ private val knownNames = mutableMapOf<String, String>()
+
+ fun saveInventoryIds(gui: GuiScreen) {
+ val chest = (gui as? GuiChest) ?: return
+ val slots = chest.inventorySlots as ContainerChest
+ slots.inventorySlots.forEach {
+ val stack = it.stack ?: return@forEach
+ val nbt = stack.tagCompound ?: NBTTagCompound()
+ val display = nbt.getCompoundTag("display")
+ val name = display.getString("Name").unformattedString()
+ val extraAttributes = nbt.getCompoundTag("ExtraAttributes")
+ val id = extraAttributes.getString("id")
+ if (id.isNotBlank() && 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 773f90e..7f2f306 100644
--- a/src/main/kotlin/moe/nea/ledger/Ledger.kt
+++ b/src/main/kotlin/moe/nea/ledger/Ledger.kt
@@ -37,10 +37,12 @@ class Ledger {
@Mod.EventHandler
fun init(event: FMLInitializationEvent) {
val ledger = LedgerLogger()
+ val ids = ItemIdProvider()
listOf(
this,
+ ids,
BankDetection(ledger),
- BazaarDetection(ledger)
+ BazaarDetection(ledger, ids)
).forEach(MinecraftForge.EVENT_BUS::register)
}
diff --git a/src/main/kotlin/moe/nea/ledger/NumberUtil.kt b/src/main/kotlin/moe/nea/ledger/NumberUtil.kt
index 8aa9bb8..267a8c5 100644
--- a/src/main/kotlin/moe/nea/ledger/NumberUtil.kt
+++ b/src/main/kotlin/moe/nea/ledger/NumberUtil.kt
@@ -30,3 +30,4 @@ fun parseShortNumber(string: String): Double {
fun <T> Pattern.useMatcher(string: String, block: Matcher.() -> T): T? =
matcher(string).takeIf { it.matches() }?.let(block)
+fun String.unformattedString(): String = replace("§.".toRegex(), "") \ No newline at end of file