75 lines
1.8 KiB
Kotlin
75 lines
1.8 KiB
Kotlin
package ru.dbotthepony.kstarbound.util
|
|
|
|
import com.google.common.collect.ImmutableList
|
|
import com.google.common.collect.ImmutableMap
|
|
import com.google.common.collect.ImmutableSet
|
|
import com.google.gson.JsonArray
|
|
import com.google.gson.JsonElement
|
|
import com.google.gson.JsonObject
|
|
import ru.dbotthepony.kommons.util.KOptional
|
|
import ru.dbotthepony.kstarbound.Starbound
|
|
import java.lang.ref.Reference
|
|
import java.util.*
|
|
import java.util.concurrent.CompletableFuture
|
|
import java.util.function.Consumer
|
|
import java.util.stream.Stream
|
|
|
|
fun String.sbIntern(): String {
|
|
return Starbound.STRINGS.intern(this)
|
|
}
|
|
|
|
fun String.sbIntern2(): String {
|
|
return Starbound.STRINGS.intern(this.intern())
|
|
}
|
|
|
|
fun traverseJsonPath(path: String?, element: JsonElement?): JsonElement? {
|
|
element ?: return null
|
|
path ?: return element
|
|
|
|
if (path.contains('.')) {
|
|
var current: JsonElement? = element
|
|
|
|
for (name in path.split('.')) {
|
|
if (current is JsonObject) {
|
|
current = current[name]
|
|
} else if (current is JsonArray) {
|
|
val toInt = name.toIntOrNull() ?: return null
|
|
if (toInt !in 0 until current.size()) return null
|
|
current = current.get(toInt)
|
|
} else {
|
|
return null
|
|
}
|
|
}
|
|
|
|
return current
|
|
} else {
|
|
if (element is JsonObject) {
|
|
return element[path]
|
|
} else if (element is JsonArray) {
|
|
val toInt = path.toIntOrNull() ?: return null
|
|
if (toInt !in 0 until element.size()) return null
|
|
return element[toInt]
|
|
} else {
|
|
return null
|
|
}
|
|
}
|
|
}
|
|
|
|
fun UUID.toStarboundString(): String {
|
|
val builder = StringBuilder(32)
|
|
val a = mostSignificantBits.toString(16)
|
|
val b = mostSignificantBits.toString(16)
|
|
|
|
for (i in a.length until 16)
|
|
builder.append("0")
|
|
|
|
builder.append(a)
|
|
|
|
for (i in b.length until 16)
|
|
builder.append("0")
|
|
|
|
builder.append(b)
|
|
|
|
return builder.toString()
|
|
}
|