From 38fa6c24843240ad38d299e59ba14f5840f7d6b9 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 8 Jul 2018 11:01:59 +0200 Subject: [PATCH] Merge with Killjoy's changes --- .../hdskins/skins/BethlehemSkinServer.java | 49 ++++++------------- .../hdskins/skins/LegacySkinServer.java | 4 +- .../hdskins/skins/SkinServer.java | 3 +- .../hdskins/skins/ValhallaSkinServer.java | 1 + 4 files changed, 17 insertions(+), 40 deletions(-) diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java index b335e99a..850e6907 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java @@ -4,17 +4,16 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; +import java.net.URI; import java.net.URL; -import java.nio.file.Path; import java.util.Locale; import java.util.Map; import java.util.Optional; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.annotation.Nullable; - import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -22,8 +21,6 @@ import org.apache.logging.log4j.Logger; import com.google.common.base.MoreObjects; import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; @@ -54,7 +51,8 @@ public class BethlehemSkinServer implements SkinServer { private static final Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); - public MinecraftTexturesPayload getProfileData(GameProfile profile) { + @Override + public Optional loadProfileData(GameProfile profile) { String url = getPath(profile); @@ -89,7 +87,7 @@ public class BethlehemSkinServer implements SkinServer { if (s.has("success") && s.get("success").getAsBoolean()) { s = s.get("data").getAsJsonObject(); - return gson.fromJson(s, MinecraftTexturesPayload.class); + return Optional.ofNullable(gson.fromJson(s, MinecraftTexturesPayload.class)); } } catch (IOException e) { logger.trace("Couldn't reach skin server for {} at {}", profile.getName(), url, e); @@ -101,46 +99,27 @@ public class BethlehemSkinServer implements SkinServer { IOUtils.closeQuietly(reader); } - return null; + return Optional.empty(); } - public MinecraftProfileTexture getPreview(Type type, GameProfile profile) { - MinecraftTexturesPayload payload = getProfileData(profile); + @Override + public CompletableFuture uploadSkin(Session session, URI image, Type type, Map metadata) { - if (payload != null && payload.getTextures().containsKey(type)) { - return payload.getTextures().get(type); + if (Strings.isNullOrEmpty(gateway)) { + return CallableFutures.failedFuture(new NullPointerException("gateway url is blank")); } - return null; - } - - @Override - public final Optional loadProfileData(GameProfile profile) { - return Optional.ofNullable(getProfileData(profile)); - } - - @Override - public final Optional getPreviewTexture(Type type, GameProfile profile) { - return Optional.ofNullable(getPreview(type, profile)); - } - - @Override - public ListenableFuture uploadSkin(Session session, @Nullable Path image, MinecraftProfileTexture.Type type, boolean thinSkinType) { - - if (Strings.isNullOrEmpty(gateway)) - return Futures.immediateFailedFuture(new NullPointerException("gateway url is blank")); - - return HDSkinManager.skinUploadExecutor.submit(() -> { + return CallableFutures.asyncFailableFuture(() -> { verifyServerConnection(session, SERVER_ID); - Map data = image == null ? getClearData(session, type) : getUploadData(session, type, (thinSkinType ? "slim" : "default"), image); + Map data = image == null ? getClearData(session, type) : getUploadData(session, type, metadata.getOrDefault("mode", "default"), image); ThreadMultipartPostUpload upload = new ThreadMultipartPostUpload(gateway, data); String response = upload.uploadMultipart(); return new SkinUploadResponse(response.equalsIgnoreCase("OK"), response); - }); + }, HDSkinManager.skinUploadExecutor); } protected static ImmutableMap.Builder getData(Session session, MinecraftProfileTexture.Type type) { @@ -157,7 +136,7 @@ public class BethlehemSkinServer implements SkinServer { .build(); } - protected static Map getUploadData(Session session, MinecraftProfileTexture.Type type, String model, Path skinFile) { + protected static Map getUploadData(Session session, MinecraftProfileTexture.Type type, String model, URI skinFile) { return getData(session, type) .put("model", model) .put(type.toString().toLowerCase(Locale.US), skinFile) diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/LegacySkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/LegacySkinServer.java index a15b4557..c4644982 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/LegacySkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/LegacySkinServer.java @@ -93,9 +93,7 @@ public class LegacySkinServer implements SkinServer { } @Override - public CompletableFuture uploadSkin(Session session, @Nullable URI image, - MinecraftProfileTexture.Type type, Map metadata) { - + public CompletableFuture uploadSkin(Session session, @Nullable URI image, MinecraftProfileTexture.Type type, Map metadata) { if (Strings.isNullOrEmpty(this.gateway)) { return CallableFutures.failedFuture(new NullPointerException("gateway url is blank")); } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinServer.java index 6310aba6..a38f2069 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/SkinServer.java @@ -27,7 +27,6 @@ public interface SkinServer { return loadProfileData(profile).map(MinecraftTexturesPayload::getTextures).orElse(Collections.emptyMap()); } - CompletableFuture uploadSkin(Session session, @Nullable URI image, - MinecraftProfileTexture.Type type, Map metadata); + CompletableFuture uploadSkin(Session session, @Nullable URI image, MinecraftProfileTexture.Type type, Map metadata); } diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/ValhallaSkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/ValhallaSkinServer.java index ffd4c8c4..bd40e19b 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/ValhallaSkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/ValhallaSkinServer.java @@ -46,6 +46,7 @@ public class ValhallaSkinServer implements SkinServer { @Expose private final String address; + private final Gson gson = new GsonBuilder() .registerTypeAdapter(UUID.class, new UUIDTypeAdapter()) .create();