Commons/src/main/java/eu/univento/commons/player/uuid/UUIDFetcher.java

114 lines
3.8 KiB
Java

package eu.univento.commons.player.uuid;
import com.google.gson.Gson;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.UUID;
/**
* @author codebucketdev
*/
public class UUIDFetcher {
private static final double MAX_REQUEST = 100;
private static final String REQUEST_URL = "https://mcapi.de/api/user/";
private static final String PROFILE_URL = "https://api.mojang.com/profiles/page/";
public static UUID getRequest(String username) {
try {
JSONParser jsonParser = new JSONParser();
HttpURLConnection connection = (HttpURLConnection) new URL(REQUEST_URL + username).openConnection();
JSONObject response = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
String name = (String) response.get("username");
if (name.length() == 0) {
throw new IllegalArgumentException("A UUID for Username '" + username + "' was not found in the database! Is the account not premium?");
}
return fromString((String) response.get("uuid"));
} catch (Exception e) {
return callMojang(username);
}
}
private static UUID callMojang(String username) {
try {
Gson gson = new Gson();
ProfileData data = new ProfileData(username);
String uuid = null;
for (int i = 1; i < MAX_REQUEST; i++) {
PlayerProfile[] result = post(new URL(PROFILE_URL + i), Proxy.NO_PROXY, gson.toJson(data).getBytes());
if (result.length == 0) {
break;
}
uuid = result[0].getId();
}
return fromString(uuid);
} catch (Exception e) {
throw new IllegalArgumentException("A UUID for Username '" + username + "' was not found in the database! Is the account not premium?");
}
}
private static UUID fromString(String uuid) {
return UUID.fromString(uuid.substring(0, 8) + "-" + uuid.substring(8, 12) + "-"
+ uuid.substring(12, 16) + "-" + uuid.substring(16, 20) + "-" + uuid.substring(20, 32));
}
private static PlayerProfile[] post(URL url, Proxy proxy, byte[] bytes) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(bytes);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
response.append('\r');
}
reader.close();
return new Gson().fromJson(response.toString(), SearchResult.class).getProfiles();
}
private static class PlayerProfile {
private String id;
public String getId() {
return id;
}
}
private static class SearchResult {
private PlayerProfile[] profiles;
public PlayerProfile[] getProfiles() {
return profiles;
}
}
private static class ProfileData {
private String name;
private String agent = "minecraft";
public ProfileData(String name) {
this.name = name;
}
}
}