diff options
author | Linnea Gräf <nea@nea.moe> | 2024-12-06 19:46:33 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-12-06 19:46:33 +0100 |
commit | 6bdc91b4bda1497e785af695769acae91e8e7064 (patch) | |
tree | 60dede243612d386ae4217e97979062e2b2a67b7 /src/main/kotlin | |
parent | f7507f384459b57460af899bf9ceae4f52f1ea21 (diff) | |
download | LocalTransactionLedger-6bdc91b4bda1497e785af695769acae91e8e7064.tar.gz LocalTransactionLedger-6bdc91b4bda1497e785af695769acae91e8e7064.tar.bz2 LocalTransactionLedger-6bdc91b4bda1497e785af695769acae91e8e7064.zip |
feat: Add logging toggle to options
Diffstat (limited to 'src/main/kotlin')
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/ConfigCommand.kt | 31 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/Ledger.kt | 33 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/LedgerLogger.kt | 220 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/LogChatCommand.kt | 27 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/config/DebugOptions.kt | 13 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/config/LedgerConfig.kt | 6 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/config/SynchronizationOptions.kt (renamed from src/main/kotlin/moe/nea/ledger/config/Synchronization.kt) | 2 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/utils/DIProvider.kt | 4 |
8 files changed, 181 insertions, 155 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/ConfigCommand.kt b/src/main/kotlin/moe/nea/ledger/ConfigCommand.kt new file mode 100644 index 0000000..5222e3b --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/ConfigCommand.kt @@ -0,0 +1,31 @@ +package moe.nea.ledger + +import io.github.notenoughupdates.moulconfig.common.IMinecraft +import net.minecraft.command.CommandBase +import net.minecraft.command.ICommandSender + +class ConfigCommand : CommandBase() { + override fun canCommandSenderUseCommand(sender: ICommandSender?): Boolean { + return true + } + + override fun getCommandName(): String { + return "ledger" + } + + override fun getCommandUsage(sender: ICommandSender?): String { + return "" + } + + override fun processCommand(sender: ICommandSender?, args: Array<out String>) { + val editor = Ledger.managedConfig.getEditor() + editor.search(args.joinToString(" ")) + Ledger.runLater { + IMinecraft.instance.openWrappedScreen(editor) + } + } + + override fun getCommandAliases(): List<String> { + return listOf("moneyledger") + } +}
\ 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 521fd9d..54ad6e7 100644 --- a/src/main/kotlin/moe/nea/ledger/Ledger.kt +++ b/src/main/kotlin/moe/nea/ledger/Ledger.kt @@ -1,6 +1,5 @@ package moe.nea.ledger -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 @@ -17,8 +16,7 @@ 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 +import net.minecraft.command.ICommand import net.minecraftforge.client.ClientCommandHandler import net.minecraftforge.client.event.ClientChatReceivedEvent import net.minecraftforge.common.MinecraftForge @@ -83,31 +81,6 @@ class Ledger { logger.info("Initializing ledger") Database.init() - ClientCommandHandler.instance.registerCommand(object : CommandBase() { - override fun canCommandSenderUseCommand(sender: ICommandSender?): Boolean { - return true - } - - override fun getCommandName(): String { - return "ledger" - } - - override fun getCommandUsage(sender: ICommandSender?): String { - return "" - } - - override fun processCommand(sender: ICommandSender?, args: Array<out String>) { - val editor = managedConfig.getEditor() - editor.search(args.joinToString(" ")) - runLater { - IMinecraft.instance.openWrappedScreen(editor) - } - } - - override fun getCommandAliases(): List<String> { - return listOf("moneyledger") - } - }) val di = DI() di.registerSingleton(this) di.registerInjectableClasses( @@ -122,9 +95,13 @@ class Ledger { BitsShopDetection::class.java, MinionDetection::class.java, NpcDetection::class.java, + LogChatCommand::class.java, + ConfigCommand::class.java, ) di.instantiateAll() di.getAllInstances().forEach(MinecraftForge.EVENT_BUS::register) + di.getAllInstances().filterIsInstance<ICommand>() + .forEach { ClientCommandHandler.instance.registerCommand(it) } } var lastJoin = -1L 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" diff --git a/src/main/kotlin/moe/nea/ledger/LogChatCommand.kt b/src/main/kotlin/moe/nea/ledger/LogChatCommand.kt new file mode 100644 index 0000000..0b49a9a --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/LogChatCommand.kt @@ -0,0 +1,27 @@ +package moe.nea.ledger + +import moe.nea.ledger.utils.Inject +import net.minecraft.command.CommandBase +import net.minecraft.command.ICommandSender + +class LogChatCommand : CommandBase() { + @Inject + lateinit var logger: LedgerLogger + + override fun getCommandName(): String { + return "ledgerlogchat" + } + + override fun canCommandSenderUseCommand(sender: ICommandSender?): Boolean { + return true + } + + override fun getCommandUsage(sender: ICommandSender?): String { + return "" + } + + override fun processCommand(sender: ICommandSender?, args: Array<out String>?) { + logger.shouldLog = !logger.shouldLog + logger.printOut("§eLedger logging toggled " + (if (logger.shouldLog) "§aon" else "§coff") + "§e.") + } +}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/ledger/config/DebugOptions.kt b/src/main/kotlin/moe/nea/ledger/config/DebugOptions.kt new file mode 100644 index 0000000..fd5ed3d --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/config/DebugOptions.kt @@ -0,0 +1,13 @@ +package moe.nea.ledger.config + +import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean +import io.github.notenoughupdates.moulconfig.annotations.ConfigOption + +class DebugOptions { + @ConfigOption(name = "Log entries to chat", + desc = "Appends all logged entries into the chat as they are logged. This option does not persist on restarts.") + @Transient + @ConfigEditorBoolean + @JvmField + var logEntries = false +} diff --git a/src/main/kotlin/moe/nea/ledger/config/LedgerConfig.kt b/src/main/kotlin/moe/nea/ledger/config/LedgerConfig.kt index 7ba5abf..367f1e2 100644 --- a/src/main/kotlin/moe/nea/ledger/config/LedgerConfig.kt +++ b/src/main/kotlin/moe/nea/ledger/config/LedgerConfig.kt @@ -22,6 +22,10 @@ class LedgerConfig : Config() { @Category(name = "Synchronization", desc = "") @JvmField - val synchronization = Synchronization() + val synchronization = SynchronizationOptions() + + @Category(name = "Debug", desc = "") + @JvmField + val debug = DebugOptions() }
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/ledger/config/Synchronization.kt b/src/main/kotlin/moe/nea/ledger/config/SynchronizationOptions.kt index e8d5906..b8c740b 100644 --- a/src/main/kotlin/moe/nea/ledger/config/Synchronization.kt +++ b/src/main/kotlin/moe/nea/ledger/config/SynchronizationOptions.kt @@ -3,7 +3,7 @@ package moe.nea.ledger.config import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean import io.github.notenoughupdates.moulconfig.annotations.ConfigOption -class Synchronization { +class SynchronizationOptions { @ConfigOption(name = "Test Option", desc = "Test Description") @ConfigEditorBoolean @JvmField diff --git a/src/main/kotlin/moe/nea/ledger/utils/DIProvider.kt b/src/main/kotlin/moe/nea/ledger/utils/DIProvider.kt index 3cedf40..98d1bbc 100644 --- a/src/main/kotlin/moe/nea/ledger/utils/DIProvider.kt +++ b/src/main/kotlin/moe/nea/ledger/utils/DIProvider.kt @@ -30,9 +30,7 @@ fun interface DIProvider<T : Any> : BaseDIProvider<T, Unit> { }.toTypedArray() val instance = cons.newInstance(*typArgs) for (it in clazz.fields) { - if (it.getAnnotation(Inject::class.java) != null) { - continue - } + if (it.getAnnotation(Inject::class.java) == null) continue it.set(instance, di.provide(it.type, it)) } instance |