Case insensitive UUID retrieval [Better than Bukkit retrieval] fast!

Discussion in 'Resources' started by Heirteir, Apr 26, 2014.

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

    Heirteir

    Alright so this is the smallest and simplest library for getting a uuid from a playername and getting a name from a uuid there is so far!

    I use the mojang API for this so everything is contained in the zip of what you need!
    Installation:
    1. download the zip and drag out what you need
    2. go to your workspace folder
    3. open your project folder
    4. go to your src folder
    5. go into the first package folder
    6. drag in the mojang folder
    7. copy the library class from this webpage or just drag in the file from the zip
    8. there you go!

    LIBRARY:
    Code:java
    1. package com.finishfinal.util.player;
    2.  
    3. import java.util.UUID;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.entity.Player;
    7.  
    8. import com.mojang.api.profiles.HttpProfileRepository;
    9. import com.mojang.api.profiles.Profile;
    10.  
    11. public class PUUID {
    12.  
    13. /* Created by Heirteir
    14. * for Bukkit and Spigot and any other Bukkit fork users
    15. * Please give Heirteir credit or at least don't say you made it.
    16. * thanks for using my library and I hope it helps.
    17. */
    18. private static final String AGENT = "minecraft";
    19. private static final HttpProfileRepository repository = new HttpProfileRepository(AGENT);
    20.  
    21. /**
    22. * Get the UUID from a playername
    23. *
    24. * @param name Name of player to get the UUID of
    25. * @return String (UUID)
    26. */
    27.  
    28. public static String getUUID(String name) {
    29.  
    30. Profile[] profiles = repository.findProfilesByNames(name.toString());
    31.  
    32. for (Player p : Bukkit.getOnlinePlayers()) {
    33. if (p.getName().equalsIgnoreCase(name)) {
    34. return p.getUniqueId().toString();
    35. }
    36. }
    37.  
    38. if (profiles.length == 1) {
    39.  
    40. StringBuilder str = new StringBuilder(profiles[0].getId());
    41. str.insert(8, "-");
    42. str.insert(13, "-");
    43. str.insert(18, "-");
    44. str.insert(23, "-");
    45.  
    46. return str.toString();
    47. } else
    48. return "UUID Not Found";
    49. }
    50. public static String getNameFromUUID (String uuid) {
    51. return Bukkit.getOfflinePlayer(UUID.fromString(uuid)).getName();
    52. }
    53. }
    54.  


    If this helped you please say thanks below if I need to fix something please let me know below!

    To cast the string returned from the getUUID() method just do UUID.fromString(getUUID(playername)); and that will convert the string to a UUID.

    LIBRARY DOWNLOAD

    --Heirteir
     
  2. Offline

    bigteddy98

    Why should we use this instead of the Bukkit build-in system?
     
  3. Offline

    Heirteir

    bigteddy98
    Bukkit's built in system doesn't support case insensitive Strings.
    Example: If you trying to take a player's input lets say they put in 'heirteir' it would return null because they have to put the name completely correct including case in order for it to work so they would have to put 'Heirteir' for it to not return null. My library supports case insensitive so players can type the name in whatever case they want while still keeping the productivity of the bukkit UUID fetcher.
     
  4. Offline

    Aqua

    Heirteir
    Bukkit.getPlayer(name).getName() returns the name as shown in chat.
    So if I did Bukkit.getPlayer("heirteir").getName() it would give me 'Heirteir'.
     
  5. Offline

    Heirteir

    Aqua
    You can use a UUID for player retrieval now
    Example:
    Bukkit.getOfflinePlayer("f8f43c61-9ecd-4a24-9c17-fdb17ed0c13f").getName() would return 'Heirteir'.

    OR

    Bukkit.getPlayer("f8f43c61-9ecd-4a24-9c17-fdb17ed0c13f").getName() would return 'Heirteir' if I were on the server.
     
  6. Offline

    Aqua

    Heirteir
    I know, but I mean your code supports names in which lettercases are incorrect compared to the correct name.
    Doing what I said above will fix the cases and let you use the name to retrieve UUID in which the name has to be correct regarding upper and lower case.
     
  7. Offline

    Heirteir

    Aqua
    What if the player changes there name while there offline? People want a reliable way to get names offline players.
     
  8. Offline

    Aqua

    Heirteir
    For offline still use yours, since what I said only works with online people.
     
  9. Offline

    Heirteir

    Aqua
    Ah sorry for misunderstanding you, yes your right lol.
     
Thread Status:
Not open for further replies.

Share This Page