aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util')
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/SkyblockId.kt1
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/item/SkullItemData.kt39
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/json/DashlessUUIDSerializer.kt24
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/json/InstantAsLongSerializer.kt20
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/uuid.kt10
5 files changed, 94 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/SkyblockId.kt b/src/main/kotlin/moe/nea/firmament/util/SkyblockId.kt
index f0998a7..b3c0e9d 100644
--- a/src/main/kotlin/moe/nea/firmament/util/SkyblockId.kt
+++ b/src/main/kotlin/moe/nea/firmament/util/SkyblockId.kt
@@ -56,6 +56,7 @@ value class SkyblockId(val neuItem: String) {
}
companion object {
+ val COINS: SkyblockId = SkyblockId("SKYBLOCK_COIN")
private val bazaarEnchantmentRegex = "ENCHANTMENT_(\\D*)_(\\d+)".toRegex()
val NULL: SkyblockId = SkyblockId("null")
}
diff --git a/src/main/kotlin/moe/nea/firmament/util/item/SkullItemData.kt b/src/main/kotlin/moe/nea/firmament/util/item/SkullItemData.kt
new file mode 100644
index 0000000..ad9d388
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/util/item/SkullItemData.kt
@@ -0,0 +1,39 @@
+@file:UseSerializers(DashlessUUIDSerializer::class, InstantAsLongSerializer::class)
+
+package moe.nea.firmament.util.item
+
+import com.mojang.authlib.GameProfile
+import com.mojang.authlib.minecraft.MinecraftProfileTexture
+import com.mojang.authlib.properties.Property
+import java.util.UUID
+import kotlinx.datetime.Clock
+import kotlinx.datetime.Instant
+import kotlinx.serialization.Serializable
+import kotlinx.serialization.UseSerializers
+import kotlinx.serialization.encodeToString
+import net.minecraft.client.texture.PlayerSkinProvider
+import moe.nea.firmament.Firmament
+import moe.nea.firmament.util.json.DashlessUUIDSerializer
+import moe.nea.firmament.util.json.InstantAsLongSerializer
+
+@Serializable
+data class MinecraftProfileTextureKt(
+ val url: String,
+ val metadata: Map<String, String> = mapOf(),
+)
+
+@Serializable
+data class MinecraftTexturesPayloadKt(
+ val textures: Map<MinecraftProfileTexture.Type, MinecraftProfileTextureKt>,
+ val profileId: UUID,
+ val profileName: String,
+ val isPublic: Boolean = true,
+ val timestamp: Instant = Clock.System.now(),
+)
+
+fun GameProfile.setTextures(textures: MinecraftTexturesPayloadKt) {
+ val json = Firmament.json.encodeToString(textures)
+ val encoded = java.util.Base64.getEncoder().encodeToString(json.encodeToByteArray())
+ properties.put(PlayerSkinProvider.TEXTURES, Property(PlayerSkinProvider.TEXTURES, encoded))
+}
+
diff --git a/src/main/kotlin/moe/nea/firmament/util/json/DashlessUUIDSerializer.kt b/src/main/kotlin/moe/nea/firmament/util/json/DashlessUUIDSerializer.kt
new file mode 100644
index 0000000..c95b343
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/util/json/DashlessUUIDSerializer.kt
@@ -0,0 +1,24 @@
+package moe.nea.firmament.util.json
+
+import java.util.UUID
+import kotlinx.serialization.KSerializer
+import kotlinx.serialization.Serializer
+import kotlinx.serialization.descriptors.PrimitiveKind
+import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
+import kotlinx.serialization.descriptors.SerialDescriptor
+import kotlinx.serialization.encoding.Decoder
+import kotlinx.serialization.encoding.Encoder
+import moe.nea.firmament.util.parseDashlessUUID
+
+object DashlessUUIDSerializer : KSerializer<UUID> {
+ override val descriptor: SerialDescriptor =
+ PrimitiveSerialDescriptor("DashlessUUIDSerializer", PrimitiveKind.STRING)
+
+ override fun deserialize(decoder: Decoder): UUID {
+ return parseDashlessUUID(decoder.decodeString())
+ }
+
+ override fun serialize(encoder: Encoder, value: UUID) {
+ encoder.encodeString(value.toString().replace("-", ""))
+ }
+}
diff --git a/src/main/kotlin/moe/nea/firmament/util/json/InstantAsLongSerializer.kt b/src/main/kotlin/moe/nea/firmament/util/json/InstantAsLongSerializer.kt
new file mode 100644
index 0000000..988c7dd
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/util/json/InstantAsLongSerializer.kt
@@ -0,0 +1,20 @@
+package moe.nea.firmament.util.json
+
+import kotlinx.datetime.Instant
+import kotlinx.serialization.KSerializer
+import kotlinx.serialization.descriptors.PrimitiveKind
+import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
+import kotlinx.serialization.descriptors.SerialDescriptor
+import kotlinx.serialization.encoding.Decoder
+import kotlinx.serialization.encoding.Encoder
+
+object InstantAsLongSerializer : KSerializer<Instant> {
+ override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("InstantAsLongSerializer", PrimitiveKind.LONG)
+ override fun deserialize(decoder: Decoder): Instant {
+ return Instant.fromEpochMilliseconds(decoder.decodeLong())
+ }
+
+ override fun serialize(encoder: Encoder, value: Instant) {
+ encoder.encodeLong(value.toEpochMilliseconds())
+ }
+}
diff --git a/src/main/kotlin/moe/nea/firmament/util/uuid.kt b/src/main/kotlin/moe/nea/firmament/util/uuid.kt
new file mode 100644
index 0000000..d5409fa
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/util/uuid.kt
@@ -0,0 +1,10 @@
+package moe.nea.firmament.util
+
+import java.math.BigInteger
+import java.util.UUID
+
+fun parseDashlessUUID(dashlessUuid: String): UUID {
+ val most = BigInteger(dashlessUuid.substring(0, 16), 16)
+ val least = BigInteger(dashlessUuid.substring(16, 32), 16)
+ return UUID(most.toLong(), least.toLong())
+}