aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/UUIDUtil.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/UUIDUtil.kt')
-rw-r--r--src/main/kotlin/moe/nea/ledger/UUIDUtil.kt39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/UUIDUtil.kt b/src/main/kotlin/moe/nea/ledger/UUIDUtil.kt
new file mode 100644
index 0000000..ddfb8c2
--- /dev/null
+++ b/src/main/kotlin/moe/nea/ledger/UUIDUtil.kt
@@ -0,0 +1,39 @@
+package moe.nea.ledger
+
+import com.mojang.util.UUIDTypeAdapter
+import io.azam.ulidj.ULID
+import net.minecraft.client.Minecraft
+import java.time.Instant
+import java.util.UUID
+import kotlin.random.Random
+
+object UUIDUtil {
+ @JvmInline
+ value class ULIDWrapper(
+ val wrapped: String
+ ) {
+ init {
+ require(ULID.isValid(wrapped))
+ }
+ }
+
+ fun parseDashlessUuid(string: String) = UUIDTypeAdapter.fromString(string)
+ val NIL_UUID = UUID(0L, 0L)
+ fun getPlayerUUID(): UUID {
+ val currentUUID = Minecraft.getMinecraft().thePlayer?.uniqueID
+ ?: Minecraft.getMinecraft().session?.playerID?.let(::parseDashlessUuid)
+ ?: lastKnownUUID
+ lastKnownUUID = currentUUID
+ return currentUUID
+ }
+
+ fun createULIDAt(timestamp: Instant): ULIDWrapper {
+ return ULIDWrapper(ULID.generate(
+ timestamp.toEpochMilli(),
+ Random.nextBytes(10)
+ ))
+ }
+
+ private var lastKnownUUID: UUID = NIL_UUID
+
+} \ No newline at end of file