feat: Add firmament waypoint import / export that remembers relative waypoints
This commit is contained in:
@@ -13,6 +13,7 @@ import kotlin.math.roundToInt
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
import net.minecraft.text.Text
|
||||
import net.minecraft.util.math.BlockPos
|
||||
|
||||
object FirmFormatters {
|
||||
|
||||
@@ -131,4 +132,7 @@ object FirmFormatters {
|
||||
return if (boolean == trueIsGood) text.lime() else text.red()
|
||||
}
|
||||
|
||||
fun formatPosition(position: BlockPos): Text {
|
||||
return Text.literal("x: ${position.x}, y: ${position.y}, z: ${position.z}")
|
||||
}
|
||||
}
|
||||
|
||||
63
src/main/kotlin/util/data/MultiFileDataHolder.kt
Normal file
63
src/main/kotlin/util/data/MultiFileDataHolder.kt
Normal file
@@ -0,0 +1,63 @@
|
||||
package moe.nea.firmament.util.data
|
||||
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlin.io.path.createDirectories
|
||||
import kotlin.io.path.deleteExisting
|
||||
import kotlin.io.path.exists
|
||||
import kotlin.io.path.extension
|
||||
import kotlin.io.path.listDirectoryEntries
|
||||
import kotlin.io.path.nameWithoutExtension
|
||||
import kotlin.io.path.readText
|
||||
import kotlin.io.path.writeText
|
||||
import moe.nea.firmament.Firmament
|
||||
|
||||
abstract class MultiFileDataHolder<T>(
|
||||
val dataSerializer: KSerializer<T>,
|
||||
val configName: String
|
||||
) { // TODO: abstract this + ProfileSpecificDataHolder
|
||||
val configDirectory = Firmament.CONFIG_DIR.resolve(configName)
|
||||
private var allData = readValues()
|
||||
protected fun readValues(): MutableMap<String, T> {
|
||||
if (!configDirectory.exists()) {
|
||||
configDirectory.createDirectories()
|
||||
}
|
||||
val profileFiles = configDirectory.listDirectoryEntries()
|
||||
return profileFiles
|
||||
.filter { it.extension == "json" }
|
||||
.mapNotNull {
|
||||
try {
|
||||
it.nameWithoutExtension to Firmament.json.decodeFromString(dataSerializer, it.readText())
|
||||
} catch (e: Exception) { /* Expecting IOException and SerializationException, but Kotlin doesn't allow multi catches*/
|
||||
IDataHolder.badLoads.add(configName)
|
||||
Firmament.logger.error(
|
||||
"Exception during loading of multi file data holder $it ($configName). This will reset that profiles config.",
|
||||
e
|
||||
)
|
||||
null
|
||||
}
|
||||
}.toMap().toMutableMap()
|
||||
}
|
||||
|
||||
fun save() {
|
||||
if (!configDirectory.exists()) {
|
||||
configDirectory.createDirectories()
|
||||
}
|
||||
val c = allData
|
||||
configDirectory.listDirectoryEntries().forEach {
|
||||
if (it.nameWithoutExtension !in c.mapKeys { it.toString() }) {
|
||||
it.deleteExisting()
|
||||
}
|
||||
}
|
||||
c.forEach { (name, value) ->
|
||||
val f = configDirectory.resolve("$name.json")
|
||||
f.writeText(Firmament.json.encodeToString(dataSerializer, value))
|
||||
}
|
||||
}
|
||||
|
||||
fun list(): Map<String, T> = allData
|
||||
val validPathRegex = "[a-zA-Z0-9_][a-zA-Z0-9\\-_.]*".toPattern()
|
||||
fun insert(name: String, value: T) {
|
||||
require(validPathRegex.matcher(name).matches()) { "Not a valid name: $name" }
|
||||
allData[name] = value
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user