aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-02-15 19:06:46 +0100
committerLinnea Gräf <nea@nea.moe>2024-02-15 19:06:46 +0100
commitaa8ca77ae239b9b6be140911524d70ba2b46c42d (patch)
tree14a91f9756853b1162a6725bb9100ab04061c2c2
parent3e19b97b52fae7422f8b0654da9341bf8c2393cb (diff)
downloadmoney-ledger-aa8ca77ae239b9b6be140911524d70ba2b46c42d.tar.gz
money-ledger-aa8ca77ae239b9b6be140911524d70ba2b46c42d.tar.bz2
money-ledger-aa8ca77ae239b9b6be140911524d70ba2b46c42d.zip
Add auction support
-rw-r--r--src/main/java/moe/nea/ledger/AuctionHouseDetection.kt73
-rw-r--r--src/main/kotlin/moe/nea/ledger/BeforeGuiAction.kt6
-rw-r--r--src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt16
-rw-r--r--src/main/kotlin/moe/nea/ledger/ItemUtil.kt13
-rw-r--r--src/main/kotlin/moe/nea/ledger/Ledger.kt3
5 files changed, 102 insertions, 9 deletions
diff --git a/src/main/java/moe/nea/ledger/AuctionHouseDetection.kt b/src/main/java/moe/nea/ledger/AuctionHouseDetection.kt
new file mode 100644
index 0000000..b9d76c5
--- /dev/null
+++ b/src/main/java/moe/nea/ledger/AuctionHouseDetection.kt
@@ -0,0 +1,73 @@
+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.util.regex.Pattern
+
+class AuctionHouseDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) {
+ data class LastViewedItem(
+ val count: Int,
+ val id: String,
+ )
+ /*
+ You collected 8,712,000 coins from selling Ultimate Carrot Candy Upgrade to [VIP] kodokush in an auction!
+ You collected 60,000 coins from selling Walnut to [MVP++] Alea1337 in an auction!
+ You purchased 2x Walnut for 69 coins!
+ You purchased ◆ Ice Rune I for 4,000 coins!
+ */
+
+ val collectSold =
+ Pattern.compile("You collected (?<coins>$SHORT_NUMBER_PATTERN) coins? from selling (?<what>.*) to (?<buyer>.*) in an auction!")
+ val purchased =
+ Pattern.compile("You purchased (?:(?<amount>[0-9]+)x )?(?<what>.*) for (?<coins>$SHORT_NUMBER_PATTERN) coins!")
+ var lastViewedItem: LastViewedItem? = null
+
+ @SubscribeEvent
+ fun onEvent(event: ChatReceived) {
+ collectSold.useMatcher(event.message) {
+ ledger.logEntry(
+ LedgerEntry(
+ "AUCTION_SOLD",
+ event.timestamp,
+ parseShortNumber(group("coins")),
+ lastViewedItem?.id,
+ lastViewedItem?.count
+ )
+ )
+ }
+ purchased.useMatcher(event.message) {
+ ledger.logEntry(
+ LedgerEntry(
+ "AUCTION_BOUGHT",
+ event.timestamp,
+ parseShortNumber(group("coins")),
+ ids.findForName(group("what")),
+ group("amount")?.let { it.toInt() } ?: 1
+ )
+ )
+ }
+ }
+
+ @SubscribeEvent
+ fun onBeforeAuctionCollected(event: BeforeGuiAction) {
+ // TODO: collect all support
+ val chest = (event.gui as? GuiChest) ?: return
+ val slots = chest.inventorySlots as ContainerChest
+ val name = slots.lowerChestInventory.displayName.unformattedText.unformattedString()
+
+ if (name == "BIN Auction View" || name == "Auction View") {
+ handleCollectSingleAuctionView(slots)
+ }
+ }
+
+
+ fun handleCollectSingleAuctionView(slots: ContainerChest) {
+ val soldItem = slots.lowerChestInventory.getStackInSlot(9 + 4) ?: return
+ val id = soldItem.getInternalId() ?: return
+ val count = soldItem.stackSize
+ lastViewedItem = LastViewedItem(count, id)
+ }
+
+
+}
diff --git a/src/main/kotlin/moe/nea/ledger/BeforeGuiAction.kt b/src/main/kotlin/moe/nea/ledger/BeforeGuiAction.kt
new file mode 100644
index 0000000..1615263
--- /dev/null
+++ b/src/main/kotlin/moe/nea/ledger/BeforeGuiAction.kt
@@ -0,0 +1,6 @@
+package moe.nea.ledger
+
+import net.minecraft.client.gui.GuiScreen
+import net.minecraftforge.fml.common.eventhandler.Event
+
+data class BeforeGuiAction(val gui: GuiScreen) : Event()
diff --git a/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt b/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
index 8ad8d26..988e317 100644
--- a/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
+++ b/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
@@ -1,37 +1,37 @@
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.common.MinecraftForge
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
class ItemIdProvider {
@SubscribeEvent
fun onMouseInput(event: GuiScreenEvent.MouseInputEvent.Pre) {
- saveInventoryIds(event.gui)
+ MinecraftForge.EVENT_BUS.post(BeforeGuiAction(event.gui))
}
@SubscribeEvent
fun onKeyInput(event: GuiScreenEvent.KeyboardInputEvent.Pre) {
- saveInventoryIds(event.gui)
+ MinecraftForge.EVENT_BUS.post(BeforeGuiAction(event.gui))
}
private val knownNames = mutableMapOf<String, String>()
- fun saveInventoryIds(gui: GuiScreen) {
- val chest = (gui as? GuiChest) ?: return
+ @SubscribeEvent
+ fun saveInventoryIds(event: BeforeGuiAction) {
+ val chest = (event.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()) {
+ val id = stack.getInternalId()
+ if (id != null && name.isNotBlank()) {
knownNames[name] = id
}
}
diff --git a/src/main/kotlin/moe/nea/ledger/ItemUtil.kt b/src/main/kotlin/moe/nea/ledger/ItemUtil.kt
new file mode 100644
index 0000000..365c37e
--- /dev/null
+++ b/src/main/kotlin/moe/nea/ledger/ItemUtil.kt
@@ -0,0 +1,13 @@
+package moe.nea.ledger
+
+import net.minecraft.item.ItemStack
+import net.minecraft.nbt.NBTTagCompound
+
+
+fun ItemStack.getInternalId(): String? {
+ val nbt = this.tagCompound ?: NBTTagCompound()
+ val extraAttributes = nbt.getCompoundTag("ExtraAttributes")
+ val id = extraAttributes.getString("id")
+ return id.takeIf { it.isNotBlank() }
+}
+
diff --git a/src/main/kotlin/moe/nea/ledger/Ledger.kt b/src/main/kotlin/moe/nea/ledger/Ledger.kt
index 7f2f306..d1fc57d 100644
--- a/src/main/kotlin/moe/nea/ledger/Ledger.kt
+++ b/src/main/kotlin/moe/nea/ledger/Ledger.kt
@@ -42,7 +42,8 @@ class Ledger {
this,
ids,
BankDetection(ledger),
- BazaarDetection(ledger, ids)
+ BazaarDetection(ledger, ids),
+ AuctionHouseDetection(ledger, ids),
).forEach(MinecraftForge.EVENT_BUS::register)
}