Small UUID library

Discussion in 'Resources' started by CraftThatBlock, Mar 9, 2014.

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

    CraftThatBlock

    Hey guys, CraftThatBlock here.

    I made a very small Minecraft username to UUID library (in PHP) with examples in Java and PHP (pull requests welcomed).

    The actual library is hosted on https://uuid.swordpvp.com/.
    To get UUID: simple use this format: https://uuid.swordpvp.com/uuid/CraftThatB (CraftThatB being the username getting the UUID from)

    This gets directly from Mojang's server, but it is easier to use as Mojang's uses a post request and a bunch of other complicated things, and do not provide examples.

    To use it in Java, simple use this:
    Code:java
    1. public static String getUUID(String player) {
    2. String uuid = null;
    3. try {
    4. // Get the UUID from SwordPVP
    5. URL url = new URL("[url]https://uuid.swordpvp.com/uuid/[/url]" + player);
    6. URLConnection uc = url.openConnection();
    7. uc.setUseCaches(false);
    8. uc.setDefaultUseCaches(false);
    9. uc.addRequestProperty("User-Agent", "Mozilla/5.0");
    10. uc.addRequestProperty("Cache-Control", "no-cache, no-store, must-revalidate");
    11. uc.addRequestProperty("Pragma", "no-cache");
    12.  
    13. // Parse it
    14. String json = new Scanner(uc.getInputStream(), "UTF-8").useDelimiter("\\A").next();
    15. JSONParser parser = new JSONParser();
    16. Object obj = parser.parse(json);
    17. uuid = (String) ((JSONObject) ((JSONArray) ((JSONObject) obj).get("profiles")).get(0)).get("id");
    18. } catch (Exception ex) {
    19. ex.printStackTrace();
    20. }
    21. return uuid;
    22. }


    Then you can use getUUID(username) to get a string version of the UUID.
    PHP and other languages are on the Github.

    Github

    Thanks!
     
  2. Offline

    ECB2

    Why are you using such a big framework for a simple task like that?

    Also it's easier and safer to grab it yourself, since you won't have to rely on someones service.

    Example:
    Code:java
    1. RequestFactory requestFactory = new RequestFactory("[url]https://api.mojang.com/profiles/page/1[/url]");
    2. requestFactory.setPost(true);
    3. requestFactory.setJsonPost("[{\"name\":\"ECB2\", \"agent\":\"Minecraft\"}]");
    4. JsonObject json = JsonObject.readFrom(requestFactory.run());
    5. JsonObject profiles = json.get("profiles").asArray().get(0).asObject();
    6. System.out.println("UUID: " + profiles.get("id").asString());
     
    Milkywayz likes this.
  3. Offline

    DarkBladee12

    CraftThatBlock ECB2 Why so complicated when you can just use PLAYER.getUniqueId()? I've tested this and the method of this thread and they both return me the same as seen in this picture (upper is with the method of the thread and lower with the Bukkit method):

    [​IMG]

    The only downside of this is that OfflinePlayer doesn't have such a method, but I assume Bukkit will also add this method to this class when Mojang's system changes fully to UUID!
     
  4. Offline

    ECB2

    DarkBladee12 iirc bukkits method generates the entity UUID, not the players UUID.
     
  5. Offline

    turt2live

    False. Bukkit retrieves the UUID from the NMS code, which uses the entity UUID (correct), but for players NMS will use the player's UUID (provided you are on an online mode server). Offline mode servers are unsupported by this functionality.

    So Bukkit just ports the call to NMS for getting the UUID.
     
  6. Offline

    DarkBladee12

    turt2live is there currently a way to retrieve the name from an UUID when the player is offline or do you have to make yourself a database where you store the name that is bound to the UUID?
     
  7. Offline

    turt2live

    Within Bukkit, no.
     
  8. Offline

    DarkBladee12

    turt2live will such a method be added to Bukkit once Mojang's system has fully changed to UUID and allows name changing?
     
  9. Offline

    turt2live

    No idea.
     
  10. Offline

    CraftThatBlock

    My library simply gets it from Mojang, but in a simpler form factor. Making a post to api.mojang and stuff sometimes can be a bit hard, but uuid.swordpvp.com/uuid/Player is much simpler.
    This was meant to make it so even offline servers (using BungeeCord) works, or basically ANY Java application, or PHP, as in the examples. This was not meant to replace player.getUUID() nor Mojang's API, as it uses Mojang's API. This was simply made to make it easier.
     
  11. Offline

    DarkBladee12

    CraftThatBlock well but it would be more useful to create something that gets the current name from an UUID.
     
  12. Offline

    CraftThatBlock

    That would be epic... but Mojang doesn't provide that yet (afaik).
     
  13. Offline

    DarkBladee12

    CraftThatBlock I hope either Mojang or Bukkit provides a way to to that in the future!
     
  14. Offline

    CraftThatBlock

    I hope they do. :)
     
  15. Offline

    CraftThatBlock

    Last edited by a moderator: Jun 7, 2016
  16. Offline

    ftbastler


    Maybe you want to try this. (Look at the last paragraph of the post.)
     
    DarkBladee12 likes this.
  17. Offline

    DevRosemberg

    CraftThatBlock Doing this:

    PHP:
    require('./includes/config.php');
     
    $sUUID getUUID("DevRo_");
     
    echo 
    $sUUID;
    Outputs nothing for some reason.
     
Thread Status:
Not open for further replies.

Share This Page