Created
April 21, 2014 13:41
-
-
Save FarisR99/11143062 to your computer and use it in GitHub Desktop.
UUIDApi - Mineskin
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.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.UUID; | |
import org.json.simple.JSONObject; | |
import org.json.simple.parser.JSONParser; | |
public class UUIDApi { | |
private static final String API_LINK_PLAYER = "http://mineskin.ca/name/?uuid="; | |
private static final String API_LINK_UUID = "http://mineskin.ca/uuid/?player="; | |
private static final JSONParser jsonParser = new JSONParser(); | |
public static UUID getUUID(String player) { | |
String strUUID = getUUIDToString(player); | |
return strUUID == null ? null : UUID.fromString(strUUID); | |
} | |
public static String getUUIDToString(String player) { | |
try { | |
URL url = new URL(API_LINK_PLAYER + player); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
JSONObject object = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream())); | |
if (object.containsKey("uuid")) { | |
String uuid = (String) object.get("uuid"); | |
if (uuid.length() != 32) { | |
return null; | |
} | |
return uuid.substring(0, 8) + "-" + uuid.substring(8, 12) + "-" + uuid.substring(12, 16) + "-" + uuid.substring(16, 20) + "-" + uuid.substring(20, 32); | |
} | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
return null; | |
} | |
public static String getName(String uuid) { | |
try { | |
URL url = new URL(API_LINK_UUID + uuid.replaceAll("-", "")); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
JSONObject object = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream())); | |
if (object.containsKey("name")) { | |
String player = (String) object.get("name"); | |
if (!player.isEmpty()) return player; | |
} | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
return null; | |
} | |
public static String getName(UUID uuid) { | |
return getName(uuid.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment