From d19b649d6d6e81d7efc94099b0de9e19d330af80 Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 10 Aug 2018 12:23:35 +0200 Subject: [PATCH] Fix and update error handling for bethlehem --- .../hdskins/skins/BethlehemSkinServer.java | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java b/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java index 39b5c3fd..5d5667b3 100644 --- a/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java +++ b/src/hdskins/java/com/voxelmodpack/hdskins/skins/BethlehemSkinServer.java @@ -2,11 +2,8 @@ package com.voxelmodpack.hdskins.skins; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; -import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; import com.mojang.authlib.GameProfile; -import com.mojang.authlib.minecraft.MinecraftProfileTexture; -import com.mojang.authlib.minecraft.MinecraftProfileTexture.Type; import com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload; import com.mojang.util.UUIDTypeAdapter; import com.voxelmodpack.hdskins.HDSkinManager; @@ -14,13 +11,10 @@ import net.minecraft.util.Session; import org.apache.commons.lang3.builder.ToStringBuilder; import java.io.IOException; -import java.net.URI; import java.util.Locale; import java.util.Map; import java.util.concurrent.CompletableFuture; -import javax.annotation.Nullable; - @ServerType("bethlehem") public class BethlehemSkinServer implements SkinServer { @@ -37,28 +31,26 @@ public class BethlehemSkinServer implements SkinServer { public MinecraftTexturesPayload loadProfileData(GameProfile profile) throws IOException { try (MoreHttpResponses response = new NetClient("GET", getPath(profile)).send()) { - JsonObject s = response.json(JsonObject.class); - - if (s.has("success") && s.get("success").getAsBoolean()) { - s = s.get("data").getAsJsonObject(); - return gson.fromJson(s, MinecraftTexturesPayload.class); + if (!response.ok()) { + throw new IOException(response.getResponse().getStatusLine().getReasonPhrase()); } - throw new IOException(s.get("error").getAsString()); + + return response.json(MinecraftTexturesPayload.class); } } @Override - public CompletableFuture uploadSkin(Session session, SkinUpload skin) { - URI image = skin.getImage(); - Map metadata = skin.getMetadata(); - MinecraftProfileTexture.Type type = skin.getType(); + public CompletableFuture uploadSkin(Session session, SkinUpload upload) { return CallableFutures.asyncFailableFuture(() -> { SkinServer.verifyServerConnection(session, SERVER_ID); - NetClient client = new NetClient("POST", address).putHeaders(createHeaders(session, type, image, metadata)); + // TODO: Fix this + NetClient client = new NetClient("POST", address); - if (image != null) { - client.putFile(type.toString().toLowerCase(Locale.US), "image/png", image); + client.putHeaders(createHeaders(session, upload)); + + if (upload.getImage() != null) { + client.putFile(upload.getType().toString().toLowerCase(Locale.US), "image/png", upload.getImage()); } try (MoreHttpResponses response = client.send()) { @@ -71,17 +63,17 @@ public class BethlehemSkinServer implements SkinServer { }, HDSkinManager.skinUploadExecutor); } - protected Map createHeaders(Session session, Type type, @Nullable URI image, Map metadata) { + protected Map createHeaders(Session session, SkinUpload upload) { Builder builder = ImmutableMap.builder() .put("accessToken", session.getToken()) .put("user", session.getUsername()) .put("uuid", UUIDTypeAdapter.fromUUID(session.getProfile().getId())) - .put("type", type.toString().toLowerCase(Locale.US)); + .put("type", upload.getType().toString().toLowerCase(Locale.US)); - if (image == null) { + if (upload.getImage() == null) { builder.put("clear", "1"); } else { - builder.put("model", metadata.getOrDefault("mode", "default")); + builder.put("model", upload.getMetadata().getOrDefault("mode", "default")); } return builder.build();