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; package com.gmail.nossr50.util.uuid;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import org.json.simple.JSONArray; import com.google.gson.JsonArray;
import org.json.simple.JSONObject; import com.google.gson.JsonObject;
import org.json.simple.parser.JSONParser; import com.google.gson.JsonPrimitive;
import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; 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 int PROFILES_PER_REQUEST = 50;
private static final long RATE_LIMIT = 100L; private static final long RATE_LIMIT = 100L;
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft"; 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 List<String> names;
private final boolean rateLimiting; 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); int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
for (int i = 0; i < requests; i++) { for (int i = 0; i < requests; i++) {
HttpURLConnection connection = createConnection(); 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); writeBody(connection, body);
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
for (Object profile : array) { for (Object profile : array) {
JSONObject jsonProfile = (JSONObject) profile; JsonObject jsonProfile = (JsonObject) profile;
String id = (String) jsonProfile.get("id"); String id = jsonProfile.get("id").getAsString();
String name = (String) jsonProfile.get("name"); String name = jsonProfile.get("name").getAsString();
UUID uuid = UUIDFetcher.getUUID(id); UUID uuid = UUIDFetcher.getUUID(id);
uuidMap.put(name, uuid); uuidMap.put(name, uuid);
} }