[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

    Blah1

    bigteddy98 unless the player is online, it just returns null.
     
  2. Offline

    bigteddy98

    Are you sure you are correctly using this lib? I think you aren't because for this lib it doesn't matter if the player is online or not.
     
  3. Offline

    bigteddy98

    Just to keep you up-to-date. Since CraftBukkit now also almost fully supports the UUID system there is no need for using this lib anymore ;)
     
  4. Offline

    Thinglord129

    Much faster way:

    Code:java
    1. public static String getUniqueIdFromMojang(String name) {
    2. try {
    3. URL url = new URL("[url]https://api.mojang.com/users/profiles/minecraft/[/url]" + name);
    4. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    5.  
    6. Object obj = jsonParser.parse(in);
    7. JSONObject jsonObject = (JSONObject) obj;
    8.  
    9. String uuid = (String) jsonObject.get("id");
    10.  
    11. uuid = uuid.replaceAll("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5");
    12.  
    13.  
    14. return uuid;
    15. } catch (Exception e) {
    16. return "Player not found or Mojang server down!";
    17. }
    18. }
     
  5. Offline

    TrenTech

    Why not just create a hashmap and collect players uuid's on join? Then do something like this

    public static UUID getUUID(String playerName){
    UUID uuid = null;
    HashMap<UUID, String> players = MyPlugin.players;
    Set<Map.Entry<UUID, String>> keys = players.entrySet();
    for(Entry<UUID, String> key : keys){
    if(key.getValue().equalsIgnoreCase(playerName)){
    uuid = key.getKey();
    }
    }
    return uuid;
    }

    Not sure how well this would perform on a large server though
     
Thread Status:
Not open for further replies.

Share This Page