mirror of
https://github.com/Suiranoil/SkinRestorer.git
synced 2026-01-16 04:42:12 +00:00
add proper minecraft textures parsing to MojangSkinProvider
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
package net.lionarius.skinrestorer.skin.provider;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.mojang.authlib.properties.Property;
|
||||
import net.lionarius.skinrestorer.skin.SkinVariant;
|
||||
import net.lionarius.skinrestorer.util.JsonUtils;
|
||||
import net.lionarius.skinrestorer.util.PlayerUtils;
|
||||
import net.lionarius.skinrestorer.util.Result;
|
||||
import net.lionarius.skinrestorer.util.WebUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
@@ -45,14 +46,36 @@ public final class MojangSkinProvider implements SkinProvider {
|
||||
var uuid = MojangSkinProvider.getUuid(username);
|
||||
var profile = MojangSkinProvider.getMojangProfile(uuid);
|
||||
|
||||
var texture = profile.getAsJsonArray("properties").get(0).getAsJsonObject();
|
||||
var properties = profile.getAsJsonArray("properties");
|
||||
var textures = MojangSkinProvider.findTexturesProperty(properties);
|
||||
|
||||
return Result.ofNullable(new Property(PlayerUtils.TEXTURES_KEY, texture.get("value").getAsString(), texture.get("signature").getAsString()));
|
||||
return Result.ofNullable(textures);
|
||||
} catch (Exception e) {
|
||||
return Result.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static Property findTexturesProperty(JsonArray properties) {
|
||||
Property textures = null;
|
||||
for (var property : properties) {
|
||||
var propertyObject = property.getAsJsonObject();
|
||||
if (propertyObject == null)
|
||||
continue;
|
||||
|
||||
try {
|
||||
textures = JsonUtils.fromJson(propertyObject, Property.class);
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
// ignored
|
||||
}
|
||||
}
|
||||
|
||||
if (textures == null)
|
||||
throw new IllegalStateException("no textures in profile");
|
||||
|
||||
return textures;
|
||||
}
|
||||
|
||||
private static String getUuid(final String name) throws IOException {
|
||||
var request = HttpRequest.newBuilder()
|
||||
.uri(MojangSkinProvider.API_URI
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package net.lionarius.skinrestorer.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.*;
|
||||
import com.mojang.authlib.properties.Property;
|
||||
import net.lionarius.skinrestorer.SkinRestorer;
|
||||
|
||||
@@ -21,6 +18,10 @@ public final class JsonUtils {
|
||||
return GSON.fromJson(json, clazz);
|
||||
}
|
||||
|
||||
public static <T> T fromJson(JsonElement json, Class<T> clazz) {
|
||||
return GSON.fromJson(json, clazz);
|
||||
}
|
||||
|
||||
public static <T> T fromJson(String json, Type type) {
|
||||
return GSON.fromJson(json, type);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user