Skip to content

Instantly share code, notes, and snippets.

@codebucketdev
Created July 5, 2014 13:04
Show Gist options
  • Save codebucketdev/8d3db60569b79398820f to your computer and use it in GitHub Desktop.
Save codebucketdev/8d3db60569b79398820f to your computer and use it in GitHub Desktop.
This snippet is a part from my MojangRepository.
package de.codebucket.utils;
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;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import com.google.gson.Gson;
public class UUIDFetcher
{
private static final double MAX_REQUEST = 100;
private static final String REQUEST_URL = "http://mcapi.sweetcode.de/api/v2/?uuid&request=";
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 Mojang 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 Mojang 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()));
StringBuffer response = new StringBuffer();
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;
}
}
@SuppressWarnings("unused")
private static class ProfileData
{
private String name;
private String agent = "minecraft";
public ProfileData(String name)
{
this.name = name;
}
}
}
@jan-krueger
Copy link

Would be great if you could update the old API endpoint with the new one (http://mcapi.de/user/<uuid|username>. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment