Skip to content

Commit 3e36451

Browse files
author
Jitse Boonstra
authored
Merge pull request #130 from BlockVillage-net/master
Added Sync MineSkinFetcher Function
2 parents a5e716c + 78e0c43 commit 3e36451

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

api/src/main/java/net/jitse/npclib/api/skin/MineSkinFetcher.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ public static void fetchSkinFromIdAsync(int id, Callback callback) {
5454
}
5555
});
5656
}
57+
58+
public static void fetchSkinFromIdSync(int id, Callback callback) {
59+
try {
60+
StringBuilder builder = new StringBuilder();
61+
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + id).openConnection();
62+
httpURLConnection.setRequestMethod("GET");
63+
httpURLConnection.setDoOutput(true);
64+
httpURLConnection.setDoInput(true);
65+
httpURLConnection.connect();
66+
67+
Scanner scanner = new Scanner(httpURLConnection.getInputStream());
68+
while (scanner.hasNextLine()) {
69+
builder.append(scanner.nextLine());
70+
}
71+
72+
scanner.close();
73+
httpURLConnection.disconnect();
74+
75+
JsonObject jsonObject = (JsonObject) new JsonParser().parse(builder.toString());
76+
JsonObject textures = jsonObject.get("data").getAsJsonObject().get("texture").getAsJsonObject();
77+
String value = textures.get("value").getAsString();
78+
String signature = textures.get("signature").getAsString();
79+
80+
callback.call(new Skin(value, signature));
81+
} catch (IOException exception) {
82+
Bukkit.getLogger().severe("Could not fetch skin! (Id: " + id + "). Message: " + exception.getMessage());
83+
exception.printStackTrace();
84+
callback.failed();
85+
}
86+
}
5787

5888
public interface Callback {
5989

0 commit comments

Comments
 (0)