[Request] UUID Username Replace Method

Discussion in 'Plugin Development' started by Reddcola, Apr 12, 2014.

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


    Hello everyone, today id like to request someone help me with some code. What I have is a Tokens plugin already, and I need some help with UUID support with it. I really liked the way amhokies did this, but I would like to request someone provide me a flatfile method, as many other developers probally need this too. Here is what his method was:

    Thanks for any help, I will properly credit whoever can fulfill this request.
  2. Offline


    I could make a resource for this, but it might take a few days since I don't have very much free time.
    Reddcola likes this.
  3. Offline


    That would be amazing considering many developers are probally stunned by this new update. Anyone else, feel free to reply with your own code and fulfill the request, thanks.
  4. Offline


    I also saw this thread and if possible, could you send me the resource as well. I am having trouble adapting to the new UUID system as well. Thanks :)
  5. Offline


    Keep in mind that this would simply allow you to look up the username of a player given their uuid when they're offline. I needed to do this for a protection plugin to display members of a region and allow owners of the regions to remove the player by using their username instead of the uuid. It would also only work for players that have been online the server.
  6. Offline


    Keep also in mind that you will only have the "last known name" with this method.
    So checking an offline players UUID by his (last known) name will never be 100% reliable.
    If someone comes up with a reliable solution for looking up UUIDs of offline players, he's welcome.
  7. Offline


    you always can request mojang database for name updates...
  8. Offline


    For offline players I check if it's null, and I don't store any UUID's, and I set the as offline.
    So offline people' files will be named 'Offline-<playername>'
  9. Offline


    So I'm guessing we as developers can store a player's username and UUID on our own databases or won't Mojang be happy with us doing this?
  10. Offline


    We can do this, but player's name won't ever be 100% up to date as it will only update
    your database when they log in whereas if they go offline and change it then your database won't
    be updated.

    Unless we can somehow retrieve the data of players username and save it to ours from Mojang.
    Reddcola and Jozeth like this.
  11. Offline


    Yes but you don't need to have the current name all the time, for example in this situation you only need it for saving player's token balance and modifying their balance.
    Konkz likes this.
  12. Offline


    what you could do is creating 2 instances a online player instance and a offline player instance.

    these instances are based on the filename which name is entirely UUID, but the purpose is on the online player instance when it will be instanced it will lookup certain things in the constructor such as does the file exist with the UUID credentials and is the name changed, if the name is changed update it.

    now when a player joins: you could add them in a global HashMap<String, yourPlayerInstance>

    now for offline player support:

    you could create a method which loops through each file (including caching) and compair the normal username (aka LastKnownName), if you see it makes a cycle everytime when a player joins the name will be updated and when he leaves you can depend on the old name.

    I actually wanted to write code here but I guess the problem is explaining the logic behind it, however I like to sent some of my github code in the hope for a better explaination.

    the main class (where you can find all the getters and setters likely as getPlayer(), getOfflinePlayer() )

    the event which instanced the online player instance(note you can better use PlayerLoginEvent or AsyncLoginEvent):

    the mojangUUID my own version which I try to make most compat for each version of minecraft:

    the online player instance, make sure to look into the constructor just ignore the event:

    and the offline player instance:

    its kinda messy, and lots of work but I think this could help in someway :), it would only be sad if the OfflinePlayer in bukkit will be handled with UUID's rather than last known names.
  13. Offline



    Here's a method that is almost exactly what you want. First, you'll need evilmidget38's UUIDFetcher, then you can use this (made by myself)

    1. import java.util.Arrays;
    2. import java.util.HashMap;
    3. import java.util.UUID;
    4. import java.util.logging.Level;
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.EventPriority;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11. import org.bukkit.event.player.PlayerQuitEvent;
    13. public class UUIDUtil implements Listener {
    14. static HashMap<String, UUID> uuids = new HashMap<String, UUID>();
    16. public static UUID nameToUUID(String playername) {
    17. UUID uuid;
    18. try {
    19. uuid = new UUIDFetcher(Arrays.asList(playername)).call().get(playername);
    20. } catch (Exception e) {
    21. System.out.println("ERROR: Failed to get UUID of " + playername);
    22. e.printStackTrace();
    23. uuid = null;
    24. }
    25. return uuid;
    26. }
    27. @SuppressWarnings("deprecation")
    28. public static String getUUID(String name) {
    29. if (uuids.containsKey(name)) {
    30. return uuids.get(name).toString();
    31. }
    32. else {
    33. if (Bukkit.getOfflinePlayer(name).isOnline()) {
    34. if (nameToUUID(name) != null) {
    35. Bukkit.getPlayer(name).kickPlayer("§cUnsynchronized data detected for UUID " + nameToUUID(name).toString() + "! Please log back in.");
    36. }
    37. else {
    38. Bukkit.getPlayer(name).kickPlayer("§cUnsynchronized data detected! Mojang UUID Profiles are currently offline.");
    39. }
    40. }
    41. return nameToUUID(name).toString();
    42. }
    43. }
    45. @EventHandler(priority = EventPriority.HIGHEST)
    46. public void onPlayerJoin(PlayerJoinEvent event) {
    47. uuids.put(event.getPlayer().getName(), nameToUUID(event.getPlayer().getName()));
    48. if (nameToUUID(event.getPlayer().getName()) == null) {
    49. event.getPlayer().kickPlayer("§cUUID error! Mojang UUID Profiles are currently offline.");
    50. }
    51. }
    53. @EventHandler(priority = EventPriority.HIGHEST)
    54. public void onPlayerQuit(PlayerQuitEvent event) {
    55. uuids.remove(event.getPlayer().getName());
    56. }
    57. }

    To get a player's name from a UUID, simply use Bukkit.getOfflinePlayer(UUID).getName()
    The reason this class is useful is because you probably wouldn't want to send a web request to search for a UUID every time you need it. Make sure to register this class as a Listener in your main plugin!
  14. Offline


    You can't just "simply" use this method though. It's a blocking method, meaning that if you run it in the main server thread, it will stop everything until it has finished looking up the player, which would take a significant amount of time.
    CubieX likes this.
Thread Status:
Not open for further replies.

Share This Page