Search in sources :

Example 1 with SkinProperty

use of com.github.games647.changeskin.core.model.skin.SkinProperty in project ChangeSkin by games647.

the class MojangSkinApi method downloadSkin.

public Optional<SkinModel> downloadSkin(UUID ownerUUID) {
    if (crackedUUID.containsKey(ownerUUID)) {
        return Optional.empty();
    }
    // unsigned is needed in order to receive the signature
    String uuidString = UUIDTypeAdapter.toMojangId(ownerUUID);
    try {
        HttpURLConnection httpConnection = getConnection(String.format(SKIN_URL, uuidString));
        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) {
            crackedUUID.put(ownerUUID, new Object());
            return Optional.empty();
        }
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), StandardCharsets.UTF_8))) {
            TexturesModel texturesModel = gson.fromJson(reader, TexturesModel.class);
            SkinProperty[] properties = texturesModel.getProperties();
            if (properties != null && properties.length > 0) {
                SkinProperty propertiesModel = properties[0];
                // base64 encoded skin data
                String encodedSkin = propertiesModel.getValue();
                String signature = propertiesModel.getSignature();
                return Optional.of(SkinModel.createSkinFromEncoded(encodedSkin, signature));
            }
        }
    } catch (IOException ex) {
        logger.error("Tried downloading skin data of: {} from Mojang", ownerUUID, ex);
    }
    return Optional.empty();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) SkinProperty(com.github.games647.changeskin.core.model.skin.SkinProperty) TexturesModel(com.github.games647.changeskin.core.model.skin.TexturesModel)

Aggregations

SkinProperty (com.github.games647.changeskin.core.model.skin.SkinProperty)1 TexturesModel (com.github.games647.changeskin.core.model.skin.TexturesModel)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1