diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/mc_version/MinecraftVersionList.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/mc_version/MinecraftVersionList.java index da51dc2..29af90e 100644 --- a/pandalib-core/src/main/java/fr/pandacube/lib/core/mc_version/MinecraftVersionList.java +++ b/pandalib-core/src/main/java/fr/pandacube/lib/core/mc_version/MinecraftVersionList.java @@ -1,5 +1,18 @@ package fr.pandacube.lib.core.mc_version; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import com.google.gson.TypeAdapterFactory; +import com.google.gson.internal.bind.TreeTypeAdapter; +import com.google.gson.reflect.TypeToken; +import fr.pandacube.lib.core.json.Json; + +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -14,6 +27,10 @@ public record MinecraftVersionList( Map protocolOfVersion, Map> versionsOfProtocol ) { + static { + Json.registerTypeAdapterFactory(MinecraftVersionListAdapter.FACTORY); + } + /** * Creates an empty {@link MinecraftVersionList}. */ @@ -32,4 +49,35 @@ public record MinecraftVersionList( versions.add(versionId); versions.sort(MinecraftVersionUtil::compareVersions); } + + + /** + * Gson Adapter that ensure the data in {@link MinecraftVersionList} is sorted correctly when deserializing. + */ + public static class MinecraftVersionListAdapter implements JsonSerializer, JsonDeserializer { + public static final TypeAdapterFactory FACTORY = TreeTypeAdapter.newTypeHierarchyFactory(MinecraftVersionList.class, new MinecraftVersionListAdapter()); + + private static final TypeToken> MAP_STR_INT_TYPE = new TypeToken<>() { }; + private static final TypeToken>> MAP_INT_LIST_STRING_TYPE = new TypeToken<>() { }; + @Override + public MinecraftVersionList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + if (!(json instanceof JsonObject jsonObj)) + throw new JsonParseException("Expected JsonObject, got " + json.getClass().getSimpleName() + "."); + MinecraftVersionList mvList = new MinecraftVersionList(); + mvList.protocolOfVersion.putAll(context.deserialize(jsonObj.get("protocolOfVersion"), MAP_STR_INT_TYPE.getType())); + mvList.versionsOfProtocol.putAll(context.deserialize(jsonObj.get("versionsOfProtocol"), MAP_INT_LIST_STRING_TYPE.getType())); + for (List versionLists : mvList.versionsOfProtocol.values()) { + versionLists.sort(MinecraftVersionUtil::compareVersions); + } + return mvList; + } + + @Override + public JsonElement serialize(MinecraftVersionList src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject obj = new JsonObject(); + obj.add("protocolOfVersion", context.serialize(src.protocolOfVersion)); + obj.add("versionsOfProtocol", context.serialize(src.versionsOfProtocol)); + return obj; + } + } } diff --git a/pandalib-core/src/main/java/fr/pandacube/lib/core/mc_version/ProtocolVersion.java b/pandalib-core/src/main/java/fr/pandacube/lib/core/mc_version/ProtocolVersion.java index 8a5e512..184c608 100644 --- a/pandalib-core/src/main/java/fr/pandacube/lib/core/mc_version/ProtocolVersion.java +++ b/pandalib-core/src/main/java/fr/pandacube/lib/core/mc_version/ProtocolVersion.java @@ -216,6 +216,14 @@ public class ProtocolVersion implements Comparable { return displayOptimizedListOfVersions(List.of(this), finalWordSeparator); } + public String getFirstVersion() { + return versions.get(0); + } + + public String getLastVersion() { + return versions.get(versions.size() - 1); + } + @Override public boolean equals(Object o) { return o instanceof ProtocolVersion pv && protocolVersionNumber == pv.protocolVersionNumber;