Search in sources :

Example 1 with UUIDTypeAdapter

use of com.mojang.util.UUIDTypeAdapter in project launcher by MyFTB.

the class PlayerHeadScheme method getRemoteSkin.

private byte[] getRemoteSkin(String uuid) {
    try {
        HttpResponse response = HttpRequest.get("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid).execute().returnResponse();
        if (response.getStatusLine().getStatusCode() != 200) {
            return new byte[0];
        }
        Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
        JsonObject jsonObject = gson.fromJson(new InputStreamReader(response.getEntity().getContent()), JsonElement.class).getAsJsonObject();
        JsonArray properties = jsonObject.getAsJsonArray("properties");
        for (int i = 0; i < properties.size(); i++) {
            JsonObject property = properties.get(i).getAsJsonObject();
            if (!"textures".equals(property.get("name").getAsString())) {
                continue;
            }
            String json = new String(Base64.decodeBase64(property.get("value").getAsString()), Charsets.UTF_8);
            MinecraftTexturesPayload textures = gson.fromJson(json, MinecraftTexturesPayload.class);
            if (textures.getTextures() != null && textures.getTextures().containsKey(MinecraftProfileTexture.Type.SKIN)) {
                MinecraftProfileTexture skin = textures.getTextures().get(MinecraftProfileTexture.Type.SKIN);
                URI skinUri = new URI(skin.getUrl());
                if (skinUri.getHost().endsWith(".mojang.com") || skinUri.getHost().endsWith(".minecraft.net")) {
                    HttpResponse skinResponse = HttpRequest.get(skinUri).execute().returnResponse();
                    if (skinResponse.getStatusLine().getStatusCode() != 200) {
                        return new byte[0];
                    }
                    BufferedImage wholeSkin = ImageIO.read(skinResponse.getEntity().getContent());
                    BufferedImage head = wholeSkin.getSubimage(8, 8, 8, 8);
                    AffineTransform transform = new AffineTransform();
                    transform.scale(PlayerHeadScheme.targetSize / 8.0, PlayerHeadScheme.targetSize / 8.0);
                    AffineTransformOp scaleOperation = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
                    BufferedImage scaledHead = new BufferedImage(PlayerHeadScheme.targetSize, PlayerHeadScheme.targetSize, BufferedImage.TYPE_INT_RGB);
                    scaledHead = scaleOperation.filter(head, scaledHead);
                    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
                        ImageIO.write(scaledHead, "png", byteArrayOutputStream);
                        return byteArrayOutputStream.toByteArray();
                    }
                }
            }
        }
    } catch (IOException | URISyntaxException e) {
        PlayerHeadScheme.log.warn("Fehler bei der Skinabfrage", e);
    }
    return new byte[0];
}
Also used : UUIDTypeAdapter(com.mojang.util.UUIDTypeAdapter) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) HttpResponse(org.apache.http.HttpResponse) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) MinecraftTexturesPayload(com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) MinecraftProfileTexture(com.mojang.authlib.minecraft.MinecraftProfileTexture) URI(java.net.URI) BufferedImage(java.awt.image.BufferedImage) AffineTransformOp(java.awt.image.AffineTransformOp) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) AffineTransform(java.awt.geom.AffineTransform) UUID(java.util.UUID)

Example 2 with UUIDTypeAdapter

use of com.mojang.util.UUIDTypeAdapter in project MCCustomSkinLoader by xfl03.

the class MojangAPILoader method loadGameProfile.

public static GameProfile loadGameProfile(String username) throws Exception {
    // Doc (http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time)
    HttpResponce responce = HttpRequestUtil.makeHttpRequest(new HttpRequest("https://api.mojang.com/users/profiles/minecraft/" + username).setCacheTime(0));
    if (StringUtils.isEmpty(responce.content))
        return null;
    Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
    GameProfile gameProfile = gson.fromJson(responce.content, GameProfile.class);
    if (gameProfile.getId() == null)
        return null;
    return new GameProfile(gameProfile.getId(), gameProfile.getName());
}
Also used : HttpRequest(customskinloader.utils.HttpRequestUtil.HttpRequest) UUIDTypeAdapter(com.mojang.util.UUIDTypeAdapter) GsonBuilder(com.google.gson.GsonBuilder) GameProfile(com.mojang.authlib.GameProfile) HttpResponce(customskinloader.utils.HttpRequestUtil.HttpResponce) Gson(com.google.gson.Gson) UUID(java.util.UUID)

Example 3 with UUIDTypeAdapter

use of com.mojang.util.UUIDTypeAdapter in project MCCustomSkinLoader by xfl03.

the class MojangAPILoader method getTextures.

public static Map<MinecraftProfileTexture.Type, MinecraftProfileTexture> getTextures(GameProfile gameProfile) throws Exception {
    Property textureProperty = (Property) Iterables.getFirst(gameProfile.getProperties().get("textures"), null);
    if (textureProperty == null)
        return new HashMap();
    String value = textureProperty.getValue();
    if (StringUtils.isBlank(value))
        return new HashMap();
    String json = new String(Base64.decodeBase64(value), Charsets.UTF_8);
    Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
    MinecraftTexturesPayload result = gson.fromJson(json, MinecraftTexturesPayload.class);
    if (result == null || result.getTextures() == null)
        return new HashMap();
    return result.getTextures();
}
Also used : UUIDTypeAdapter(com.mojang.util.UUIDTypeAdapter) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) MinecraftTexturesPayload(com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload) UUID(java.util.UUID) Property(com.mojang.authlib.properties.Property)

Example 4 with UUIDTypeAdapter

use of com.mojang.util.UUIDTypeAdapter in project MCCustomSkinLoader by xfl03.

the class DynamicSkullManager method parseGameProfile.

private void parseGameProfile(GameProfile profile) {
    Property textureProperty = (Property) Iterables.getFirst(profile.getProperties().get("textures"), null);
    if (textureProperty == null) {
        staticTextures.put(profile, new HashMap());
        return;
    }
    String value = textureProperty.getValue();
    if (StringUtils.isBlank(value)) {
        staticTextures.put(profile, new HashMap());
        return;
    }
    String json = new String(Base64.decodeBase64(value), Charsets.UTF_8);
    Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create();
    SkullTexture result = gson.fromJson(json, SkullTexture.class);
    if (result == null) {
        staticTextures.put(profile, new HashMap());
        return;
    }
    staticTextures.put(profile, (result.textures == null || !result.textures.containsKey(Type.SKIN)) ? new HashMap() : parseTextures(result.textures));
    if (StringUtils.isNotEmpty(result.index)) {
        File indexFile = new File(CustomSkinLoader.DATA_DIR, result.index);
        try {
            String index = FileUtils.readFileToString(indexFile, Charsets.UTF_8);
            if (StringUtils.isNotEmpty(index)) {
                ArrayList<String> skins = CustomSkinLoader.GSON.fromJson(index, ArrayList.class);
                if (skins != null && !skins.isEmpty())
                    result.skins = skins;
            }
        } catch (Exception e) {
            CustomSkinLoader.logger.warning("Exception occurs while parsing index file: " + e.toString());
        }
    }
    if (!CustomSkinLoader.config.enableDynamicSkull || result.skins == null || result.skins.isEmpty())
        return;
    CustomSkinLoader.logger.info("Try to load Dynamic Skull: " + json);
    for (int i = 0; i < result.skins.size(); i++) {
        // check and cache skins
        String skin = result.skins.get(i);
        if (HttpUtil0.isLocal(skin)) {
            // Local Skin
            File skinFile = new File(CustomSkinLoader.DATA_DIR, skin);
            if (skinFile.isFile() && skinFile.length() > 0) {
                // Skin found
                String fakeUrl = HttpTextureUtil.getLocalFakeUrl(skin);
                result.skins.set(i, fakeUrl);
            } else {
                // Could not find skin
                result.skins.remove(i--);
                continue;
            }
        } else {
            // Online Skin
            HttpResponce responce = HttpRequestUtil.makeHttpRequest(new HttpRequest(skin).setCacheFile(HttpTextureUtil.getCacheFile(FilenameUtils.getBaseName(skin))).setCacheTime(0).setLoadContent(false));
            if (!responce.success) {
                // Could not load skin
                result.skins.remove(i--);
                continue;
            }
        }
    }
    if (result.skins.isEmpty()) {
        // Nothing loaded
        CustomSkinLoader.logger.info("Failed: Nothing loaded.");
        return;
    }
    result.interval = Math.max(result.interval, 50);
    if (result.fromZero)
        result.startTime = System.currentTimeMillis();
    result.period = result.interval * result.skins.size();
    CustomSkinLoader.logger.info("Successfully loaded Dynamic Skull: " + new Gson().toJson(result));
    dynamicTextures.put(profile, result);
    staticTextures.remove(profile);
}
Also used : HttpRequest(customskinloader.utils.HttpRequestUtil.HttpRequest) UUIDTypeAdapter(com.mojang.util.UUIDTypeAdapter) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) HttpResponce(customskinloader.utils.HttpRequestUtil.HttpResponce) UUID(java.util.UUID) Property(com.mojang.authlib.properties.Property) File(java.io.File)

Aggregations

Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 UUIDTypeAdapter (com.mojang.util.UUIDTypeAdapter)4 UUID (java.util.UUID)4 Property (com.mojang.authlib.properties.Property)2 MinecraftTexturesPayload (com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload)2 HttpRequest (customskinloader.utils.HttpRequestUtil.HttpRequest)2 HttpResponce (customskinloader.utils.HttpRequestUtil.HttpResponce)2 HashMap (java.util.HashMap)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 GameProfile (com.mojang.authlib.GameProfile)1 MinecraftProfileTexture (com.mojang.authlib.minecraft.MinecraftProfileTexture)1 AffineTransform (java.awt.geom.AffineTransform)1 AffineTransformOp (java.awt.image.AffineTransformOp)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1