Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MineSkin UUID system and depecrate MineSkin legacy id system #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 77 additions & 3 deletions api/src/main/java/net/jitse/npclib/api/skin/MineSkinFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@
*/
public class MineSkinFetcher {

private static final String MINESKIN_API = "https://api.mineskin.org/get/id/";
private static final String MINESKIN_API = "https://api.mineskin.org/get/";
private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();

/**
* @deprecated
* Mineskin.org has deprecated its id system.
* Use MineSkinFetcher#fetchSkinAsync(String, Callback) instead.
*/
@Deprecated
public static void fetchSkinFromIdAsync(int id, Callback callback) {
EXECUTOR.execute(() -> {
try {
StringBuilder builder = new StringBuilder();
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + id).openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + "id/" + id).openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
Expand Down Expand Up @@ -55,10 +61,16 @@ public static void fetchSkinFromIdAsync(int id, Callback callback) {
});
}

/**
* @deprecated
* Mineskin.org has deprecated its id system.
* Use MineSkinFetcher#fetchSkinSync(String, Callback) instead.
*/
@Deprecated
public static void fetchSkinFromIdSync(int id, Callback callback) {
try {
StringBuilder builder = new StringBuilder();
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + id).openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + "id/" + id).openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
Expand All @@ -85,6 +97,68 @@ public static void fetchSkinFromIdSync(int id, Callback callback) {
}
}

public static void fetchSkinAsync(String uuid, Callback callback) {
EXECUTOR.execute(() -> {
try {
StringBuilder builder = new StringBuilder();
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + "uuid/" + uuid).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! (uuid: " + uuid + "). Message: " + exception.getMessage());
exception.printStackTrace();
callback.failed();
}
});
}

public static void fetchSkinSync(String uuid, Callback callback) {
try {
StringBuilder builder = new StringBuilder();
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + "uuid/" + uuid).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! (uuid: " + uuid + "). Message: " + exception.getMessage());
exception.printStackTrace();
callback.failed();
}
}

public interface Callback {

void call(Skin skinData);
Expand Down