Switch to GSON since mojang lib does not exist in 1.14

This commit is contained in:
nossr50 2019-04-23 02:12:22 -07:00
parent d862f7f779
commit 09cba965d3

View File

@ -1,11 +1,10 @@
package com.gmail.nossr50.util.uuid;
import com.google.common.collect.ImmutableList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
@ -17,7 +16,6 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
private static final int PROFILES_PER_REQUEST = 50;
private static final long RATE_LIMIT = 100L;
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
private final JSONParser jsonParser = new JSONParser();
private final List<String> names;
private final boolean rateLimiting;
@ -35,13 +33,22 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
for (int i = 0; i < requests; i++) {
HttpURLConnection connection = createConnection();
String body = JSONArray.toJSONString(names.subList(i * PROFILES_PER_REQUEST, Math.min((i + 1) * PROFILES_PER_REQUEST, names.size())));
List<String> nameSubList = names.subList(i * PROFILES_PER_REQUEST, Math.min((i + 1) * PROFILES_PER_REQUEST, names.size()));
JsonArray array = new JsonArray();
for(String name : nameSubList)
{
JsonPrimitive element = new JsonPrimitive(name);
array.add(element);
}
String body = array.getAsString();
writeBody(connection, body);
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
for (Object profile : array) {
JSONObject jsonProfile = (JSONObject) profile;
String id = (String) jsonProfile.get("id");
String name = (String) jsonProfile.get("name");
JsonObject jsonProfile = (JsonObject) profile;
String id = jsonProfile.get("id").getAsString();
String name = jsonProfile.get("name").getAsString();
UUID uuid = UUIDFetcher.getUUID(id);
uuidMap.put(name, uuid);
}