String to player

Discussion in 'Plugin Development' started by HamOmlet, Jun 22, 2012.

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

    HamOmlet

    I'm trying to get a player without color codes with the below code:
    Code:
      public static boolean has(Player player, String permission) {
          String name = ChatColor.stripColor(player.getName());
        return player.hasPermission(permission);
      }
    The problem I'm having is converting the string into a player. I've tried Bukkit.getPlayer(name), but I get a NullPointerException.

    Any help?
     
  2. you already have an player?
     
  3. Offline

    Firefly

    If you use Bukkit.getPlayer(name) and the player is offline, you may get a NullPointer. Before doing anything to the player you have to check it with something like...

    Code:
    Player player = Bukkit.getPlayer(name);
     
    if (player != null && player.isOnline()) {
        //Do whatever with the player here
    }
    [EDIT] But as @ferrybig said, you already have a Player object since your method requires you to pass a player to it.
     
Thread Status:
Not open for further replies.

Share This Page