Player name -> UUID "Fetcher"

Discussion in 'Resources' started by evilmidget38, Mar 30, 2014.

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

    evilmidget38

    UUIDFetcher has been updated to work with Mojang's changes to their profiles api. The latest version, as always, is available at the bottom of the post.

    AccountsClient works wonderfully for any of Mojang's games that use their accounts system(currently only minecraft), however it's somewhat overkill and over complicated for retrieving UUID's given a List of player names. As such, I wrote my own alternative based off of the implementation found in AccountsClient to retrieve a mapping of provided names to Mojang account ID's. It's all contained within a single class file, so it should be easy to copy+paste into your own plugin.

    Usage:
    Using UUIDFetcher is fairly simple. First, you need to instantiate a UUIDFetcher with a List of names to retrieve the UUID for.
    Code:java
    1.  
    2. UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList("evilmidget38", "mbaxter"));
    3.  

    Once you've created your UUIDFetcher, all that's left is to actually run it.

    If you'd like to run your UUIDFetcher on the thread that you're currently on, simply invoke the method "call", as shown below. Note that if you're currently on the main thread(inside of an EventHandler or scheduled task), you should not ever do this.
    Code:java
    1.  
    2. Map<String, UUID> response = null;
    3. try {
    4. response = fetcher.call();
    5. } catch (Exception e) {
    6. getLogger().warning("Exception while running UUIDFetcher");
    7. e.printStackTrace();
    8. }
    9.  


    To run your UUIDFetcher on another thread you can pass it into an ExecutorService if you've got one, or alternatively, run it on an async thread using Bukkit's scheduler. Your actual implementation of this will vary a lot depending upon the layout of your plugin and how you're using the data, so I'm not going to write an example of running the UUIDFetcher on an async thread.

    Download/Source:

    https://gist.github.com/evilmidget38/26d70114b834f71fb3b4

    This is designed to be used in plugins, so please, go ahead and use it!

    EDIT: I also created NameFetcher, a similar class, for retrieving the username of a player given their UUID. Usage and expected results are identical to those of UUIDFetcher. The implementation of NameFetcher is based off of "MojangNameLookup" from Craftbukkit.
     
    Mitsugaru, aman207, Wruczek and 19 others like this.
  2. Offline

    drtshock

    Awesome! Much shorter than using AccountsClient.
     
    Phasesaber likes this.
  3. Offline

    RawCode

  4. Offline

    SoThatsIt

    evilmidget38 just after i made my own api to do this, could have saved much work, looks good though
     
  5. Offline

    desht

    Hmm. I just added UUIDFetcher to my plugin, and ran it on my username, and I got: desht => 4143306a-84e5-44aa-8d15-74ba2fe88c58. All well and good, except I also added a player.getUniqueId() call in one of my PlayerInteractEventHandler methods, and I get desht => 808df108-8a23-35c4-97f4-2a1088d130fe

    Now I thought player.getUniqueId() was supposed to return my Mojang UUID, so what's going on here?

    Ignore me, my fault for having my dev server in offline mode :) It matches up properly when run online.
     
    Pew446, TigerHix and KingFaris11 like this.
  6. Offline

    iiHeroo

    great.
     
  7. Offline

    evilmidget38

  8. Offline

    DevRosemberg

    Isnt doing player.getUniqueId() the same thing but not parsed?
     
  9. Offline

    evilmidget38

    Player#getUniqueId() will give you the same result as a lookup. The purpose of these classes is for data file conversion. If you're storing user data and want to migrate to a new system utilizing UUID's, the player's aren't going to be online.

    I'm not sure what you mean by "not parsed" though. The UUID object obtained by this method should be equivalent to what you obtain from Player#getUniqueId().
     
    lukegb likes this.
  10. Offline

    DevRosemberg

    evilmidget38 Ah, k. So the problem is getting the offline UUID, thanks, gona be implementing this in my core.

    Though the thing of it returning a UUIDFetcher is quite bothering me as it dosent have the same us it would have returning an UUID or a String.

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

    evilmidget38

    DevRosemberg You shouldn't be returning a UUIDFetcher. Instead, you should be returning the Map<String, UUID> (or Map<UUID, String> for NameFetcher) obtained by invoking call(). Note that call is a blocking method, so you should probably run it asynchronously.
     
  12. Offline

    Garris0n

    You probably shouldn't use it like that, just saying.
     
  13. Offline

    DarkBladee12

    I just saw that they actually added an API for dealing with UUIDs recently.
     
  14. Offline

    Minnymin3

    Okay excuse my lack of knowledge but what the heck are UUIDs used for?
     
  15. Offline

    Goblom

    Minnymin3 Mojang/Minecraft is converting to uuids to support name changing. If a user changes their name their uuid stays the same.

    Its basically a new way to keep track a accounts instead of just using a username
     
  16. Offline

    Minnymin3

    Goblom
    Ah okay thanks. Can you change your username now or is it something that Mojang is planning?
     
  17. Offline

    Goblom

    Minnymin3 They have been planning it for a while now.
     
  18. Offline

    Crast

    I added a support class for the purpose of supporting pre-1.7.5 servers and current ones simultaneously within the same plugin code, that is meant for more of 'real time' active use (whereas the UUIDFetcher is more for conversions, as evilmidget38 stated.) It adds a name->UUID cache which clears entries when users quit, and uses UUIDFetcher in the background to get the UUID.

    You can get it here: https://github.com/crast/CrastBukki.../main/java/us/crast/bukkituuid/UUIDCache.java
     
  19. Offline

    Jozeth

    Any licence for this?
     
  20. Offline

    Crast

    public domain for my cache class, I don't think it's worth me making a hullaballoo over a single little class, though it'd be best to check with evilmidget38 if he's got a license on his fetcher class.
     
  21. Offline

    Amaranth

    If this only keeps the data for as long as the user is on the server you can just use player.getUniqueId() instead. Why would you need to look up something the server already has?
     
  22. Offline

    Crast

    For servers not running 1.7.5+, player.getUniqueId() does not return their Mojang UUID. This service will consistently return their mojang UUID regardless of server version, for plugins wanting to work across the transition. Also, it can fetch the ID for offline players as well, it just uses the join/quit events to prefetch/clear them.
     
    Jhtzb likes this.
  23. Offline

    ZachBora

    evilmidget38 If you were a girl I would give you a hug and kiss you. But since you aren't I'll just give you a Like.
     
    Venexor likes this.
  24. Offline

    blablubbabc

  25. Offline

    jast

    In preparation of migrating Gringotts, I just went ahead and made a proper Maven project from the two classes linked from here: https://github.com/MinecraftWars/bukkit-migration-tools

    It will also be available as Maven dependency once I get an Artifactory server running (next week I suppose)
     
  26. I'm a bit lost. Why can't we just use:
    Code:
    Bukkit.getOfflinePlayer(uuid).getName();
    ^ To get the username.
    Code:
    Bukkit.getOfflinePlayer(username).getUniqueId();
    ^ To get the UUID of a player.
     
  27. Offline

    viper_monster

    KingFaris11 that will only return an UUID if the player has played before on your server
     
  28. That's what I was thinking, but according to Bukkit's JavaDoc, getOfflinePlayer(UUID):
    getOfflinePlayer(String):
    I'm not sure exactly what this means, but maybe it would work? Can someone please test this?
    Anyway, that's my main use of getting UUID and storing, like many other plugins, I'm sure it's simpler.

    Tested. getOfflinePlayer() works even if they haven't joined the server... although the JavaDoc said it's a temporary hack. Hopefully they don't remove it. :S

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

    Amaranth

    getOfflinePlayer(name) will always give you an OfflinePlayer with a name but the UUID may be invalid if the name isn't a valid account. UUID#version() will return 3 in that case. This involves doing a web request to Mojang on the main thread if the lookup isn't in the cache. getOfflinePlayer(UUID) will always give you an OfflinePlayer with a UUID but the name will be null if the player with that UUID has not played on your server before or if you are importing player data files from vanilla.
     
    rbrick, KingFaris11 and viper_monster like this.
  30. Offline

    guyag

    Does this tweet change anything?
     
Thread Status:
Not open for further replies.

Share This Page