Util Player name to UUID

Discussion in 'Resources' started by nielsbwashere, Oct 21, 2015.

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

    nielsbwashere

    Hey guys nielsbwashere here,
    Today, I was trying to get the UUID of a player, WITHOUT having information about him (So without him joining the server). (This is useful for getting their skulls, skins and so on)
    I think that what I found is useful to maybe a few of you, so I will show you here.

    To accomplish this, I used Mojang's api (On their website), they have a php script that writes a response with the UUID of a player if you enter this website: https://api.mojang.com/users/profiles/minecraft/<user>
    They respond with a .JSON file that includes the tag: id, with the UUID (No dashes unfortunately) and the name of the player in a different tag.

    Code:
    public static UUID fetchUUID(String s) throws Exception{
            URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + s);
            String uuid = (String)((JSONObject)new JSONParser().parse(new InputStreamReader(url.openStream()))).get("id");
            String realUUID = uuid.substring(0, 8) + "-" + uuid.substring(8, 12) + "-" + uuid.substring(12, 16) + "-" + uuid.substring(16, 20) + "-" + uuid.substring(20, 32);
            return UUID.fromString(realUUID);
    }
    An important thing to note, is that this method CAN'T BE RUN AT RUNTIME! You must create a separate thread and activate it. If you don't, your server will freeze and you'll have a big problem!

    A full explanation:
    I create an object called URL, which is able to fetch the response of a website. A response is a bounce of text which is used for safely transporting information and data.
    I then get this as a JSON file, and I get the 'id' element of it, which is the UUID without dashes.
    We know that the UUID toString always puts dashes like this: 8 chars - 4 chars - 4 chars - 4 chars - 12 chars, so we did that.
    After that, we could just use the method; UUID.fromString to convert the string to a UUID.

    Thanks to TehHypnoz for his reply, on vaguely how to do it! (See: https://bukkit.org/threads/uuid-of-offline-players.351874/)

    Hopefully this is useful to somebody.

    ~Nielsbwashere

    EDIT: Thanks for teej107 for pointing out that I should use JSON
     
    Last edited: Oct 21, 2015
  2. Offline

    teej107

    @nielsbwashere
    1. This belongs in the resource section. EDIT by Timtower: moved
    2. You should parse the JSON text with json-simple. It's already packaged with the Bukkit API.
     
Thread Status:
Not open for further replies.

Share This Page