Skip to content

Commit

Permalink
Merge pull request #130 from BlockVillage-net/master
Browse files Browse the repository at this point in the history
Added Sync MineSkinFetcher Function
  • Loading branch information
Jitse Boonstra committed Dec 27, 2020
2 parents a5e716c + 78e0c43 commit 3e36451
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions api/src/main/java/net/jitse/npclib/api/skin/MineSkinFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,36 @@ public static void fetchSkinFromIdAsync(int id, Callback callback) {
}
});
}

public static void fetchSkinFromIdSync(int id, Callback callback) {
try {
StringBuilder builder = new StringBuilder();
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + id).openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.connect();

Scanner scanner = new Scanner(httpURLConnection.getInputStream());
while (scanner.hasNextLine()) {
builder.append(scanner.nextLine());
}

scanner.close();
httpURLConnection.disconnect();

JsonObject jsonObject = (JsonObject) new JsonParser().parse(builder.toString());
JsonObject textures = jsonObject.get("data").getAsJsonObject().get("texture").getAsJsonObject();
String value = textures.get("value").getAsString();
String signature = textures.get("signature").getAsString();

callback.call(new Skin(value, signature));
} catch (IOException exception) {
Bukkit.getLogger().severe("Could not fetch skin! (Id: " + id + "). Message: " + exception.getMessage());
exception.printStackTrace();
callback.failed();
}
}

public interface Callback {

Expand Down

0 comments on commit 3e36451

Please sign in to comment.