Get a player's uuid by his name

Discussion in 'Plugin Development' started by Xibalba7, Jul 16, 2014.

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

    Xibalba7

    Hi ! I want to make to VIP plugin (gives vip by command) compatible with UUID but I ran into an issue :

    What my plugin do is giving a vip account to Player123 when I use the command /vip Player123.

    The .getPlayer("Player123") as been deprecated but I don't know how to get Player123's UUID in order to use the non deprecated .getPlayer(uuid) method.

    Any help is welcome,
    Xibalba.

    PS : I do not store anything (uuids or playernames)
     
  2. Offline

    dsouzamatt

    Xibalba7 Even though it's been deprecated, in cases like these it's fine to use .getPlayer(String) to get a player from their string name.
     
  3. Offline

    MCForger

    Xibalba7
    You could just loop through all of the online players until you find an online player name with the same name as the one in your command argument. This is basically the same as calling the deprecated method though (getPlayerExact()).
    Example:
    Code:java
    1. String targetName = "MCForger";
    2. Player player = null;
    3. for (Player online : Bukkit.getServer().getOnlinePlayers())
    4. {
    5. if (online.getName().equalsIgnoreCase(targetName))
    6. {
    7. player = online;
    8. }
    9. }
    10.  
    11. if (player == null)
    12. {
    13. // Player is not currently online.
    14. return;
    15. }
     
  4. Offline

    Deleted user

    There's no point in trying to bypass deprecation when it's with code like this.

    The deprecation was placed to draw attention to the fact that Bukkit#getPlayer() is no longer safe when it comes to file storage, and players should be stored by UUID instead.

    MCForger all you do is do getPlayerExact in a different way. This is completely pointless and shows you have understood nothing about the deprecation.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 8, 2022
    tommyhoogstra likes this.
  5. Offline

    Xibalba7

    Ok thanks, I won't change anything then.

    I thought I had read something like "deprecated methods will be removed" but in fact it says "UUID awareness deprecations will be removed"
     
  6. Offline

    Deleted user

    MCForger
    Well, same thing. getPlayer does some vague guessing

    Player found = null;
    String lowerName = name.toLowerCase();
    int delta = Integer.MAX_VALUE;
    for (Player player : getOnlinePlayers()) {
    if (player.getName().toLowerCase().startsWith(lowerName)) {
    int curDelta = player.getName().length() - lowerName.length();
    if (curDelta < delta) {
    found = player;
    delta = curDelta;
    }
    if (curDelta == 0) break;
    }
    }
    return found;
    While getPlayerExact loops through the players. There's no point in re-inventing the wheel here.
     
Thread Status:
Not open for further replies.

Share This Page