Can't figure out how to get a player by display name.

Discussion in 'Plugin Development' started by harry10potter, May 25, 2014.

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

    harry10potter

    Hi! I'm trying to make a simple nickname plugin and I'm having trouble getting a player by their display name... ._. I have gotten it to set a nick but I just can't think of a way to find the player by the nick. I am trying to develop a little plugin for my brother on is RPG server. Thanks in advance!
     
  2. Offline

    Etsijä

    Player.getDisplayName().
     
    Garris0n likes this.
  3. Offline

    harry10potter

    That just gets the name. It cant identify a player BY that name as far as I know.
     
  4. Offline

    Garris0n

    Loop through all players and check for a match.

    Return the match.

    ???

    Profit.
     
    mythbusterma likes this.
  5. Offline

    harry10potter

    I'll try that...

    Garris0n Well then... Every single time I run the command it crashes the server XD
    Code:

    Code:java
    1. if(commandLabel.equalsIgnoreCase("realname")){
    2. if(args.length == 0){
    3. player.sendMessage(ChatColor.RED + "Cannot find player with that nickname!");
    4. }else if(args.length == 1){
    5. String toggle = "no";
    6. while(toggle == "no"){
    7. List<Player> onlinePlayers = Arrays.asList(Bukkit.getServer().getOnlinePlayers());
    8. Iterator<Player> iterator = onlinePlayers.iterator();
    9. while(iterator.hasNext()){
    10. Player onlinePlayer = iterator.next();
    11. if(onlinePlayer.getDisplayName().equalsIgnoreCase(args[0])){
    12. player.sendMessage(onlinePlayer.getDisplayName() + "'s realname is " + onlinePlayer.getName());
    13. toggle = "yes";


    Any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  6. Offline

    mine-care

    Lol people why not Bukkit.getplayer("string") or getplayerexact("string") and then check if its null :)
     
  7. Offline

    PreFiXAUT

    1. Please change the toggle to a boolean and don't check a String with "==", this won't work propertly. Use the "StringObject.equals("Anything")".
    2. Do it simpler:
    Code:java
    1. for (Player p : Bukkit.getOnlinePlayers()) {
    2. if (!p.getDisplayName().equalsIgnoreCase("CheckName") continue;
    3. // Handle Player
    4. }
     
    Garris0n likes this.
  8. Offline

    Etsijä

  9. Offline

    harry10potter

    Sorry, I'm kinda new to coding :p This is a screenshot of what I have:

    [​IMG]

    It just crashed. No errors, just a huge lag burst serverside then kicked.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  10. Offline

    Lone_MisfitMC

    I'm with mine-care.. I just use:

    Code:java
    1. Player p = Bukkit.getServer().getPlayerExact("Lone");


    IIRC, it is just marked as depreciated to drawn attention to the UUID change. If you're certain of the player name, you should be safe using it.

    Lone
     
    mine-care likes this.
  11. Offline

    PreFiXAUT

    Uhm, the "onlinePlayer" is not defined. Lemme re-write it:
    Code:java
    1. // Checking if it's "my" Command
    2. if (!commandLabel.equalsIgnoreCase("realname")) return true;
    3. // Check if a nickname is even entered. if not return a message.
    4. if (args.lenght == 0) {
    5. player.sendMessage(ChatColor.RED + "Enter a nickname!");
    6. return true;
    7. // Nickname has been entered! Maybe we find someone.
    8. } else if (args.lenght == 1) {
    9. // It's getting all Players, one after another and saves each Player as p
    10. for (Player p : Bukkit.getOnlinePlayers()) {
    11. // The Player doesn't have our searched name. Continue the loop and get next player to check
    12. if(!p.getDisplayName().equalsIgnoreCase(args[0])) continue;
    13. // Player has been found. Sending message and stoping Command.
    14. player.sendMessage(args[0] + "'s realname is " + p.getName());
    15. return true
    16. }
    17. // Tell the sender no Player was found, otherwise it would have told him alread.
    18. player.sendMessage(ChatColor.RED + "No Player has been found with this nickname!");
    19. return true;
    20. }
    21. player.sendMessage(ChatColor.RED + "Wrong useage! Use " + ChatColor.GOLD + "'/realname <player>'" + ChatColor.RED + " to get the real Name of a Player!");

    I hope with the Comments it will help you to understand it :)
     
  12. Offline

    harry10potter

    THX!

    PreFiXAUT I tested the code and I didn't get any errors, but it couldn't find the nickname.

    [​IMG]

    My Code BTW:
    Code:java
    1. if (commandLabel.equalsIgnoreCase("realname")){
    2. if (args.length == 0) {
    3. player.sendMessage(ChatColor.RED + "Enter a nickname!");
    4. }else if (args.length == 1) {
    5. for (Player p : Bukkit.getOnlinePlayers()) {
    6. if(!p.getDisplayName().equalsIgnoreCase(args[0])) continue;
    7. player.sendMessage(args[0] + "'s realname is " + p.getName());
    8. return true;
    9. }
    10. player.sendMessage(ChatColor.RED + "No Player has been found with this nickname!");
    11. return true;
    12. }
    13. player.sendMessage(ChatColor.RED + "Wrong usage! Use " + ChatColor.GOLD + "'/realname <player>'" + ChatColor.RED + " to get the real Name of a Player!");
    14. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  13. Offline

    PreFiXAUT

    The thing is, the Display-Name is searched (it's made by me on purpose because it's what your plugin should do as far as I saw), so when the Display-Name of a Player is in Green, you also need to enter the name in green for example.

    #EDIT: I see that your name is not "somethingelse", it's actually " somethingelse " with spaces. name yourself without them then it should work.
     
  14. Offline

    mine-care

    Thanks dude :D

    Btw, not the pro's way but how about adding the nickname of the player as key and value the real name of this player on a hashmap? So you can load them after a reload or restart?
    It will be a hashmap of string and string, I can make ne for you if u want :-D

    Sorry for spelling issues {DAMN U AUTOCORECT}

    Okay so u want me to give u a example?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  15. mine-care Lone_MisfitMC getPlayer() and getPlayerExact() is based on the player's name, not their display name.
     
  16. Offline

    mine-care

    ehh at first by the threat tit;e i thought that :S But still see ma latest posts,
     
  17. Offline

    mythbusterma

    You can use ChatColor.stripColor(String) to remove the coloring from their name, and then you can try player.getDisplayName ().contains (ChatColor .stripColor (arg)) to check for a partial match.
     
  18. Offline

    Etsijä

    At first I thought this would be the upside-down way of doing it, since the key is then not necessarily unique, while the value would be. Then I thought - there's probably nothing in implementation of HashMaps preventing or even discouraging this kind of use, so it could actually be beneficial to implement it this way around!
     
  19. Offline

    mine-care

    Etsijä The reason i said the nick as key, ite asyer to acess it from anywhere, like hashmap.get(key); that will return the actual players name.
     
  20. Offline

    Etsijä

    Yep, understood that and I think it's a good solution.
     
    mine-care likes this.
  21. Offline

    mine-care

    :D but the author of the treat hasn't replayed yet :/
     
  22. Offline

    harry10potter

    Sorry! I never got notifications!
    mine-care mythbusterma @Etsija AdamQpzm

    @Etsijä
    I didnt get tag right last time

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  23. harry10potter You can just click the "tahg" link under the person's post and it will do it for you :) i.e. Etsijä
     
Thread Status:
Not open for further replies.

Share This Page