Getting UUID from Player name

Discussion in 'Plugin Development' started by Chaoscrasher, Jun 2, 2014.

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

    Chaoscrasher

    Hello guys!

    I am currently working on a plugin that translates a player name into his UUID and deletes the corresponding file in the world/players folder.

    Here is how I implement that:
    Code:java
    1.  
    2. public static boolean deletePlayerData(String pname)
    3. {
    4. File F = new File(getPlayerDataPath());
    5. Player onp = Bukkit.getPlayer(pname);
    6.  
    7. boolean b = false;
    8. if (onp != null)
    9. {
    10. b = deletePlayerDataByUUID(onp.getUniqueId().toString());
    11. }
    12. return b;
    13. }
    14.  
    15. public static boolean deletePlayerDataByUUID(String UUID)
    16. {
    17. File F = new File(getPlayerDataPath()+UUID+ex);
    18. if (F.exists())
    19. {
    20. F.delete();
    21. return true;
    22. }
    23. else
    24. {
    25. return false;
    26. }
    27. }
    28.  


    So far the whole thing only works if I call the second method with the long hexadecimal number.
    But it doesn't manage to convert the player name into the UUID. Can someone tell me where I made a mistake in my first method?
     
  2. Offline

    MCForger

    Chaoscrasher
    Does it throw an error? It might be the fact you are trying to get a player object when you do not know if they are online or not. Try changing the Bukkit.getPlayer function into Bukkit.getOfflinePlayer and making the Player object to OfflinePlayer.
     
  3. Offline

    NonameSL

    Code:java
    1. Bukkit.getOfflinePlayer(name).getUniqueId().toString();
     
  4. Offline

    Chaoscrasher

    Thanks guys! That was the problem indeed :)
    I Thought Bukkit.getPlayer() returns a Player whether they are online or not and Bukkit.getOfflinePlayer() only if the player is offline.
    Seems like it was the reverse ;)

    Btw: Bukkit.getOnlinePlayer(String name) is deprecated. Does that mean they will delete the method some time in the future? I know that if you try to save values over the player name this can conflict, as with the implementation of UUIDs identities can change names. However in this case the method isn't used in the wrong way at all. So does deprecated generally mean "is going to be deleted soon" or just "warning! Don't use this method the way you used to!"?
     
  5. Offline

    MrZoraman

    That method is deprecated. The documentation explains why. Also a quote from EvilSeph:
    I believe the recommended route is to use the UUIDFetcher tool made by evilmidget38.
     
  6. Offline

    Gnat008

    MrZoraman
    Actually, I believe that the above changed with 1.7.9, so that getting a player from a name is blocking, and getting it from UUID is not.
     
  7. Offline

    Chaoscrasher

    What do you mean by "blocking"?
     
  8. Offline

    NonameSL

    I wasn't talking about Bukkit.getOfflinePlayer(uuid), I was talking about Bukkit.getOfflinePlayer(name) which is not deceprated, and getUniqueId() in OfflinePlayer also isn't deceperated.
     
  9. Offline

    Chaoscrasher

    But Bukkit.getOfflinePlayer(name) is deprecated >: (At least in 1.7.9)
     
  10. Offline

    Garris0n

    WinX64 likes this.
  11. Offline

    Chaoscrasher

    Okay I made it work utilizing MrZoraman's idea of implementing the UUIDFetcher tool (by evildmidget38) to do the job.
    It's a bit more complicated, but not by much and that's propably going to be the last change to that system anyway.

    So thanks to everyone who offered a solution! Your help was as generous and quick as usual :)
     
  12. Offline

    Garris0n

    For what you're doing you should just be able to use getOfflinePlayer().
     
Thread Status:
Not open for further replies.

Share This Page