mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2024-11-29 16:46:46 +01:00
Update to UUIDFetcher v3
This commit is contained in:
parent
69ebde052d
commit
ed95cfcbfc
@ -1,10 +1,11 @@
|
|||||||
package com.gmail.nossr50.util.uuid;
|
package com.gmail.nossr50.util.uuid;
|
||||||
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -14,54 +15,55 @@ import java.util.concurrent.Callable;
|
|||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import org.json.simple.JSONArray;
|
import org.json.simple.JSONArray;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
import org.json.simple.JSONValue;
|
|
||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
|
|
||||||
public class UUIDFetcher implements Callable<Map<String, UUID>> {
|
public class UUIDFetcher implements Callable<Map<String, UUID>> {
|
||||||
private static final int MAX_SEARCH = 100;
|
private static final double PROFILES_PER_REQUEST = 100;
|
||||||
private static final String PROFILE_URL = "https://api.mojang.com/profiles/page/";
|
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
|
||||||
private static final String AGENT = "minecraft";
|
|
||||||
private final JSONParser jsonParser = new JSONParser();
|
private final JSONParser jsonParser = new JSONParser();
|
||||||
private final List<String> names;
|
private final List<String> names;
|
||||||
|
private final boolean rateLimiting;
|
||||||
|
|
||||||
|
public UUIDFetcher(List<String> names, boolean rateLimiting) {
|
||||||
|
this.names = ImmutableList.copyOf(names);
|
||||||
|
this.rateLimiting = rateLimiting;
|
||||||
|
}
|
||||||
|
|
||||||
public UUIDFetcher(List<String> names) {
|
public UUIDFetcher(List<String> names) {
|
||||||
this.names = ImmutableList.copyOf(names);
|
this(names, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, UUID> call() throws Exception {
|
public Map<String, UUID> call() throws Exception {
|
||||||
Map<String, UUID> uuidMap = new HashMap<String, UUID>();
|
Map<String, UUID> uuidMap = new HashMap<String, UUID>();
|
||||||
String body = buildBody(names);
|
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
|
||||||
for (int i = 1; i < MAX_SEARCH; i++) {
|
for (int i = 0; i < requests; i++) {
|
||||||
HttpURLConnection connection = createConnection(i);
|
HttpURLConnection connection = createConnection();
|
||||||
|
String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
|
||||||
writeBody(connection, body);
|
writeBody(connection, body);
|
||||||
JSONObject jsonObject = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
|
JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
|
||||||
JSONArray array = (JSONArray) jsonObject.get("profiles");
|
|
||||||
Number count = (Number) jsonObject.get("size");
|
|
||||||
|
|
||||||
if (count.intValue() == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Object profile : array) {
|
for (Object profile : array) {
|
||||||
JSONObject jsonProfile = (JSONObject) profile;
|
JSONObject jsonProfile = (JSONObject) profile;
|
||||||
String id = (String) jsonProfile.get("id");
|
String id = (String) jsonProfile.get("id");
|
||||||
String name = (String) jsonProfile.get("name");
|
String name = (String) jsonProfile.get("name");
|
||||||
UUID uuid = UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32));
|
UUID uuid = UUIDFetcher.getUUID(id);
|
||||||
uuidMap.put(name, uuid);
|
uuidMap.put(name, uuid);
|
||||||
}
|
}
|
||||||
|
if (rateLimiting && i != requests - 1) {
|
||||||
|
Thread.sleep(100L);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return uuidMap;
|
return uuidMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void writeBody(HttpURLConnection connection, String body) throws Exception {
|
private static void writeBody(HttpURLConnection connection, String body) throws Exception {
|
||||||
DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
|
OutputStream stream = connection.getOutputStream();
|
||||||
writer.write(body.getBytes());
|
stream.write(body.getBytes());
|
||||||
writer.flush();
|
stream.flush();
|
||||||
writer.close();
|
stream.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HttpURLConnection createConnection(int page) throws Exception {
|
private static HttpURLConnection createConnection() throws Exception {
|
||||||
URL url = new URL(PROFILE_URL + page);
|
URL url = new URL(PROFILE_URL);
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
connection.setRequestMethod("POST");
|
connection.setRequestMethod("POST");
|
||||||
connection.setRequestProperty("Content-Type", "application/json");
|
connection.setRequestProperty("Content-Type", "application/json");
|
||||||
@ -71,17 +73,28 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
|
|||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
private static UUID getUUID(String id) {
|
||||||
private static String buildBody(List<String> names) {
|
return UUID.fromString(id.substring(0, 8) + "-" + id.substring(8, 12) + "-" + id.substring(12, 16) + "-" + id.substring(16, 20) + "-" + id.substring(20, 32));
|
||||||
List<JSONObject> lookups = new ArrayList<JSONObject>();
|
}
|
||||||
|
|
||||||
for (String name : names) {
|
public static byte[] toBytes(UUID uuid) {
|
||||||
JSONObject obj = new JSONObject();
|
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
|
||||||
obj.put("name", name);
|
byteBuffer.putLong(uuid.getMostSignificantBits());
|
||||||
obj.put("agent", AGENT);
|
byteBuffer.putLong(uuid.getLeastSignificantBits());
|
||||||
lookups.add(obj);
|
return byteBuffer.array();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UUID fromBytes(byte[] array) {
|
||||||
|
if (array.length != 16) {
|
||||||
|
throw new IllegalArgumentException("Illegal byte array length: " + array.length);
|
||||||
}
|
}
|
||||||
|
ByteBuffer byteBuffer = ByteBuffer.wrap(array);
|
||||||
|
long mostSignificant = byteBuffer.getLong();
|
||||||
|
long leastSignificant = byteBuffer.getLong();
|
||||||
|
return new UUID(mostSignificant, leastSignificant);
|
||||||
|
}
|
||||||
|
|
||||||
return JSONValue.toJSONString(lookups);
|
public static UUID getUUIDOf(String name) throws Exception {
|
||||||
|
return new UUIDFetcher(Arrays.asList(name)).call().get(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user