From db0b75a351e08c1e82885fcf01e967c59a915b8c Mon Sep 17 00:00:00 2001 From: jani270 <69345714+jani270@users.noreply.github.com> Date: Wed, 30 Oct 2024 11:48:09 +0100 Subject: feat: NPC Detection --- src/main/kotlin/moe/nea/ledger/Ledger.kt | 8 ++++- src/main/kotlin/moe/nea/ledger/NpcDetection.kt | 42 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/moe/nea/ledger/NpcDetection.kt diff --git a/src/main/kotlin/moe/nea/ledger/Ledger.kt b/src/main/kotlin/moe/nea/ledger/Ledger.kt index b4fe2c4..31330e2 100644 --- a/src/main/kotlin/moe/nea/ledger/Ledger.kt +++ b/src/main/kotlin/moe/nea/ledger/Ledger.kt @@ -46,7 +46,12 @@ class Ledger { You purchased 2x Walnut for 69 coins! You purchased ◆ Ice Rune I for 4,000 coins! - TODO: TRADING, FORGE, COOKIE_EATEN, NPC_SELL, NPC_BUY + // NPC + + // You bought Cactus x32 for 465.6 Coins! + // You sold Cactus x1 for 3 Coins! + + TODO: TRADING, FORGE, COOKIE_EATEN */ companion object { val dataFolder = File("money-ledger").apply { mkdirs() } @@ -104,6 +109,7 @@ class Ledger { BitsDetection(ledger), BitsShop(ledger), MinionDetection(ledger), + NpcDetection(ledger, ids), ).forEach(MinecraftForge.EVENT_BUS::register) } diff --git a/src/main/kotlin/moe/nea/ledger/NpcDetection.kt b/src/main/kotlin/moe/nea/ledger/NpcDetection.kt new file mode 100644 index 0000000..8f20bc4 --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/NpcDetection.kt @@ -0,0 +1,42 @@ +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 (?.*?) x(?$SHORT_NUMBER_PATTERN) for (?$SHORT_NUMBER_PATTERN) Coins!") + val npcSellPattern = + Pattern.compile("You sold (?.*) x(?$SHORT_NUMBER_PATTERN) for (?$SHORT_NUMBER_PATTERN) Coins!") + + // You bought Cactus x32 for 465.6 Coins! + // You sold Cactus x1 for 3 Coins! + + + @SubscribeEvent + fun onNpcBuy(event: ChatReceived) { + npcBuyPattern.useMatcher(event.message) { + ledger.logEntry( + LedgerEntry( + "NPC_BUY", + event.timestamp, + parseShortNumber(group("coins")), + ids.findForName(group("what")), + parseShortNumber(group("count")).toInt(), + ) + ) + } + npcSellPattern.useMatcher(event.message) { + ledger.logEntry( + LedgerEntry( + "NPC_SELL", + event.timestamp, + parseShortNumber(group("coins")), + ids.findForName(group("what")), + parseShortNumber(group("count")).toInt(), + ) + ) + } + } +} -- cgit From 5861b04239c801fac74aadac8583594bfc4a46ea Mon Sep 17 00:00:00 2001 From: jani270 <69345714+jani270@users.noreply.github.com> Date: Wed, 30 Oct 2024 11:50:15 +0100 Subject: fix: Spacing of comment --- src/main/kotlin/moe/nea/ledger/Ledger.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/moe/nea/ledger/Ledger.kt b/src/main/kotlin/moe/nea/ledger/Ledger.kt index 31330e2..23d1265 100644 --- a/src/main/kotlin/moe/nea/ledger/Ledger.kt +++ b/src/main/kotlin/moe/nea/ledger/Ledger.kt @@ -49,7 +49,7 @@ class Ledger { // NPC // You bought Cactus x32 for 465.6 Coins! - // You sold Cactus x1 for 3 Coins! + // You sold Cactus x1 for 3 Coins! TODO: TRADING, FORGE, COOKIE_EATEN */ -- cgit From 6c793e838599fa30494893eccab52f9e86c69d4f Mon Sep 17 00:00:00 2001 From: jani270 <69345714+jani270@users.noreply.github.com> Date: Wed, 30 Oct 2024 11:54:33 +0100 Subject: refactor: Remove comment from NpcDetection as its already present in Ledger file --- src/main/kotlin/moe/nea/ledger/NpcDetection.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/kotlin/moe/nea/ledger/NpcDetection.kt b/src/main/kotlin/moe/nea/ledger/NpcDetection.kt index 8f20bc4..656f1d6 100644 --- a/src/main/kotlin/moe/nea/ledger/NpcDetection.kt +++ b/src/main/kotlin/moe/nea/ledger/NpcDetection.kt @@ -10,10 +10,6 @@ class NpcDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) { val npcSellPattern = Pattern.compile("You sold (?.*) x(?$SHORT_NUMBER_PATTERN) for (?$SHORT_NUMBER_PATTERN) Coins!") - // You bought Cactus x32 for 465.6 Coins! - // You sold Cactus x1 for 3 Coins! - - @SubscribeEvent fun onNpcBuy(event: ChatReceived) { npcBuyPattern.useMatcher(event.message) { -- cgit From 3001af7c4277c96f2818ea2efe3c7836b1f1498f Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Wed, 30 Oct 2024 19:01:07 +0100 Subject: fix: single buy/sell npc detection --- src/main/kotlin/moe/nea/ledger/NpcDetection.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/moe/nea/ledger/NpcDetection.kt b/src/main/kotlin/moe/nea/ledger/NpcDetection.kt index 656f1d6..65693ef 100644 --- a/src/main/kotlin/moe/nea/ledger/NpcDetection.kt +++ b/src/main/kotlin/moe/nea/ledger/NpcDetection.kt @@ -6,9 +6,9 @@ import java.util.regex.Pattern class NpcDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) { val npcBuyPattern = - Pattern.compile("You bought (?.*?) x(?$SHORT_NUMBER_PATTERN) for (?$SHORT_NUMBER_PATTERN) Coins!") + Pattern.compile("You bought (?.*?) (x(?$SHORT_NUMBER_PATTERN) )?for (?$SHORT_NUMBER_PATTERN) Coins!") val npcSellPattern = - Pattern.compile("You sold (?.*) x(?$SHORT_NUMBER_PATTERN) for (?$SHORT_NUMBER_PATTERN) Coins!") + Pattern.compile("You sold (?.*) (x(?$SHORT_NUMBER_PATTERN) )?for (?$SHORT_NUMBER_PATTERN) Coins!") @SubscribeEvent fun onNpcBuy(event: ChatReceived) { @@ -19,7 +19,7 @@ class NpcDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) { event.timestamp, parseShortNumber(group("coins")), ids.findForName(group("what")), - parseShortNumber(group("count")).toInt(), + group("count")?.let(::parseShortNumber)?.toInt() ?: 1, ) ) } @@ -30,7 +30,7 @@ class NpcDetection(val ledger: LedgerLogger, val ids: ItemIdProvider) { event.timestamp, parseShortNumber(group("coins")), ids.findForName(group("what")), - parseShortNumber(group("count")).toInt(), + group("count")?.let(::parseShortNumber)?.toInt() ?: 1, ) ) } -- cgit