Help With .getPlayer(args[0]);

Discussion in 'Plugin Development' started by Joshuabohning, Apr 9, 2014.

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


    This is my code:

    import java.util.logging.Logger;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;

    public class Youtube extends JavaPlugin {
    public final Logger logger = Logger.getLogger("Minecraft");
    public static Youtube plugin;

    public void onDisable() {
    PluginDescriptionFile pdfFile = this.getDescription(); + " Has Been Disabled!");

    public void onEnable() {
    PluginDescriptionFile pdfFile = this.getDescription(); + " Version " + pdfFile.getVersion() + " Has Been Enabled!");

    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player player = (Player) sender;

    if(commandLabel.equalsIgnoreCase("heal") || commandLabel.equalsIgnoreCase("h")){
    if(args.length == 0){
    //heal = 0 args /heal Robot_kid = 1 args
    player.sendMessage(ChatColor.GREEN + "You Have Been Healed!");
    }else if(args.length == 1){
    Player targetPlayer = player.getServer().getPlayer(args[0]);
    player.sendMessage(ChatColor.GREEN + "You Have Been Healed!");


    So i was following a tut and ran into a problem... On this line of code the .getPlayer(args[0]); has a line through it and i dont know what to do.
    Player targetPlayer = player.getServer().getPlayer(args[0]);
  2. Offline


    Instead of:
    1. Player targetPlayer = player.getServer().getPlayer(args[0]);

    1. Player targetPlayer = this.getServer().getPlayer(args[0]);

    And I recommend also wrapping a try catch around it to check if the player is online:
    1. Player targetPlayer;
    2. try{
    3. targetPlayer = this.getServer().getPlayer(args[0]);
    4. }catch(Exception e){
    5. player.sendMessage("This player is not online!");
    6. return true;
    7. }
    8. targetPlayer.setHealthScale(20);
    9. targetPlayer.sendMessage("You have been healed!");
    10. player.sendMessage(args[0] + " has been healed!");
  3. Offline



    A heal plugin wouldn't be much if you didn't heal the Hunger, So I suggest you add the following below.

    1. player.setFoodLevel(20);
  4. Offline


    The getPlayer(String name) method is deprecated to raise awareness of the upcoming UUID changes that will be a part of the 1.8 update. You can still use it, and it should work fine.

    Er... what? The getPlayer(String name) method does not throw an exception if the player is not online.. it simply returns null.
  5. amhokies Although technically, if the catch is moved down to the bottom, you could catch the NPE
  6. Offline


    Please use UUID for the getPlayer();
    1. Player targetPlayer = this.getServer().getPlayer(UUID.fromString(args[0]));
  7. Offline


    This problem occured for me yesterday as well, after trawling through the internet, I found that it is because of the upcoming 1.8 release. I'm sure you're aware that in 1.8 name changes will be supported, and are currently supported in 1.7.7, but Mojang has yet to release an actual method of changing a player name. Anyway, for a player to change name, but to keep th profile the same, a player is now tethered to a unique ID, or a UUID. The reason you are getting the yellow underline, and the strike through the word, is simply a warning from Bukkit to say that getName(args[0]) will not work with 1.8, and you will need to get their ID, not their name. However, that is 1.8, but will still work absolutly fine with 1.7, and probably even 1.6 or 1.5. It heavily advised therefore, to make sure you update your plugin so that it fetches the ID instead of the name, as soon as 1.8 is released. :)
  8. Offline


    Asgernohns No need. As long as he doesn't store any data to disc, player name is as good as UUID, since it has to be unique either way. Also, UUID.fromString would require args[0] to be a UUID formatted string, and no one wants to type that :p

    xTeCnOxShAdOwZz That doesn't mean that the player.getName will get thrown out entirely, right? You could still look for players via name - as long as the scope of the search is only the current session, it's fine.

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


    Methods involving usernames are deprecated now, since 1.8 will use the UUID system.
  10. Offline


    Yes of course, but it would good to get into the habit of doing that. :)
  11. Offline


    ZeusAllMighty11 So there won't be a way to return the player name? UUID's are unwieldy if I want to just, for example, teleport to an online player, or display a message for him.
  12. Offline


    To confirm, a quick google answered it; anything that only involves a player's name, such as getting their display name, or sending a message, can be done with getName(). However, if you wish do address something such as an account or profile, e.g. money or permissions, you need to fetch their UUID with getUUID().
    Zethariel likes this.
  13. Offline


    Zethariel ohh.. Yes im sorry. I'm using a uuid to name and name to uuid class :) my fault
  14. Offline


    Thanks guys for the help!

    much appreciated!

Thread Status:
Not open for further replies.

Share This Page