[Lib] Convert playername to UUID and back (From Mojang Servers)

Discussion in 'Resources' started by bigteddy98, Apr 1, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    bigteddy98

    This resource is no longer availabe.
     
    GregMC, ArthurMaker and SpiritGamerr like this.
  2. Offline

    ArthurMaker

    I don't understand yet what we can do with UUIDs. Can you explain it?
     
  3. Offline

    DevRosemberg

    ArthurMaker Well, player.getName() has been deprecated and UUIDs are the new 'names' as mojang is now using UUIDs to store player bans, whitelist, ops and everything so you can eventually change your IGN.
     
    bigteddy98 and ArthurMaker like this.
  4. Offline

    ArthurMaker


    Wow, looks nice then! :D
     
    bigteddy98 likes this.
  5. Offline

    Bammerbom

    best lib I seen yet to do this :)
     
    bigteddy98 likes this.
  6. Offline

    bigteddy98

    Thanks man :)
     
  7. Offline

    ItsLeoFTW

    Thank you SO much! This will be VERY helpful! Hopefully plugins like PermissionsEx update soon after 1.8 to use this, so that way I can change my name and keep my ranks on servers! Once again, thanks!
     
    bigteddy98 likes this.
  8. Offline

    bigteddy98

    Thanks! :)
     
  9. Offline

    chasechocolate

    Just an FYI, if you are going to call these methods frequently (or even more than once before the server restarts), it'd be a smart idea to cache these values in a HashMap for faster querying. What I did in addition to the HashMap, was store the UUIDs in my player database. From my tests, I can query it faster from a database as opposed to the Mojang servers.
     
    Jhtzb and bigteddy98 like this.
  10. Offline

    Blah1

    bigteddy98 You're officially the most useful person on the forums lol
     
  11. Offline

    ArthurMaker


    Agreed, gentleman.
     
    bigteddy98 likes this.
  12. Offline

    bigteddy98

    Thanks alot, I enjoy it :).
     
    SpiritGamerr likes this.
  13. Offline

    bigteddy98

    Forgot to say that this class also works for players who have never been on your server before (which isn't possible with the Bukkit API yet).
     
  14. Offline

    Blah1

    bigteddy98 Does the getUUIDFromName method work even if the capatalization of the entered name is no exact?

    Like would it work if I typed BiGTEddy98 ?
     
  15. Offline

    bigteddy98

    I will test it for you right now.

    //edit:
    Result:
    When I capitalize some characters of my name I just get my correct UUID, when I convert it back, it gives my name in lowercases (which it normally is).
     
    Blah1 likes this.
  16. Offline

    RawCode

    you just reimplemented mojang profile API posted by mojang 4 mounth ago
     
  17. Offline

    bigteddy98

    What? I don't get you?
     
  18. Offline

    Blah1

    Awesome!
     
  19. Offline

    Asgernohns

    bigteddy98 This is SO good! thanks for making that uuid thing so much more easier! :D
     
  20. Offline

    bigteddy98

    Your welcome ;)

    You should use CraftBukkit as dependency instead of Bukkit (which you are probably using).

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  21. Offline

    Asgernohns

    bigteddy98 I found out :D. Could it be an idea to change the return type of getUUIDFromName() to UUID, instead of string? Just by doing 'UUID id = UUID.fromString(string);'
     
  22. Offline

    bigteddy98

    You could change it, but for saving/storing the UUID you will need the string, not the UUID.
     
  23. Offline

    Asgernohns

    i'm using UUID object as the key. Isn't that possible?
     
  24. Offline

    bigteddy98

    Yes it is, but I mean when saving it somewhere, like in a database.
     
  25. Offline

    Blah1

    It seems to take about 0.5-1 second to get a players UUID (or get a player's name from a player's UUID).
    Basically, player.getUniqueId().toString().replace("-", ""); is faster than UUIDLibrary.getUUID...

    But for offlineplayers, UUIDLibrary.getUUID is the way to go.
    Hopefully Bukkit adds a way to get UUIDs of offline players because the delay is pretty annoying.

    Having said that, you should probably do a quick check in case a player is online for UUIDFromName

    Code:java
    1. public static String getUUIDFromName(String name) {
    2. if (Bukkit.getServer().getPlayer(name) == null) {
    3. try {
    4. ProfileData profC = new ProfileData(name);
    5. String UUID = null;
    6. for (int i = 1; i <= 100; i++) {
    7. PlayerProfile[] result = post(new URL("[url]https://api.mojang.com/profiles/page/[/url]" + i), Proxy.NO_PROXY, gson.toJson(profC).getBytes());
    8. if (result.length == 0) {
    9. break;
    10. }
    11. UUID = result[0].getId();
    12. }
    13. return UUID;
    14. } catch (Exception e) {
    15. e.printStackTrace();
    16. }
    17. } else {
    18. return Bukkit.getServer().getPlayer(name).getUniqueId().toString().replace("-", "");
    19. }
    20. return null;
    21. }
    22.  
     
  26. Offline

    bigteddy98

    This resource is no longer availabe.
     
  27. Offline

    Squid_Boss

    Ah, this is going to help quite a bit, I attempted to convert all my name based code to UUID dependent, but I didn't really understand UUID object from UUID.utils, so thanks :).
     
  28. Offline

    Ultimate_n00b

    It's faster because it's loaded when the player joins.
     
    bigteddy98 likes this.
  29. Offline

    Blah1

    For some random reason, this class stopped working. It might be 1.7.9. I added some stuff to make it easier to get info and it worked fine but it's not working now. It works when I check for players who are online but not for players who have never logged on. It also doesn't work for people who have logged on before but are offline.

    Class:
    Code:java
    1.  
    2. private static Gson gson = new Gson();
    3.  
    4. public static String getNameFromUUID(String uuid) {
    5. String name = null;
    6. try {
    7. URL url = new URL("[url]https://sessionserver.mojang.com/session/minecraft/profile/[/url]" + uuid);
    8. URLConnection connection = url.openConnection();
    9. Scanner jsonScanner = new Scanner(connection.getInputStream(), "UTF-8");
    10. String json = jsonScanner.next();
    11. JSONParser parser = new JSONParser();
    12. Object obj = parser.parse(json);
    13. name = (String) ((JSONObject) obj).get("name");
    14. jsonScanner.close();
    15. } catch (Exception ex) {
    16. ex.printStackTrace();
    17. }
    18. return name;
    19. }
    20.  
    21. public static String getQuickUUID(Player player) {
    22. return player.getUniqueId().toString().replace("-", "");
    23. }
    24.  
    25. public static String getQuickUUID(OfflinePlayer player) {
    26. return player.getUniqueId().toString().replace("-", "");
    27. }
    28.  
    29. public static String getExactNameFromName(String name) {
    30. if (!Bukkit.getServer().getOfflinePlayer(name).hasPlayedBefore()) {
    31. String n = getUUIDFromName(name);
    32. return getNameFromUUID(n);
    33. } else {
    34. return Bukkit.getServer().getOfflinePlayer(name).getName();
    35. }
    36. }
    37.  
    38. public static String getUUIDFromName(String name) {
    39. if (!Bukkit.getServer().getOfflinePlayer(name).hasPlayedBefore()) {
    40. try {
    41. ProfileData profC = new ProfileData(name);
    42. String UUID = null;
    43. for (int i = 1; i <= 100; i++) {
    44. PlayerProfile[] result = post(new URL("[url]https://api.mojang.com/profiles/page/[/url]" + i), Proxy.NO_PROXY, gson.toJson(profC).getBytes());
    45. if (result.length == 0) {
    46. break;
    47. }
    48. UUID = result[0].getId();
    49. }
    50. return UUID;
    51. } catch (Exception e) {
    52. e.printStackTrace();
    53. }
    54. } else {
    55. return Bukkit.getServer().getOfflinePlayer(name).getUniqueId().toString().replace("-", "");
    56. }
    57. return null;
    58. }
    59.  
    60. private static PlayerProfile[] post(URL url, Proxy proxy, byte[] bytes) throws IOException {
    61. HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);
    62. connection.setRequestMethod("POST");
    63. connection.setRequestProperty("Content-Type", "application/json");
    64. connection.setDoInput(true);
    65. connection.setDoOutput(true);
    66.  
    67. DataOutputStream out = new DataOutputStream(connection.getOutputStream());
    68. out.write(bytes);
    69. out.flush();
    70. out.close();
    71.  
    72. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    73. StringBuffer response = new StringBuffer();
    74. String line;
    75. while ((line = reader.readLine()) != null) {
    76. response.append(line);
    77. response.append('\r');
    78. }
    79. reader.close();
    80. return gson.fromJson(response.toString(), SearchResult.class).getProfiles();
    81. }
    82.  
    83. private static class PlayerProfile {
    84. private String id;
    85.  
    86. public String getId() {
    87. return id;
    88. }
    89. }
    90.  
    91. private static class SearchResult {
    92. private PlayerProfile[] profiles;
    93.  
    94. public PlayerProfile[] getProfiles() {
    95. return profiles;
    96. }
    97. }
    98.  
    99. private static class ProfileData {
    100. @SuppressWarnings("unused")
    101. private String name;
    102. @SuppressWarnings("unused")
    103. private String agent = "minecraft";
    104.  
    105. public ProfileData(String name) {
    106. this.name = name;
    107. }
    108. }
     
  30. Offline

    bigteddy98

    What doesn't work anymore? Are you recieving a wrong UUID, or do you have errors in your console?
     
Thread Status:
Not open for further replies.

Share This Page