diff options
author | Linnea Gräf <nea@nea.moe> | 2024-12-06 19:27:07 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-12-06 19:28:23 +0100 |
commit | f7507f384459b57460af899bf9ceae4f52f1ea21 (patch) | |
tree | 410d70c0a35de852278b03ac9243080d7e0a0490 | |
parent | 2c9132d0c755964800b710ce43c8feebd44f1299 (diff) | |
download | LocalTransactionLedger-f7507f384459b57460af899bf9ceae4f52f1ea21.tar.gz LocalTransactionLedger-f7507f384459b57460af899bf9ceae4f52f1ea21.tar.bz2 LocalTransactionLedger-f7507f384459b57460af899bf9ceae4f52f1ea21.zip |
refactor: Add DI and packages
23 files changed, 396 insertions, 171 deletions
diff --git a/src/main/java/moe/nea/ledger/mixin/MouseClickEventPatch.java b/src/main/java/moe/nea/ledger/mixin/MouseClickEventPatch.java index 9b109c4..4e6e360 100644 --- a/src/main/java/moe/nea/ledger/mixin/MouseClickEventPatch.java +++ b/src/main/java/moe/nea/ledger/mixin/MouseClickEventPatch.java @@ -1,6 +1,6 @@ package moe.nea.ledger.mixin; -import moe.nea.ledger.GuiClickEvent; +import moe.nea.ledger.events.GuiClickEvent; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.inventory.Slot; import net.minecraftforge.common.MinecraftForge; diff --git a/src/main/kotlin/moe/nea/ledger/BankDetection.kt b/src/main/kotlin/moe/nea/ledger/BankDetection.kt deleted file mode 100644 index 2a39c6d..0000000 --- a/src/main/kotlin/moe/nea/ledger/BankDetection.kt +++ /dev/null @@ -1,33 +0,0 @@ -package moe.nea.ledger - -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.util.regex.Pattern - -class BankDetection(val ledger: LedgerLogger) { - val withdrawPattern = - Pattern.compile("^(You have withdrawn|Withdrew) (?<amount>$SHORT_NUMBER_PATTERN) coins?! (?:There's now|You now have) (?<newtotal>$SHORT_NUMBER_PATTERN) coins? (?:left in the account!|in your account!)$") - val depositPattern = - Pattern.compile("^(?:You have deposited|Deposited) (?<amount>$SHORT_NUMBER_PATTERN) coins?! (?:There's now|You now have) (?<newtotal>$SHORT_NUMBER_PATTERN) coins? (?:in your account!|in the account!)$") - @SubscribeEvent - fun onChat(event: ChatReceived) { - withdrawPattern.useMatcher(event.message) { - ledger.logEntry( - LedgerEntry( - "BANK_WITHDRAW", - event.timestamp, - parseShortNumber(group("amount")), - ) - ) - } - depositPattern.useMatcher(event.message) { - ledger.logEntry( - LedgerEntry( - "BANK_DEPOSIT", - event.timestamp, - parseShortNumber(group("amount")), - ) - ) - } - } - -} diff --git a/src/main/kotlin/moe/nea/ledger/BazaarDetection.kt b/src/main/kotlin/moe/nea/ledger/BazaarDetection.kt deleted file mode 100644 index 8f7c007..0000000 --- a/src/main/kotlin/moe/nea/ledger/BazaarDetection.kt +++ /dev/null @@ -1,39 +0,0 @@ -package moe.nea.ledger - -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.util.regex.Pattern - -class BazaarDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) { - - val instaBuyPattern = - Pattern.compile("\\[Bazaar\\] Bought (?<count>$SHORT_NUMBER_PATTERN)x (?<what>.*) for (?<coins>$SHORT_NUMBER_PATTERN) coins!") - val instaSellPattern = - Pattern.compile("\\[Bazaar\\] Sold (?<count>$SHORT_NUMBER_PATTERN)x (?<what>.*) for (?<coins>$SHORT_NUMBER_PATTERN) coins!") - - - @SubscribeEvent - fun onInstSellChat(event: ChatReceived) { - instaBuyPattern.useMatcher(event.message) { - ledger.logEntry( - LedgerEntry( - "BAZAAR_BUY_INSTANT", - event.timestamp, - parseShortNumber(group("coins")), - ids.findForName(group("what")), - parseShortNumber(group("count")).toInt(), - ) - ) - } - instaSellPattern.useMatcher(event.message) { - ledger.logEntry( - LedgerEntry( - "BAZAAR_SELL_INSTANT", - event.timestamp, - parseShortNumber(group("coins")), - ids.findForName(group("what")), - parseShortNumber(group("count")).toInt(), - ) - ) - } - } -} diff --git a/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt b/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt index fa0d8fa..65580fa 100644 --- a/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt +++ b/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt @@ -1,5 +1,6 @@ package moe.nea.ledger +import moe.nea.ledger.events.BeforeGuiAction import net.minecraft.client.gui.inventory.GuiChest import net.minecraft.inventory.ContainerChest import net.minecraft.nbt.NBTTagCompound diff --git a/src/main/kotlin/moe/nea/ledger/Ledger.kt b/src/main/kotlin/moe/nea/ledger/Ledger.kt index a11480f..521fd9d 100644 --- a/src/main/kotlin/moe/nea/ledger/Ledger.kt +++ b/src/main/kotlin/moe/nea/ledger/Ledger.kt @@ -4,6 +4,18 @@ import io.github.notenoughupdates.moulconfig.common.IMinecraft import io.github.notenoughupdates.moulconfig.managed.ManagedConfig import moe.nea.ledger.config.LedgerConfig import moe.nea.ledger.database.Database +import moe.nea.ledger.events.ChatReceived +import moe.nea.ledger.events.LateWorldLoadEvent +import moe.nea.ledger.modules.AuctionHouseDetection +import moe.nea.ledger.modules.BankDetection +import moe.nea.ledger.modules.BazaarDetection +import moe.nea.ledger.modules.BazaarOrderDetection +import moe.nea.ledger.modules.BitsDetection +import moe.nea.ledger.modules.BitsShopDetection +import moe.nea.ledger.modules.DungeonChestDetection +import moe.nea.ledger.modules.MinionDetection +import moe.nea.ledger.modules.NpcDetection +import moe.nea.ledger.utils.DI import net.minecraft.client.Minecraft import net.minecraft.command.CommandBase import net.minecraft.command.ICommandSender @@ -96,22 +108,23 @@ class Ledger { return listOf("moneyledger") } }) - val ledger = LedgerLogger() - val ids = ItemIdProvider() - listOf( - this, - ids, - ledger, - BankDetection(ledger), - BazaarDetection(ledger, ids), - DungeonChestDetection(ledger), - BazaarOrderDetection(ledger, ids), - AuctionHouseDetection(ledger, ids), - BitsDetection(ledger), - BitsShop(ledger), - MinionDetection(ledger), - NpcDetection(ledger, ids), - ).forEach(MinecraftForge.EVENT_BUS::register) + val di = DI() + di.registerSingleton(this) + di.registerInjectableClasses( + LedgerLogger::class.java, + ItemIdProvider::class.java, + BankDetection::class.java, + BazaarDetection::class.java, + DungeonChestDetection::class.java, + BazaarOrderDetection::class.java, + AuctionHouseDetection::class.java, + BitsDetection::class.java, + BitsShopDetection::class.java, + MinionDetection::class.java, + NpcDetection::class.java, + ) + di.instantiateAll() + di.getAllInstances().forEach(MinecraftForge.EVENT_BUS::register) } var lastJoin = -1L diff --git a/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt b/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt index 03d306c..691eae7 100644 --- a/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt +++ b/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt @@ -3,6 +3,7 @@ package moe.nea.ledger import com.google.gson.Gson import com.google.gson.JsonArray import com.google.gson.JsonObject +import moe.nea.ledger.events.ChatReceived import net.minecraft.client.Minecraft import net.minecraft.command.CommandBase import net.minecraft.command.ICommandSender @@ -11,8 +12,6 @@ import net.minecraftforge.client.ClientCommandHandler import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent import java.io.File -import java.sql.Connection -import java.sql.DriverManager import java.text.SimpleDateFormat import java.time.Instant import java.util.* diff --git a/src/main/kotlin/moe/nea/ledger/NpcDetection.kt b/src/main/kotlin/moe/nea/ledger/NpcDetection.kt deleted file mode 100644 index 3c3fe87..0000000 --- a/src/main/kotlin/moe/nea/ledger/NpcDetection.kt +++ /dev/null @@ -1,38 +0,0 @@ -package moe.nea.ledger - -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import java.util.regex.Pattern - -class NpcDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) { - - val npcBuyPattern = - Pattern.compile("You bought (back )?(?<what>.*?) (x(?<count>$SHORT_NUMBER_PATTERN) )?for (?<coins>$SHORT_NUMBER_PATTERN) Coins!") - val npcSellPattern = - Pattern.compile("You sold (?<what>.*) (x(?<count>$SHORT_NUMBER_PATTERN) )?for (?<coins>$SHORT_NUMBER_PATTERN) Coins!") - - @SubscribeEvent - fun onNpcBuy(event: ChatReceived) { - npcBuyPattern.useMatcher(event.message) { - ledger.logEntry( - LedgerEntry( - "NPC_BUY", - event.timestamp, - parseShortNumber(group("coins")), - ids.findForName(group("what")), - group("count")?.let(::parseShortNumber)?.toInt() ?: 1, - ) - ) - } - npcSellPattern.useMatcher(event.message) { - ledger.logEntry( - LedgerEntry( - "NPC_SELL", - event.timestamp, - parseShortNumber(group("coins")), - ids.findForName(group("what")), - group("count")?.let(::parseShortNumber)?.toInt() ?: 1, - ) - ) - } - } -} diff --git a/src/main/kotlin/moe/nea/ledger/BeforeGuiAction.kt b/src/main/kotlin/moe/nea/ledger/events/BeforeGuiAction.kt index 1615263..69362bd 100644 --- a/src/main/kotlin/moe/nea/ledger/BeforeGuiAction.kt +++ b/src/main/kotlin/moe/nea/ledger/events/BeforeGuiAction.kt @@ -1,4 +1,4 @@ -package moe.nea.ledger +package moe.nea.ledger.events import net.minecraft.client.gui.GuiScreen import net.minecraftforge.fml.common.eventhandler.Event diff --git a/src/main/kotlin/moe/nea/ledger/ChatReceived.kt b/src/main/kotlin/moe/nea/ledger/events/ChatReceived.kt index f3c4c50..e88c7a0 100644 --- a/src/main/kotlin/moe/nea/ledger/ChatReceived.kt +++ b/src/main/kotlin/moe/nea/ledger/events/ChatReceived.kt @@ -1,5 +1,6 @@ -package moe.nea.ledger +package moe.nea.ledger.events +import moe.nea.ledger.unformattedString import net.minecraftforge.client.event.ClientChatReceivedEvent import net.minecraftforge.fml.common.eventhandler.Event import java.time.Instant diff --git a/src/main/kotlin/moe/nea/ledger/GuiClickEvent.kt b/src/main/kotlin/moe/nea/ledger/events/GuiClickEvent.kt index 13e74f1..9e057dd 100644 --- a/src/main/kotlin/moe/nea/ledger/GuiClickEvent.kt +++ b/src/main/kotlin/moe/nea/ledger/events/GuiClickEvent.kt @@ -1,4 +1,4 @@ -package moe.nea.ledger +package moe.nea.ledger.events import net.minecraft.inventory.Slot import net.minecraftforge.fml.common.eventhandler.Event diff --git a/src/main/kotlin/moe/nea/ledger/WorldLoadEvent.kt b/src/main/kotlin/moe/nea/ledger/events/WorldLoadEvent.kt index 4b7fa6d..d60f3a4 100644 --- a/src/main/kotlin/moe/nea/ledger/WorldLoadEvent.kt +++ b/src/main/kotlin/moe/nea/ledger/events/WorldLoadEvent.kt @@ -1,4 +1,4 @@ -package moe.nea.ledger +package moe.nea.ledger.events import net.minecraftforge.fml.common.eventhandler.Event diff --git a/src/main/kotlin/moe/nea/ledger/AuctionHouseDetection.kt b/src/main/kotlin/moe/nea/ledger/modules/AuctionHouseDetection.kt index a73d6b1..cbbff12 100644 --- a/src/main/kotlin/moe/nea/ledger/AuctionHouseDetection.kt +++ b/src/main/kotlin/moe/nea/ledger/modules/AuctionHouseDetection.kt @@ -1,11 +1,23 @@ -package moe.nea.ledger +package moe.nea.ledger.modules +import moe.nea.ledger.events.BeforeGuiAction +import moe.nea.ledger.events.ChatReceived +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.getInternalId +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.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) { +class AuctionHouseDetection @Inject constructor(val ledger: LedgerLogger, val ids: ItemIdProvider) { data class LastViewedItem( val count: Int, val id: String, @@ -28,24 +40,24 @@ class AuctionHouseDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) { collectSold.useMatcher(event.message) { val lastViewedItem = lastViewedItems.removeLastOrNull() ledger.logEntry( - LedgerEntry( - "AUCTION_SOLD", - event.timestamp, - parseShortNumber(group("coins")), - lastViewedItem?.id, - lastViewedItem?.count - ) + 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")?.toInt() ?: 1 - ) + LedgerEntry( + "AUCTION_BOUGHT", + event.timestamp, + parseShortNumber(group("coins")), + ids.findForName(group("what")), + group("amount")?.toInt() ?: 1 + ) ) } } @@ -85,4 +97,4 @@ class AuctionHouseDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) { } -} +}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/ledger/modules/BankDetection.kt b/src/main/kotlin/moe/nea/ledger/modules/BankDetection.kt new file mode 100644 index 0000000..8d0fd81 --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/modules/BankDetection.kt @@ -0,0 +1,42 @@ +package moe.nea.ledger.modules + +import moe.nea.ledger.LedgerEntry +import moe.nea.ledger.LedgerLogger +import moe.nea.ledger.SHORT_NUMBER_PATTERN +import moe.nea.ledger.events.ChatReceived +import moe.nea.ledger.parseShortNumber +import moe.nea.ledger.useMatcher +import moe.nea.ledger.utils.Inject +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import java.util.regex.Pattern + + +class BankDetection @Inject constructor(val ledger: LedgerLogger) { + val withdrawPattern = + Pattern.compile("^(You have withdrawn|Withdrew) (?<amount>$SHORT_NUMBER_PATTERN) coins?! (?:There's now|You now have) (?<newtotal>$SHORT_NUMBER_PATTERN) coins? (?:left in the account!|in your account!)$") + val depositPattern = + Pattern.compile("^(?:You have deposited|Deposited) (?<amount>$SHORT_NUMBER_PATTERN) coins?! (?:There's now|You now have) (?<newtotal>$SHORT_NUMBER_PATTERN) coins? (?:in your account!|in the account!)$") + + @SubscribeEvent + fun onChat(event: ChatReceived) { + withdrawPattern.useMatcher(event.message) { + ledger.logEntry( + LedgerEntry( + "BANK_WITHDRAW", + event.timestamp, + parseShortNumber(group("amount")), + ) + ) + } + depositPattern.useMatcher(event.message) { + ledger.logEntry( + LedgerEntry( + "BANK_DEPOSIT", + event.timestamp, + parseShortNumber(group("amount")), + ) + ) + } + } + +} diff --git a/src/main/kotlin/moe/nea/ledger/modules/BazaarDetection.kt b/src/main/kotlin/moe/nea/ledger/modules/BazaarDetection.kt new file mode 100644 index 0000000..01c8bbc --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/modules/BazaarDetection.kt @@ -0,0 +1,47 @@ +package moe.nea.ledger.modules + +import moe.nea.ledger.events.ChatReceived +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.parseShortNumber +import moe.nea.ledger.useMatcher +import moe.nea.ledger.utils.Inject +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import java.util.regex.Pattern + +class BazaarDetection @Inject constructor(val ledger: LedgerLogger, val ids: ItemIdProvider) { + + val instaBuyPattern = + Pattern.compile("\\[Bazaar\\] Bought (?<count>$SHORT_NUMBER_PATTERN)x (?<what>.*) for (?<coins>$SHORT_NUMBER_PATTERN) coins!") + val instaSellPattern = + Pattern.compile("\\[Bazaar\\] Sold (?<count>$SHORT_NUMBER_PATTERN)x (?<what>.*) for (?<coins>$SHORT_NUMBER_PATTERN) coins!") + + + @SubscribeEvent + fun onInstSellChat(event: ChatReceived) { + instaBuyPattern.useMatcher(event.message) { + ledger.logEntry( + LedgerEntry( + "BAZAAR_BUY_INSTANT", + event.timestamp, + parseShortNumber(group("coins")), + ids.findForName(group("what")), + parseShortNumber(group("count")).toInt(), + ) + ) + } + instaSellPattern.useMatcher(event.message) { + ledger.logEntry( + LedgerEntry( + "BAZAAR_SELL_INSTANT", + event.timestamp, + parseShortNumber(group("coins")), + ids.findForName(group("what")), + parseShortNumber(group("count")).toInt(), + ) + ) + } + } +} diff --git a/src/main/kotlin/moe/nea/ledger/BazaarOrderDetection.kt b/src/main/kotlin/moe/nea/ledger/modules/BazaarOrderDetection.kt index 79ba65b..7e611ac 100644 --- a/src/main/kotlin/moe/nea/ledger/BazaarOrderDetection.kt +++ b/src/main/kotlin/moe/nea/ledger/modules/BazaarOrderDetection.kt @@ -1,12 +1,20 @@ -package moe.nea.ledger +package moe.nea.ledger.modules +import moe.nea.ledger.events.ChatReceived +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.mixin.AccessorGuiEditSign +import moe.nea.ledger.parseShortNumber +import moe.nea.ledger.useMatcher +import moe.nea.ledger.utils.Inject import net.minecraft.client.gui.inventory.GuiEditSign import net.minecraftforge.client.event.GuiScreenEvent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import java.util.regex.Pattern -class BazaarOrderDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) { +class BazaarOrderDetection @Inject constructor(val ledger: LedgerLogger, val ids: ItemIdProvider) { val buyOrderClaimed = Pattern.compile("\\[Bazaar] Claimed (?<amount>$SHORT_NUMBER_PATTERN)x (?<what>.*) worth (?<coins>$SHORT_NUMBER_PATTERN) coins? bought for $SHORT_NUMBER_PATTERN each!") @@ -47,22 +55,22 @@ class BazaarOrderDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) { buyOrderClaimed.useMatcher(event.message) { ledger.logEntry( LedgerEntry( - "BAZAAR_BUY_ORDER", - event.timestamp, - parseShortNumber(group("coins")), - ids.findForName(group("what")), - parseShortNumber(group("amount")).toInt(), + "BAZAAR_BUY_ORDER", + event.timestamp, + parseShortNumber(group("coins")), + ids.findForName(group("what")), + parseShortNumber(group("amount")).toInt(), ) ) } sellOrderClaimed.useMatcher(event.message) { ledger.logEntry( LedgerEntry( - "BAZAAR_SELL_ORDER", - event.timestamp, - parseShortNumber(group("coins")), - ids.findForName(group("what")), - parseShortNumber(group("amount")).toInt(), + "BAZAAR_SELL_ORDER", + event.timestamp, + parseShortNumber(group("coins")), + ids.findForName(group("what")), + parseShortNumber(group("amount")).toInt(), ) ) } diff --git a/src/main/kotlin/moe/nea/ledger/BitsDetection.kt b/src/main/kotlin/moe/nea/ledger/modules/BitsDetection.kt index 2d26b3d..22b5392 100644 --- a/src/main/kotlin/moe/nea/ledger/BitsDetection.kt +++ b/src/main/kotlin/moe/nea/ledger/modules/BitsDetection.kt @@ -1,9 +1,19 @@ -package moe.nea.ledger +package moe.nea.ledger.modules +import moe.nea.ledger.events.ChatReceived +import moe.nea.ledger.events.LateWorldLoadEvent +import moe.nea.ledger.LedgerEntry +import moe.nea.ledger.LedgerLogger +import moe.nea.ledger.SHORT_NUMBER_PATTERN +import moe.nea.ledger.ScoreboardUtil +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 BitsDetection(val ledger: LedgerLogger) { +class BitsDetection @Inject constructor(val ledger: LedgerLogger) { var lastBits = -1 diff --git a/src/main/kotlin/moe/nea/ledger/BitsShop.kt b/src/main/kotlin/moe/nea/ledger/modules/BitsShopDetection.kt index ea9e4d2..2033d8d 100644 --- a/src/main/kotlin/moe/nea/ledger/BitsShop.kt +++ b/src/main/kotlin/moe/nea/ledger/modules/BitsShopDetection.kt @@ -1,9 +1,20 @@ -package moe.nea.ledger - +package moe.nea.ledger.modules + +import moe.nea.ledger.events.ChatReceived +import moe.nea.ledger.events.GuiClickEvent +import moe.nea.ledger.LedgerEntry +import moe.nea.ledger.LedgerLogger +import moe.nea.ledger.SHORT_NUMBER_PATTERN +import moe.nea.ledger.getInternalId +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 BitsShop(val ledger: LedgerLogger) { +class BitsShopDetection @Inject constructor(val ledger: LedgerLogger) { data class BitShopEntry( diff --git a/src/main/kotlin/moe/nea/ledger/DungeonChestDetection.kt b/src/main/kotlin/moe/nea/ledger/modules/DungeonChestDetection.kt index 2418354..4bdd37c 100644 --- a/src/main/kotlin/moe/nea/ledger/DungeonChestDetection.kt +++ b/src/main/kotlin/moe/nea/ledger/modules/DungeonChestDetection.kt @@ -1,11 +1,22 @@ -package moe.nea.ledger +package moe.nea.ledger.modules +import moe.nea.ledger.events.ChatReceived +import moe.nea.ledger.events.GuiClickEvent +import moe.nea.ledger.LedgerEntry +import moe.nea.ledger.LedgerLogger +import moe.nea.ledger.SHORT_NUMBER_PATTERN +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 net.minecraftforge.fml.common.gameevent.TickEvent import java.time.Instant import java.util.regex.Pattern -class DungeonChestDetection(val logger: LedgerLogger) { +class DungeonChestDetection @Inject constructor(val logger: LedgerLogger) { /*{ id: "minecraft:chest", diff --git a/src/main/kotlin/moe/nea/ledger/MinionDetection.kt b/src/main/kotlin/moe/nea/ledger/modules/MinionDetection.kt index bb2a187..73d06fa 100644 --- a/src/main/kotlin/moe/nea/ledger/MinionDetection.kt +++ b/src/main/kotlin/moe/nea/ledger/modules/MinionDetection.kt @@ -1,12 +1,24 @@ -package moe.nea.ledger +package moe.nea.ledger.modules +import moe.nea.ledger.events.BeforeGuiAction +import moe.nea.ledger.events.ChatReceived +import moe.nea.ledger.ExpiringValue +import moe.nea.ledger.LedgerEntry +import moe.nea.ledger.LedgerLogger +import moe.nea.ledger.ROMAN_NUMBER_PATTERN +import moe.nea.ledger.SHORT_NUMBER_PATTERN +import moe.nea.ledger.parseRomanNumber +import moe.nea.ledger.parseShortNumber +import moe.nea.ledger.unformattedString +import moe.nea.ledger.useMatcher +import moe.nea.ledger.utils.Inject 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) { +class MinionDetection @Inject constructor(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() diff --git a/src/main/kotlin/moe/nea/ledger/modules/NpcDetection.kt b/src/main/kotlin/moe/nea/ledger/modules/NpcDetection.kt new file mode 100644 index 0000000..68f0257 --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/modules/NpcDetection.kt @@ -0,0 +1,46 @@ +package moe.nea.ledger.modules + +import moe.nea.ledger.events.ChatReceived +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.parseShortNumber +import moe.nea.ledger.useMatcher +import moe.nea.ledger.utils.Inject +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import java.util.regex.Pattern + +class NpcDetection @Inject constructor(val ledger: LedgerLogger, val ids: ItemIdProvider) { + + val npcBuyPattern = + Pattern.compile("You bought (back )?(?<what>.*?) (x(?<count>$SHORT_NUMBER_PATTERN) )?for (?<coins>$SHORT_NUMBER_PATTERN) Coins!") + val npcSellPattern = + Pattern.compile("You sold (?<what>.*) (x(?<count>$SHORT_NUMBER_PATTERN) )?for (?<coins>$SHORT_NUMBER_PATTERN) Coins!") + + @SubscribeEvent + fun onNpcBuy(event: ChatReceived) { + npcBuyPattern.useMatcher(event.message) { + ledger.logEntry( + LedgerEntry( + "NPC_BUY", + event.timestamp, + parseShortNumber(group("coins")), + ids.findForName(group("what")), + group("count")?.let(::parseShortNumber)?.toInt() ?: 1, + ) + ) + } + npcSellPattern.useMatcher(event.message) { + ledger.logEntry( + LedgerEntry( + "NPC_SELL", + event.timestamp, + parseShortNumber(group("coins")), + ids.findForName(group("what")), + group("count")?.let(::parseShortNumber)?.toInt() ?: 1, + ) + ) + } + } +} diff --git a/src/main/kotlin/moe/nea/ledger/utils/DI.kt b/src/main/kotlin/moe/nea/ledger/utils/DI.kt new file mode 100644 index 0000000..1114127 --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/utils/DI.kt @@ -0,0 +1,63 @@ +package moe.nea.ledger.utils + +import java.lang.reflect.AnnotatedElement +import java.util.Collections +import java.util.Stack + +@Suppress("UNCHECKED_CAST") +class DI { + private fun <T : Any, C> internalProvide(type: Class<T>, element: AnnotatedElement? = null): T { + val provider = providers[type] as BaseDIProvider<T, C> + val context = if (element == null) provider.createEmptyContext() else provider.createContext(element) + val key = Pair(type, context) + val existingValue = values[key] + if (existingValue != null) return existingValue as T + if (type in injectionStack) { + error("Found injection cycle: ${injectionStack.joinToString(" -> ")} -> $type") + } + injectionStack.push(type) + val value = try { + provider.provideWithContext(this, context) + } catch (ex: Exception) { + throw RuntimeException("Could not create instance for type $type", ex) + } + val cycleCheckCookie = injectionStack.pop() + require(cycleCheckCookie == type) { "Unbalanced stack cookie: $cycleCheckCookie != $type" } + values[key] = value + return value + } + + fun <T : Any> provide(type: Class<T>, element: AnnotatedElement? = null): T { + return internalProvide<T, Any>(type, element) + } + + fun <T : Any> register(type: Class<T>, provider: BaseDIProvider<T, *>) { + providers[type] = provider + } + + fun registerInjectableClasses(vararg type: Class<*>) { + type.forEach { internalRegisterInjectableClass(it) } + } + + private fun <T : Any> internalRegisterInjectableClass(type: Class<T>) { + register(type, DIProvider.fromInjectableClass(type)) + } + + fun instantiateAll() { + providers.keys.forEach { + provide(it, null) + } + } + + fun getAllInstances(): Collection<Any> = + Collections.unmodifiableCollection(values.values) + + fun <T : Any> registerSingleton(value: T) { + register(value.javaClass, DIProvider.singeleton(value)) + } + + private val injectionStack: Stack<Class<*>> = Stack() + private val values = mutableMapOf<Pair<Class<*>, *>, Any>() + private val providers = mutableMapOf<Class<*>, BaseDIProvider<*, *>>() + +}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/ledger/utils/DIProvider.kt b/src/main/kotlin/moe/nea/ledger/utils/DIProvider.kt new file mode 100644 index 0000000..3cedf40 --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/utils/DIProvider.kt @@ -0,0 +1,53 @@ +package moe.nea.ledger.utils + +import java.lang.reflect.AnnotatedElement +import java.lang.reflect.Constructor + +fun interface DIProvider<T : Any> : BaseDIProvider<T, Unit> { + override fun provideWithContext(di: DI, context: Unit): T { + return provide(di) + } + + override fun createContext(element: AnnotatedElement) { + } + + override fun createEmptyContext() { + } + + fun provide(di: DI): T + + companion object { + + fun <T : Any> fromInjectableClass(clazz: Class<T>): DIProvider<T> { + @Suppress("UNCHECKED_CAST") + val cons = (clazz.constructors.find { it.getAnnotation(Inject::class.java) != null } + ?: clazz.constructors.find { it.parameterCount == 0 } + ?: error("Could not find DI injection entrypoint for class $clazz")) + as Constructor<out T> + return DIProvider { di -> + val typArgs = cons.parameters.map { + di.provide(it.type, it) + }.toTypedArray() + val instance = cons.newInstance(*typArgs) + for (it in clazz.fields) { + if (it.getAnnotation(Inject::class.java) != null) { + continue + } + it.set(instance, di.provide(it.type, it)) + } + instance + } + } + + fun <T : Any> singeleton(value: T): DIProvider<T> { + return DIProvider { _ -> value } + } + } + +} + +interface BaseDIProvider<T : Any, C> { + fun createContext(element: AnnotatedElement): C + fun provideWithContext(di: DI, context: C): T + fun createEmptyContext(): C +} diff --git a/src/main/kotlin/moe/nea/ledger/utils/Inject.kt b/src/main/kotlin/moe/nea/ledger/utils/Inject.kt new file mode 100644 index 0000000..654f77e --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/utils/Inject.kt @@ -0,0 +1,6 @@ +package moe.nea.ledger.utils + +@Retention(AnnotationRetention.RUNTIME) +@Target(AnnotationTarget.CONSTRUCTOR, AnnotationTarget.FIELD) +annotation class Inject( +) |