Get a player's half name?

Discussion in 'Plugin Development' started by Tauryuu, Mar 1, 2012.

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


    I know a few plugins that can detect an online player's short name, how is it done?

    For example: /kick Taur would kick an online player called Tauryuu. If there are more than 1 person with the prefix Taur, then it wouldn't work.

    Thanks in advance. :)
  2. Offline


    Create a list of player names, iterate through the list, and compare the given parameter to the first letters of the same length using substring and equalsIgnoreCase().
  3. Offline


    1. private Player findPlayerByName(String playerName) {
    2. Player player = Bukkit.getServer().getPlayer(playerName);
    3. if (player == null) {
    4. return null;
    5. } else {
    6. return player;
    7. }
    8. }
  4. I belive server.matchPlayer() will find players that contain a string, not necesarily from the start... it returns a list, first you must check against null and then check if it's only one item in the list, if it's not, then you should tell the command sender that there are more players matching the criteria.

    However, I belive that getPlayer() would do something similar to matchPlayer() since getPlayerExact() exists.
  5. Offline


    I think getPlayerExact() is just case sensitive (Including actual spelling).
    Of course correct me if I'm wrong
  6. Offline


    Gentlemen, there are javadocs and other resources for your believes ;)

    getPlayer, returns player if one matches the string
    getExactPlayer, returns player if whole string matches whole name of player (still case insensitive though, so are playernames in essence)
    matchPlayer, returns all players that match the string (so a List <Player>)
  7. Well, you should use matchPlayer() because it can provide usefull information (like your string matched more than one player).

    Also, I looked into the javadocs but the descriptions are kinda vague...
  8. Offline


    I agree, a few extra lines of description or an example wouldn't hurt. But I'd like to note that a 'match' in Java (and most programming languages) is something that checks for a 'needle' in an 'haystack'. Else we would talk about two objects being 'equal' or something like that :p
  9. Offline


                  if (player.getServer().getPlayer(args[0]) != null) {
                    Player targetplayer = player.getServer().getPlayer(args[0]);
    What i use and it works with shortened names
Thread Status:
Not open for further replies.

Share This Page