Skip to content

Commit

Permalink
Codec changes
Browse files Browse the repository at this point in the history
- Removed PlayerReference#CODEC
- Change toString representation of StandalonePlayerReference
- Change default StandalonePlayerReference CODEC to a mapping of the UUID codec, still supports old compound serialization
- Modified tests
  • Loading branch information
andantet committed Feb 21, 2024
1 parent 6d8ffde commit 19efe94
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ fabric_kotlin_version=1.10.17+kotlin.1.9.22
kache_version=1.0.5

# Mod Properties
mod_version=2.3.0
mod_version=2.4.0
maven_group=dev.andante
12 changes: 0 additions & 12 deletions src/main/java/dev/andante/audience/player/PlayerReference.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package dev.andante.audience.player;

import com.mojang.authlib.GameProfile;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.andante.audience.Audience;
import dev.andante.audience.AudienceInitializer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.UserCache;
import net.minecraft.util.Uuids;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -21,15 +18,6 @@
* A safe reference to a player.
*/
public interface PlayerReference extends Audience {
/**
* The codec of this class.
*/
Codec<PlayerReference> CODEC = RecordCodecBuilder.create(instance ->
instance.group(
Uuids.CODEC.fieldOf("uuid").forGetter(PlayerReference::getReferenceUuid)
).apply(instance, StandalonePlayerReference::new)
);

/**
* @return the UUID of the player
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package dev.andante.audience.player

import com.mojang.datafixers.util.Either
import com.mojang.serialization.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder
import net.minecraft.util.Uuids
import java.util.UUID
import java.util.function.Function

/**
* An instantiatiable, unattached player reference.
Expand Down Expand Up @@ -35,17 +37,33 @@ class StandalonePlayerReference(private val referenceImplUuid: UUID) : PlayerRef
}

override fun toString(): String {
return "$referenceUuid[$playerName]"
return "StandalonePlayerReference[$referenceUuid]"
}

companion object {
/**
* The codec for this class.
* The legacy codec for this class. Places the data within an unnecessary compound.
*/
val CODEC: Codec<StandalonePlayerReference> = RecordCodecBuilder.create { instance ->
private val LEGACY_CODEC: Codec<StandalonePlayerReference> = RecordCodecBuilder.create { instance ->
instance.group(
Uuids.CODEC.fieldOf("uuid").forGetter(PlayerReference::getReferenceUuid)
).apply(instance, ::StandalonePlayerReference)
}

/**
* The modern codec which maps the UUID codec to the standalone player reference.
*/
private val XMAP_CODEC: Codec<StandalonePlayerReference> = Uuids.CODEC.xmap(
::StandalonePlayerReference,
StandalonePlayerReference::getReferenceUuid
)

/**
* The codec for this class.
*/
val CODEC: Codec<StandalonePlayerReference> = Codec.either(XMAP_CODEC, LEGACY_CODEC).xmap(
{ it.map(Function.identity(), Function.identity()) },
{ Either.left(it) }
)
}
}
40 changes: 38 additions & 2 deletions src/test/kotlin/dev/andante/audience/test/AudienceTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package dev.andante.audience.test

import com.google.gson.JsonParser
import com.mojang.serialization.JsonOps
import dev.andante.audience.player.PlayerList
import dev.andante.audience.player.StandalonePlayerReference
import dev.andante.audience.resource.ResourcePack
import dev.andante.audience.resource.ResourcePackServer
import net.fabricmc.api.ModInitializer
Expand All @@ -10,11 +14,25 @@ import java.nio.file.Path
import kotlin.io.path.readBytes

object AudienceTest : ModInitializer {
private val byteArray = Path.of("resources.zip").readBytes()
private val logger = LoggerFactory.getLogger("Audience Test")

private val byteArray = try {
Path.of("resources.zip").readBytes()
} catch (exception: Exception) {
logger.error("No resources.zip", exception)
throw exception
}

private val resourcePack = ResourcePack(byteArray)
private val properties: ServerResourcePackProperties

private val otherByteArray = Path.of("resources2.zip").readBytes()
private val otherByteArray = try {
Path.of("resources2.zip").readBytes()
} catch (exception: Exception) {
logger.error("No resources2.zip", exception)
throw exception
}

private val otherResourcePack = ResourcePack(otherByteArray)
private val otherProperties: ServerResourcePackProperties

Expand All @@ -31,5 +49,23 @@ object AudienceTest : ModInitializer {
ServerLifecycleEvents.SERVER_STOPPING.register {
resourcePackServer.stopServer()
}

// test player reference codec IO (ensure possible migration of old uuids)
val input = "{\"uuid\":\"50c7e7c5-2407-4102-875f-e4b1f49ea61a\"}"
val inputJson = JsonParser.parseString(input)
val inputReferenceResult = StandalonePlayerReference.CODEC.decode(JsonOps.INSTANCE, inputJson)
val inputReference = inputReferenceResult.resultOrPartial(logger::error)
.orElseThrow()
.first

println(inputReference)

val output = StandalonePlayerReference.CODEC.encodeStart(JsonOps.INSTANCE, inputReference)
val outputJson = output.resultOrPartial(logger::error).orElseThrow()
println(outputJson)

val list = PlayerList(listOf(inputReference))
val listJson = PlayerList.CODEC.encodeStart(JsonOps.INSTANCE, list).resultOrPartial(logger::error).orElseThrow()
println(listJson)
}
}

0 comments on commit 19efe94

Please sign in to comment.