ArrayList of players, get the player whose name matches String

Discussion in 'Plugin Development' started by MrSnare, Mar 15, 2013.

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

    MrSnare

    I am trying to return the player whose name matches "MrSnare"

    Code:
    ArrayList<Player> onlinePlayers = new ArrayList<String>();
    String target = "MrSnare";
    if(onlinePlayers.contains(target)){
    onlinePlayers.get(target)
    }
     
  2. Offline

    KingKongC

    Are you trying to do this via a command eg. /who [name] or when the player joins? Could you also give us more detail?
     
  3. Offline

    Codisimus

    Bukkit.getPlayer("MrSnare");
     
  4. Offline

    Icyene

    Umm...

    In your line 'ArrayList<Player> onlinePlayers = new ArrayList<String>()', you are trying to instantiate an ArrayList of players... by setting it to an ArrayList of Strings... ArrayLists may be generic, but the same constraints apply to them that apply to other objects! You can't do int a = "Hi", just like how you can't do what you wanted with that line.

    Instantiate it to a list of players or Strings, not one of each.

    And that's the nasty way, you should use Bukkit.getPlayer(String) or Bukkit.getPlayerExact(String). The first is slower but if you have a player named "Bob", calling getPlayer on "bob" will return him.
     
    Codisimus likes this.
  5. Offline

    Codisimus

    Never looked at the difference between these, very good to know
     
Thread Status:
Not open for further replies.

Share This Page