Last active
August 29, 2015 14:15
-
-
Save thekeenant/5881712473a11571b8b2 to your computer and use it in GitHub Desktop.
Search Minecraft profiles by name or UUID. Requires Gson.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.Gson; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonObject; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Profile { | |
private final String username; | |
private final String uuid; | |
private Profile(String username, String uuid) { | |
this.username = username; | |
this.uuid = uuid; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public String getUuid() { | |
return uuid; | |
} | |
@Override | |
public String toString() { | |
return "Profile{username=" + username + ",uuid=" + uuid + "}"; | |
} | |
public static List<Profile> findByName(List<String> names) throws Exception { | |
List<Profile> list = new ArrayList<Profile>(names.size()); | |
int requests = 1; | |
for (int i = 0; i < requests; i++) { | |
HttpURLConnection connection = getNameConnection(); | |
String body = new Gson().toJson(names.subList(i * 100, Math.min((i + 1) * 100, names.size()))); | |
OutputStream stream = connection.getOutputStream(); | |
stream.write(body.getBytes()); | |
stream.flush(); | |
stream.close(); | |
JsonArray array = new Gson().fromJson(new InputStreamReader(connection.getInputStream()), JsonArray.class); | |
for (Object profile : array) { | |
JsonObject jsonProfile = (JsonObject) profile; | |
String name = jsonProfile.get("name").getAsString(); | |
String id = jsonProfile.get("id").getAsString(); | |
list.add(new Profile(name, id)); | |
} | |
} | |
return list; | |
} | |
public static List<Profile> findbyUuid(List<String> uuids) throws Exception { | |
List<Profile> list = new ArrayList<Profile>(); | |
for (String uuid : uuids) { | |
HttpURLConnection connection = getUUIDConnection(uuid); | |
JsonArray array = new Gson().fromJson(new InputStreamReader(connection.getInputStream()), JsonArray.class); | |
if (array.size() > 0) | |
list.add(new Profile(array.get(0).getAsJsonObject().get("name").getAsString(), uuid)); | |
} | |
return list; | |
} | |
private static HttpURLConnection getNameConnection() throws Exception { | |
URL url = new URL("https://api.mojang.com/profiles/minecraft"); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("POST"); | |
connection.setRequestProperty("Content-Type", "application/json"); | |
connection.setUseCaches(false); | |
connection.setDoOutput(true); | |
return connection; | |
} | |
private static HttpURLConnection getUUIDConnection(String uuid) throws Exception { | |
URL url = new URL("https://api.mojang.com/user/profiles/" + uuid + "/names"); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestMethod("GET"); | |
connection.setRequestProperty("Content-Type", "application/json"); | |
connection.setUseCaches(false); | |
connection.setDoOutput(true); | |
return connection; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Arrays; | |
public class ProfileExample { | |
public static void main(String... args) { | |
// Username | |
List<Profile> profiles = Profile.findByName(Arrays.asList("swivr", "Notch")); | |
System.out.println(profiles); | |
// Uuid | |
profiles = Profile.findByUuid(Arrays.asList("df91bff5b7e54b54b7d4463efadac146", "069a79f444e94726a5befca90e38aaf5")); | |
System.out.println(profiles); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment