aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-11-17 20:33:54 +0100
committerLinnea Gräf <nea@nea.moe>2025-11-17 20:33:54 +0100
commitdfe288cea0e5937eae218d1bc392066f18bf562f (patch)
tree9b00828191467122da89ce3904fd072ba9f64297 /src/main/kotlin
parentafcfbecad4bfe51e9c95247e289812c3c2ff147c (diff)
downloadFirmament-dfe288cea0e5937eae218d1bc392066f18bf562f.tar.gz
Firmament-dfe288cea0e5937eae218d1bc392066f18bf562f.tar.bz2
Firmament-dfe288cea0e5937eae218d1bc392066f18bf562f.zip
fix: skull property setting
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/util/mc/SkullItemData.kt92
1 files changed, 52 insertions, 40 deletions
diff --git a/src/main/kotlin/util/mc/SkullItemData.kt b/src/main/kotlin/util/mc/SkullItemData.kt
index cfe5d21..80028af 100644
--- a/src/main/kotlin/util/mc/SkullItemData.kt
+++ b/src/main/kotlin/util/mc/SkullItemData.kt
@@ -2,9 +2,12 @@
package moe.nea.firmament.util.mc
+import com.google.common.collect.Multimap
+import com.google.common.collect.Multimaps
import com.mojang.authlib.GameProfile
import com.mojang.authlib.minecraft.MinecraftProfileTexture
import com.mojang.authlib.properties.Property
+import com.mojang.authlib.properties.PropertyMap
import java.time.Instant
import java.util.UUID
import kotlinx.serialization.Serializable
@@ -21,66 +24,75 @@ import moe.nea.firmament.util.json.InstantAsLongSerializer
@Serializable
data class MinecraftProfileTextureKt(
- val url: String,
- val metadata: Map<String, String> = mapOf(),
+ val url: String,
+ val metadata: Map<String, String> = mapOf(),
)
@Serializable
data class MinecraftTexturesPayloadKt(
- val textures: Map<MinecraftProfileTexture.Type, MinecraftProfileTextureKt> = mapOf(),
- val profileId: UUID? = null,
- val profileName: String? = null,
- val isPublic: Boolean = true,
- val timestamp: Instant = Instant.now(),
+ val textures: Map<MinecraftProfileTexture.Type, MinecraftProfileTextureKt> = mapOf(),
+ val profileId: UUID? = null,
+ val profileName: String? = null,
+ val isPublic: Boolean = true,
+ val timestamp: Instant = Instant.now(),
)
-fun GameProfile.setTextures(textures: MinecraftTexturesPayloadKt) {
- val json = Firmament.json.encodeToString(textures)
- val encoded = java.util.Base64.getEncoder().encodeToString(json.encodeToByteArray())
- properties.put(propertyTextures, Property(propertyTextures, encoded))
+fun createSkullTextures(textures: MinecraftTexturesPayloadKt): PropertyMap {
+ val json = Firmament.json.encodeToString(textures)
+ val encoded = java.util.Base64.getEncoder().encodeToString(json.encodeToByteArray())
+ return PropertyMap(
+ Multimaps.forMap(mapOf(propertyTextures to Property(propertyTextures, encoded)))
+ )
}
private val propertyTextures = "textures"
fun ItemStack.setEncodedSkullOwner(uuid: UUID, encodedData: String) {
- assert(this.item == Items.PLAYER_HEAD)
- val gameProfile = GameProfile(uuid, "LameGuy123")
- gameProfile.properties.put(propertyTextures, Property(propertyTextures, encodedData.padToValidBase64()))
- this.set(DataComponents.PROFILE, ResolvableProfile.createResolved(gameProfile))
+ assert(this.item == Items.PLAYER_HEAD)
+ val gameProfile = GameProfile(
+ uuid, "LameGuy123",
+ PropertyMap(
+ Multimaps.forMap(
+ mapOf(propertyTextures to Property(propertyTextures, encodedData.padToValidBase64()))
+ )
+ )
+ )
+ this.set(DataComponents.PROFILE, ResolvableProfile.createResolved(gameProfile))
}
val arbitraryUUID = UUID.fromString("d3cb85e2-3075-48a1-b213-a9bfb62360c1")
fun createSkullItem(uuid: UUID, url: String) = ItemStack(Items.PLAYER_HEAD)
- .also { it.setSkullOwner(uuid, url) }
+ .also { it.setSkullOwner(uuid, url) }
fun ItemStack.setSkullOwner(uuid: UUID, url: String) {
- assert(this.item == Items.PLAYER_HEAD)
- val gameProfile = GameProfile(uuid, "nea89")
- gameProfile.setTextures(
- MinecraftTexturesPayloadKt(
- textures = mapOf(MinecraftProfileTexture.Type.SKIN to MinecraftProfileTextureKt(url)),
- profileId = uuid,
- profileName = "nea89",
- )
- )
- this.set(DataComponents.PROFILE, ResolvableProfile.createResolved(gameProfile))
+ assert(this.item == Items.PLAYER_HEAD)
+ val gameProfile = GameProfile(
+ uuid, "nea89", createSkullTextures(
+ MinecraftTexturesPayloadKt(
+ textures = mapOf(MinecraftProfileTexture.Type.SKIN to MinecraftProfileTextureKt(url)),
+ profileId = uuid,
+ profileName = "nea89",
+ )
+ )
+ )
+ this.set(DataComponents.PROFILE, ResolvableProfile.createResolved(gameProfile))
}
fun decodeProfileTextureProperty(property: Property): MinecraftTexturesPayloadKt? {
- assertTrueOr(property.name == propertyTextures) { return null }
- return try {
- var encodedF: String = property.value
- while (encodedF.length % 4 != 0 && encodedF.last() == '=') {
- encodedF = encodedF.substring(0, encodedF.length - 1)
- }
- val json = java.util.Base64.getDecoder().decode(encodedF).decodeToString()
- Firmament.json.decodeFromString<MinecraftTexturesPayloadKt>(json)
- } catch (e: Exception) {
- // Malformed profile data
- if (Firmament.DEBUG)
- e.printStackTrace()
- null
- }
+ assertTrueOr(property.name == propertyTextures) { return null }
+ return try {
+ var encodedF: String = property.value
+ while (encodedF.length % 4 != 0 && encodedF.last() == '=') {
+ encodedF = encodedF.substring(0, encodedF.length - 1)
+ }
+ val json = java.util.Base64.getDecoder().decode(encodedF).decodeToString()
+ Firmament.json.decodeFromString<MinecraftTexturesPayloadKt>(json)
+ } catch (e: Exception) {
+ // Malformed profile data
+ if (Firmament.DEBUG)
+ e.printStackTrace()
+ null
+ }
}