Improved MojangApiUtils

This commit is contained in:
NichtStudioCode 2021-07-17 11:57:12 +02:00
parent f1576d6983
commit 3c41fb721d
2 changed files with 7 additions and 30 deletions

@ -6,6 +6,8 @@ import com.google.gson.JsonParser;
import com.mojang.util.UUIDTypeAdapter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.UUID;
@ -15,10 +17,9 @@ public class MojangApiUtils {
private static final String NAME_AT_TIME_URL = "https://api.mojang.com/users/profiles/minecraft/%s?at=%s";
public static String[] getSkinData(UUID uuid, boolean requestSignature) throws IOException {
String url = String.format(SKIN_DATA_URL, uuid, requestSignature);
String content = WebUtils.readWebsiteContent(new URL(url));
JsonObject jsonObject = new JsonParser().parse(content).getAsJsonObject();
String url = String.format(SKIN_DATA_URL, uuid, !requestSignature);
Reader reader = new InputStreamReader(new URL(url).openConnection().getInputStream());
JsonObject jsonObject = new JsonParser().parse(reader).getAsJsonObject();
checkForError(jsonObject);
if (jsonObject.has("properties")) {
@ -38,9 +39,8 @@ public class MojangApiUtils {
public static UUID getUuidAtTime(String name, long timestamp) throws IOException {
String url = String.format(NAME_AT_TIME_URL, name, timestamp);
String content = WebUtils.readWebsiteContent(new URL(url));
JsonObject jsonObject = new JsonParser().parse(content).getAsJsonObject();
Reader reader = new InputStreamReader(new URL(url).openConnection().getInputStream());
JsonObject jsonObject = new JsonParser().parse(reader).getAsJsonObject();
checkForError(jsonObject);
if (jsonObject.has("id")) {

@ -1,23 +0,0 @@
package de.studiocode.invui.util;
import org.jetbrains.annotations.NotNull;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class WebUtils {
public static String readWebsiteContent(@NotNull URL url) throws IOException {
StringBuilder content = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
return content.substring(0, content.length() - 1);
}
}