UUIDConverter

Discussion in 'Bukkit Tools' started by SmileyCraft, Sep 23, 2016.

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

    SmileyCraft

    I could not find any UUID converter that lived up to all my desires, so I made my own.

    How do I get it working?

    First, create classes 'UUIDConverter', 'BothWaysMap' and 'GsonIO'. Here is the code for the classes:

    UUIDConverter:
    http://pastebin.com/s3HRVKHv
    BothWaysMap:
    http://pastebin.com/bM6iQ96r
    GsonIO:
    http://pastebin.com/HEpdCki9

    I put some //TODO notes in the UUIDConverter which will explain what else you need to do to get this working.

    The other two classes are useful in many more situations by the way

    How do I use this?

    Call these methods:
    UUIDConverter.getName(UUID uuid)
    UUIDConverter.getUUID(String name)
    whenever you need to get the name corresponding to a UUID or vice versa.

    IMPORTANT:
    Before you call either of these methods, you must use the method:
    convertUuids(UUID[] uuids, String[] names, Runnable task)
    Place the UUIDs you need to look up in the first argument and the names in the second argument. In the third argument, you need to place a Runnable class, and inside the run() method of this runnable class you can look up any of the uuids or names from the first two arguments.

    The execution of this Runnable may be delayed up to 0.2 seconds (although it rarely takes longer than 0.05 seconds,) so I recommend you to prevent nesting this method at all costs.

    An example:

    public static void messageUUID(Player p, String name){
    UUIDConverter.convertUuids(new UUID[]{}, new String[]{name}, new Runnable(){
    @Override
    public void run(){
    UUID uuid = UUIDConverter.getUUID(name);
    if (uuid == null) p.sendMessage("There is no player with that name.");
    else p.sendMessage("UUID: " + uuid.toString());
    }
    });
    }

    This piece of code works for any input for 'String name'. With this, you could make an in-game command '/getUUID [Name]' which calls this piece of code. It will then message the player the UUID of the player with the given name, or "There is no player with that name." if there is no player with the given name.

    What makes this the best UUID converter?

    • It will look up players online if they never entered the server before.
    • Whenever it looks up a player online, this is done asynchronically, so there is no lag.
    • It can handle names that do not have a corresponding UUID.
    • It stores all the previous results, such that it doesn't need to look up things twice.
    • To keep the database up-to-date, it will refresh player data when they join or quit.
    • When you look up a player that was last saved over half an hour ago, it will return what's stored in the database. But it will also look it up online again in case it has changed, such that the next time the player is looked up, it will certainly be up-to-date.
    • Whenever it has to look up multiple players online, they will all be looked up at the same time.
    • The method that handles the results will be called synchronically with the rest of your plugin, so there are no issues based on methods being called at the same time.
     
    Last edited: Sep 23, 2016
  2. Offline

    I Al Istannen

  3. Offline

    bwfcwalshy Retired Staff

    @SmileyCraft If you have the player why not use Player#getUniqueId? And you can easily get a player from UUID as well by using Bukkit#getPlayer(UUID id)
     
  4. Offline

    SmileyCraft

    I'm not sure if I understand your question, but if you want to know how many players can be looked up at the same time, I don't know if there even is a limit. I tried making it look up 12 names at the same time and it was done perfectly in ~0.2 seconds. Increasing the amount should not increase the time it takes.
    You don't use the UUIDConverter if you already have the player. You use this whenever you cannot be sure whether the person who you want to look up is online. Like when you store highscores, you can store them by UUID and when the person leaves the server, you can still look up their name. This system will even stay up-to-date to any changes the player makes to their name, even when they never return to the server.
    In case you are talking about the 'An example:' in the OP, this piece of code will work for any 'String name'. So you can make an in-game command '/getUUID [Name]' and this piece of code will message the player the UUID of the player with the given name, or it will give the message "There is no player with that name." if there is no player with the given name.
     
  5. Offline

    I Al Istannen

    @SmileyCraft
    Source. I did a name histroy lookup before, so I know that to well :p

    @bwfcwalshy @SmileyCraft
    You do know that you can use Bukkit.getOfflinePlayer(String name) and then OfflinePlayer#getUniqueId()?
    This method will look the name up using the Mojang API, just as you do. Source.

    UUID -> Name might need this, but I am not sure.
     
  6. Offline

    SmileyCraft

    @I Al Istannen
    Are you saying my method can only look up one player every second? But then how come it looked up 12 in ~0.2 seconds?

    And I thought Bukkit.getOfflinePlayer() didn't work for players that never entered the server before. Did this change?
     
Thread Status:
Not open for further replies.

Share This Page