diff options
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/LedgerLogger.kt')
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/LedgerLogger.kt | 220 |
1 files changed, 98 insertions, 122 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt b/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt index 691eae7..c35d203 100644 --- a/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt +++ b/src/main/kotlin/moe/nea/ledger/LedgerLogger.kt @@ -5,10 +5,7 @@ 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 import net.minecraft.util.ChatComponentText -import net.minecraftforge.client.ClientCommandHandler import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent import java.io.File @@ -17,49 +14,28 @@ import java.time.Instant import java.util.* class LedgerLogger { - fun printOut(text: String) { - Minecraft.getMinecraft().ingameGUI?.chatGUI?.printChatMessage(ChatComponentText(text)) - } + fun printOut(text: String) { + Minecraft.getMinecraft().ingameGUI?.chatGUI?.printChatMessage(ChatComponentText(text)) + } - val profileIdPattern = - "Profile ID: (?<profile>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})".toPattern() + val profileIdPattern = + "Profile ID: (?<profile>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})".toPattern() - var currentProfile: String? = null + var currentProfile: String? = null - var shouldLog = false + var shouldLog by Ledger.managedConfig.instance.debug::logEntries - init { - ClientCommandHandler.instance.registerCommand(object : CommandBase() { - override fun getCommandName(): String { - return "ledgerlogchat" - } + @SubscribeEvent + fun onProfileSwitch(event: ChatReceived) { + profileIdPattern.useMatcher(event.message) { + currentProfile = group("profile") + } + } - override fun canCommandSenderUseCommand(sender: ICommandSender?): Boolean { - return true - } - override fun getCommandUsage(sender: ICommandSender?): String { - return "" - } - - override fun processCommand(sender: ICommandSender?, args: Array<out String>?) { - shouldLog = !shouldLog - printOut("§eLedger logging toggled " + (if (shouldLog) "§aon" else "§coff") + "§e.") - } - }) - } - - @SubscribeEvent - fun onProfileSwitch(event: ChatReceived) { - profileIdPattern.useMatcher(event.message) { - currentProfile = group("profile") - } - } - - - fun printToChat(entry: LedgerEntry) { - printOut( - """ + fun printToChat(entry: LedgerEntry) { + printOut( + """ §e================= TRANSACTION START §eTYPE: §a${entry.transactionType} §eTIMESTAMP: §a${entry.timestamp} @@ -69,93 +45,93 @@ class LedgerLogger { §ePROFILE: §a${currentProfile} §e================= TRANSACTION END """.trimIndent() - ) - } - - val entries = JsonArray() - var hasRecentlyMerged = false - var lastMergeTime = System.currentTimeMillis() - - fun doMerge() { - val allFiles = folder.listFiles()?.toList() ?: emptyList() - val mergedJson = allFiles - .filter { it.name != "merged.json" && it.extension == "json" } - .sortedDescending() - .map { it.readText().trim().removePrefix("[").removeSuffix("]") } - .joinToString(",", "[", "]") - folder.resolve("merged.json").writeText(mergedJson) - hasRecentlyMerged = true - } - - init { - Runtime.getRuntime().addShutdownHook(Thread { doMerge() }) - } - - @SubscribeEvent - fun onTick(event: ClientTickEvent) { - if (!hasRecentlyMerged && (System.currentTimeMillis() - lastMergeTime) > 60_000L) { - lastMergeTime = System.currentTimeMillis() - doMerge() - } - } - - fun logEntry(entry: LedgerEntry) { - if (shouldLog) - printToChat(entry) - Ledger.logger.info("Logging entry of type ${entry.transactionType}") - entries.add(entry.intoJson(currentProfile)) - commit() - } - - fun commit() { - try { - hasRecentlyMerged = false - file.writeText(gson.toJson(entries)) - } catch (ex: Exception) { - Ledger.logger.error("Could not save file", ex) - } - } - - val gson = Gson() - - val folder = Ledger.dataFolder - val file: File = run { - val date = SimpleDateFormat("yyyy.MM.dd").format(Date()) - - generateSequence(0) { it + 1 } - .map { - if (it == 0) - folder.resolve("$date.json") - else - folder.resolve("$date-$it.json") - } - .filter { !it.exists() } - .first() - } + ) + } + + val entries = JsonArray() + var hasRecentlyMerged = false + var lastMergeTime = System.currentTimeMillis() + + fun doMerge() { + val allFiles = folder.listFiles()?.toList() ?: emptyList() + val mergedJson = allFiles + .filter { it.name != "merged.json" && it.extension == "json" } + .sortedDescending() + .map { it.readText().trim().removePrefix("[").removeSuffix("]") } + .joinToString(",", "[", "]") + folder.resolve("merged.json").writeText(mergedJson) + hasRecentlyMerged = true + } + + init { + Runtime.getRuntime().addShutdownHook(Thread { doMerge() }) + } + + @SubscribeEvent + fun onTick(event: ClientTickEvent) { + if (!hasRecentlyMerged && (System.currentTimeMillis() - lastMergeTime) > 60_000L) { + lastMergeTime = System.currentTimeMillis() + doMerge() + } + } + + fun logEntry(entry: LedgerEntry) { + if (shouldLog) + printToChat(entry) + Ledger.logger.info("Logging entry of type ${entry.transactionType}") + entries.add(entry.intoJson(currentProfile)) + commit() + } + + fun commit() { + try { + hasRecentlyMerged = false + file.writeText(gson.toJson(entries)) + } catch (ex: Exception) { + Ledger.logger.error("Could not save file", ex) + } + } + + val gson = Gson() + + val folder = Ledger.dataFolder + val file: File = run { + val date = SimpleDateFormat("yyyy.MM.dd").format(Date()) + + generateSequence(0) { it + 1 } + .map { + if (it == 0) + folder.resolve("$date.json") + else + folder.resolve("$date-$it.json") + } + .filter { !it.exists() } + .first() + } } data class LedgerEntry( - val transactionType: String, - val timestamp: Instant, - val totalTransactionCoins: Double, - val itemId: String? = null, - val itemAmount: Int? = null, + val transactionType: String, + val timestamp: Instant, + val totalTransactionCoins: Double, + val itemId: String? = null, + val itemAmount: Int? = null, ) { - fun intoJson(profileId: String?): JsonObject { - return JsonObject().apply { - addProperty("transactionType", transactionType) - addProperty("timestamp", timestamp.toEpochMilli().toString()) - addProperty("totalTransactionValue", totalTransactionCoins) - addProperty("itemId", itemId ?: "") - addProperty("itemAmount", itemAmount ?: 0) - addProperty("profileId", profileId) - addProperty( - "playerId", - (Minecraft.getMinecraft().thePlayer?.uniqueID?.toString() ?: lastKnownUUID) - .also { lastKnownUUID = it }) - } - } + fun intoJson(profileId: String?): JsonObject { + return JsonObject().apply { + addProperty("transactionType", transactionType) + addProperty("timestamp", timestamp.toEpochMilli().toString()) + addProperty("totalTransactionValue", totalTransactionCoins) + addProperty("itemId", itemId ?: "") + addProperty("itemAmount", itemAmount ?: 0) + addProperty("profileId", profileId) + addProperty( + "playerId", + (Minecraft.getMinecraft().thePlayer?.uniqueID?.toString() ?: lastKnownUUID) + .also { lastKnownUUID = it }) + } + } } var lastKnownUUID = "null" |