Skip to content

Commit df2c634

Browse files
committed
StashLogger: Add logToFile option
This patch adds the logToFile option in the StashLogger module. This option, which is enabled by default, appends all the newfound stashes to a JSON file ($lambda_folder/stash-logger/servername.json). This is useful for when you are afk stash hunting and you would like to keep a log of all the found stashes :)
1 parent 414581b commit df2c634

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

Diff for: src/main/kotlin/com/lambda/client/module/modules/misc/StashLogger.kt

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package com.lambda.client.module.modules.misc
22

33
import com.lambda.client.commons.extension.synchronized
4+
import com.lambda.client.event.SafeClientEvent
45
import com.lambda.client.manager.managers.WaypointManager
56
import com.lambda.client.module.Category
67
import com.lambda.client.module.Module
78
import com.lambda.client.module.modules.movement.AutoWalk
89
import com.lambda.client.util.BaritoneUtils
10+
import com.lambda.client.util.FolderUtils
911
import com.lambda.client.util.TickTimer
1012
import com.lambda.client.util.TimeUnit
1113
import com.lambda.client.util.math.CoordinateConverter.asString
1214
import com.lambda.client.util.text.MessageSendHelper
1315
import com.lambda.client.util.threads.defaultScope
1416
import com.lambda.client.util.threads.onMainThread
1517
import com.lambda.client.util.threads.safeListener
18+
import kotlinx.coroutines.Dispatchers
1619
import kotlinx.coroutines.coroutineScope
1720
import kotlinx.coroutines.launch
21+
import kotlinx.coroutines.withContext
1822
import net.minecraft.client.audio.PositionedSoundRecord
1923
import net.minecraft.entity.Entity
2024
import net.minecraft.entity.item.EntityMinecartChest
@@ -24,7 +28,13 @@ import net.minecraft.init.SoundEvents
2428
import net.minecraft.tileentity.*
2529
import net.minecraft.util.math.BlockPos
2630
import net.minecraft.util.math.ChunkPos
31+
import net.minecraftforge.fml.common.FMLCommonHandler
2732
import net.minecraftforge.fml.common.gameevent.TickEvent
33+
import org.json.JSONArray
34+
import org.json.JSONObject
35+
import java.io.File
36+
import java.nio.file.Files
37+
import java.nio.file.Paths
2838
import java.text.SimpleDateFormat
2939
import java.util.*
3040
import kotlin.math.roundToInt
@@ -36,6 +46,7 @@ object StashLogger : Module(
3646
) {
3747
private val saveToWaypoints by setting("Save To Waypoints", true)
3848
private val logToChat by setting("Log To Chat", true)
49+
private val logToFile by setting("Log To File", true, description = "Logs found stashes in \".minecraft/lambda/stash-logger/servername.json\"")
3950
private val playSound by setting("Play Sound", true)
4051
private val logChests by setting("Chests", true)
4152
private val chestDensity by setting("Min Chests", 5, 1..20, 1, { logChests })
@@ -76,7 +87,7 @@ object StashLogger : Module(
7687
}
7788
}
7889

79-
private suspend fun notification() {
90+
private suspend fun SafeClientEvent.notification() {
8091
var found = false
8192

8293
for (chunkStats in chunkData.values) {
@@ -96,10 +107,36 @@ object StashLogger : Module(
96107
}
97108
}
98109

99-
if (logToChat) {
100-
val positionString = center.asString()
101-
val timeStr = SimpleDateFormat.getDateTimeInstance().format(Calendar.getInstance().time)
110+
val positionString = center.asString()
111+
val timeStr = SimpleDateFormat.getDateTimeInstance().format(Calendar.getInstance().time)
112+
if (logToChat)
102113
MessageSendHelper.sendChatMessage("$chatName Found $string at ($positionString) [$timeStr]")
114+
if(logToFile) {
115+
val fileName = mc.currentServerData?.serverIP ?: FMLCommonHandler.instance()?.minecraftServerInstance?.folderName
116+
withContext(Dispatchers.IO) {
117+
val stashLoggerFolder = Files.createDirectory(Paths.get(FolderUtils.lambdaFolder + "stash-logger"))
118+
val file = File(stashLoggerFolder.toString() + "/${fileName}.json")
119+
val json = when {
120+
file.exists() -> {
121+
val jsonString = file.readText(Charsets.UTF_8)
122+
JSONObject(jsonString)
123+
}
124+
else -> JSONObject()
125+
}
126+
val stashesJson = when {
127+
json.has("stashes") -> json.getJSONArray("stashes")
128+
else -> JSONArray()
129+
}
130+
131+
val stashJson = JSONObject()
132+
stashJson.put("date", timeStr)
133+
stashJson.put("location", positionString)
134+
stashJson.put("info", string)
135+
136+
stashesJson.put(stashJson)
137+
json.put("stashes", stashesJson)
138+
file.writeText(json.toString(4))
139+
};
103140
}
104141

105142
found = true

0 commit comments

Comments
 (0)