Skip to content

Commit

Permalink
Fix skin fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSchlueter committed Jan 22, 2025
1 parent b40666f commit b1ed494
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions api/src/main/java/de/oliver/fancynpcs/api/utils/SkinFetcher.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.oliver.fancynpcs.api.utils;

import com.destroystokyo.paper.profile.ProfileProperty;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import de.oliver.fancylib.UUIDFetcher;
Expand All @@ -15,9 +16,9 @@

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Optional;
Expand All @@ -31,8 +32,10 @@
*/
@Deprecated
public final class SkinFetcher {

@Deprecated
public static final Map<String, SkinData> skinCache = new ConcurrentHashMap<>(); // identifier -> skinData
private final static Gson GSON = new Gson();

private SkinFetcher() {
}
Expand Down Expand Up @@ -171,13 +174,37 @@ public static CompletableFuture<SkinData> fetchSkinByURL(String skinURL) {
try {
URL url = new URL("https://api.mineskin.org/generate/url");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoOutput(true);

// Send POST data
DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream());
outputStream.writeBytes("url=" + URLEncoder.encode(skinURL, StandardCharsets.UTF_8));
outputStream.writeBytes(GSON.toJson(new MineSkinRequest(skinURL, 0, "", "auto")));
outputStream.close();

String json = new Scanner(conn.getInputStream(), StandardCharsets.UTF_8).useDelimiter("\\A").next();
// Handle response
int responseCode = conn.getResponseCode();
InputStream inputStream = null;

if (responseCode >= 200 && responseCode < 300) {
inputStream = conn.getInputStream();
} else if (responseCode >= 400) {
inputStream = conn.getErrorStream();
String errorBody = new Scanner(inputStream, StandardCharsets.UTF_8).useDelimiter("\\A").next();
FancyNpcsPlugin.get().getFancyLogger().warn("4xx Error Response Body: " + errorBody);
inputStream.close();
return null;
}

if (inputStream == null) {
FancyNpcsPlugin.get().getFancyLogger().warn("Failed to fetch skin data for URL " + skinURL);
return null;
}

String json = new Scanner(inputStream, StandardCharsets.UTF_8).useDelimiter("\\A").next();
inputStream.close();

JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(json).getAsJsonObject();

Expand Down Expand Up @@ -278,4 +305,7 @@ public boolean isExpired() {
return timeToLive > 0 && System.currentTimeMillis() - lastUpdated > timeToLive;
}
}

private record MineSkinRequest(String url, int visibility, String name, String variant) {
}
}

0 comments on commit b1ed494

Please sign in to comment.