forked from LogBlock/LogBlock
Use GSON in UUIDFetcher
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
package de.diddiz.util;
|
package de.diddiz.util;
|
||||||
|
|
||||||
import org.json.simple.JSONArray;
|
import com.google.gson.Gson;
|
||||||
import org.json.simple.JSONObject;
|
import com.google.gson.GsonBuilder;
|
||||||
import org.json.simple.parser.JSONParser;
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
@@ -18,18 +19,18 @@ import java.util.UUID;
|
|||||||
public class UUIDFetcher {
|
public class UUIDFetcher {
|
||||||
|
|
||||||
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 static final JSONParser jsonParser = new JSONParser();
|
private static final Gson gson = new GsonBuilder().setLenient().create();
|
||||||
|
|
||||||
public static Map<String, UUID> getUUIDs(List<String> names) throws Exception {
|
public static Map<String, UUID> getUUIDs(List<String> names) throws Exception {
|
||||||
Map<String, UUID> uuidMap = new HashMap<>();
|
Map<String, UUID> uuidMap = new HashMap<>();
|
||||||
HttpURLConnection connection = createConnection();
|
HttpURLConnection connection = createConnection();
|
||||||
String body = JSONArray.toJSONString(names);
|
String body = gson.toJson(names);
|
||||||
writeBody(connection, body);
|
writeBody(connection, body);
|
||||||
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
|
JsonArray array = gson.fromJson(new InputStreamReader(connection.getInputStream()), JsonArray.class);
|
||||||
for (Object profile : array) {
|
for (JsonElement 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 = getUUID(id);
|
UUID uuid = getUUID(id);
|
||||||
uuidMap.put(name, uuid);
|
uuidMap.put(name, uuid);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user