Tutorial How to convert UUID to Name and Name to UUID! UIsing Mojang API

Discussion in 'Resources' started by Proxygames14, Aug 14, 2017.

Thread Status:
Not open for further replies.
  1. There is an easy way to convert it! This requires internet!

    First we going to use the Mojang API to get the player data!
    The URL's we need are to convert Name to UUID.
    You can do Name to UUID with an ez bukkit way but Im going to show you the API way!

    1. https://api.mojang.com/users/profiles/minecraft/<username>

    And the URL to convert UUID to Name

    2. https://api.mojang.com/user/profiles/<uuid>/names

    The results will be for 1. (Example)
    Code:
    {
    
      "id": "7125ba8b1c864508b92bb5c042ccfe2b",
      "name": "KrisJelbring"
    }
    For 2 the results will be (Example)
    Code:
    [
      {
        "name": "Gold"
      },
      {
        "name": "Diamond",
        "changedToAt": 1414059749000
      }
    ]

    And of course you need the import list for both methods
    Code:
    import java.io.IOException;
    import java.net.URL;
    
    import org.apache.commons.io.IOUtils;
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.JSONValue;
    import org.json.simple.parser.ParseException;



    Name to UUID Turtorial.

    Step 1. So the first thing to do is getting the data we need the url in a String (Example)
    Code:
    String name = "(PlayerName)";
    String url = "https://api.mojang.com/users/profiles/minecraft/"+name;
    Step 2. Reading the url and getting the .txt value
    Code:
    String UUIDJson = IOUtils.toString(new URL(url));
    Extra Step. To avoid invalid query's, Add an if statement
    Code:
    if(UUIDJson.isEmpty()) //STOP THE CODE;    
    Step 3. Converting the data to a JSONObject
    Code:
    JSONObject UUIDObject = (JSONObject) JSONValue.parseWithException(UUIDJson);
    Step 4. Getting the uuid from the JsonData.
    Code:
    String uuid = UUIDObject.get("id").toString();

    So here is the full method!
    Code:
        public String getUuid(String name) {
            String url = "https://api.mojang.com/users/profiles/minecraft/"+name;
            try {
                @SuppressWarnings("deprecation")
                String UUIDJson = IOUtils.toString(new URL(url));           
                if(UUIDJson.isEmpty()) return "invalid name";                       
                JSONObject UUIDObject = (JSONObject) JSONValue.parseWithException(UUIDJson);
                return UUIDObject.get("id").toString();
            } catch (IOException | ParseException e) {
                e.printStackTrace();
            }
           
            return "error";
        }


    UUID to Name Turtorial.

    Step 1. Create the link with the uuid! (Im just making the 2 ways of uuids, With and without hyphens)
    Code:
    String uuid = "(PlayerUUID)";
    String url = "https://api.mojang.com/user/profiles/"+uuid.replace("-", "")+"/names";
    Step 2. Get the data from the api site
    Code:
    String nameJson = IOUtils.toString(new URL(url));
    Step 3. This Jsondata is different so we need to convert it to an JsonArray Instead JsonObject.
    This will return the name history so we will get the last Array of the list (Current Name).
    Code:
    JSONArray nameValue = (JSONArray) JSONValue.parseWithException(nameJson);
    Step 4. We need to get the last Array of the nameValue. By converting the JsonArray to Array[] and getting the value (size() - 1)
    Code:
    String playerSlot = nameValue.get(nameValue.size()-1).toString();
    Step 5. Getting the correct row of the Json String. Now we can use JsonObject because we broke appart the jsonArray.
    Code:
    JSONObject nameObject = (JSONObject) JSONValue.parseWithException(playerSlot);
    Step 6. Get the name slot of the JsonObject.
    Code:
    String name = nameObject.get("name").toString();


    This is the whole method
    Code:
        public String getName(String uuid) {
            String url = "https://api.mojang.com/user/profiles/"+uuid.replace("-", "")+"/names";
            try {
                @SuppressWarnings("deprecation")
                String nameJson = IOUtils.toString(new URL(url));           
                JSONArray nameValue = (JSONArray) JSONValue.parseWithException(nameJson);
                String playerSlot = nameValue.get(nameValue.size()-1).toString();
                JSONObject nameObject = (JSONObject) JSONValue.parseWithException(playerSlot);
                return nameObject.get("name").toString();
            } catch (IOException | ParseException e) {
                e.printStackTrace();
            }
            return "error";
        }

    I hope you you find this turtorial usefull!
    If you got any problems/Questions Just ask them in the comments!
     
    DaniroCraft and Zombie_Striker like this.
  2. Offline

    jimmibond

    This tool may help while working with JSON using JSON Editor
     
  3. I might can help you if you need help!
     
  4. @Proxygames14
    Please, just don't do this! You're making HTTP requests on the main thread! What if somebody has a slow connection (IE mobile broadband or something) and the request takes 5 seconds? Then you'll freeze the entire server, and probably crash it in the process.

    Ontop of that, you have basically no handling if anything goes wrong, you don't check response codes, no nothing, and you're just catching the exceptions and returning "error" as a username. I'd suggest you rework this quite a bit. (Have a look here for at least a somewhat more safe implementation).
     
  5. Thanks, I totaly forgot about that! I knew there was something missing.
     
  6. Offline

    Blares

    this is way more complex than it needs to be. I can do this in like 2 lines...
     
  7. Offline

    Zombie_Striker

    @Blares
    Do you mind explaining how you would access the name of a player that has not been on the server using two lines?
     
    _ningattes369 and timtower like this.
Thread Status:
Not open for further replies.

Share This Page