How do I target Players?

Discussion in 'Plugin Development' started by cheer60823, Jul 5, 2013.

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

    cheer60823

    I've been creating a plugin and would like to know how to target players. ex: /heal player1
    whats the code to get player1? I know how to heal and setting up a command but not how to navigate to a certain player. Please Help!
     
  2. Offline

    Warreo

    Use this in your heal command:

    Code:
    String playerName = args[0];
    Player player = getServer().getPlayer(playerName);
    :D
     
  3. Offline

    ZeusAllMighty11

    In your situation, if it was a command:

    Code:
    Player p = Bukkit.getPlayer(args[0]);
    if(p != null){
        p.setHealth(Integer.parseInt(args[1]);
    }
     
  4. Offline

    cheer60823

    So where would I add it? @TheGreenGamerHD Warreo
    Code:java
    1. if(cmd.getLabel().equalsIgnoreCase("Heal")) {
    2. if(sender instanceof Player) {
    3. Player player = (Player) sender;
    4. player.setHealth(20);
    5. player.setFoodLevel(20);
    6. player.setLevel(100);
    7. player.sendMessage(ChatColor.GOLD + "You have been Healed, Feed, and Given XP!");
    8. } //end of player check
    9. else {
    10. sender.sendMessage(ChatColor.DARK_RED + "Only players can use this command!");
    11. } //end of else statement
    12. } //end of command check
     
  5. Offline

    Warreo

    Change it to this:

    So now you can use the command /heal [PlayerName]

    Code:java
    1. if(cmd.getLabel().equalsIgnoreCase("heal"){
    2. if(!sender instanceof Player){
    3. sender.sendMessage("Must be a player to use this command!");
    4. }
    5. Player toHeal = getServer.getPlayer(args[0]);
    6. toHeal.setHealth(20);
    7. toHeal.setFoodLevel(20);
    8. toHeal.setLevel(100);
    9. toHeal.sendMessage(ChatColor.GOLD + "You have been Healed, Feed, and Given XP!");
    10. }
     
  6. Offline

    cheer60823

    Theres some errors...
     

    Attached Files:

  7. Offline

    Henzz

    cheer60823
    What kind errors, what's causing it, what line? Post some stack-traces :)
     
  8. Offline

    cheer60823

    Such as

    Syntax error, insert ") Statement" to complete IfStatement

    getServer cannot be resolved

    Syntax error on token "else", delete this token
    Syntax error on token "}", { expected after this token

    Warreo Henzz
     
  9. Offline

    Warreo

    Sorry coded that quickly by hand with no IDE xD my bad here is the right code:
    Code:java
    1. if (cmd.getLabel().equalsIgnoreCase("heal")) {
    2. if (!(sender instanceof Player)) {
    3. sender.sendMessage("Must be a player to use this command!");
    4. }
    5. Player toHeal = getServer().getPlayer(args[0]);
    6. toHeal.setHealth(20);
    7. toHeal.setFoodLevel(20);
    8. toHeal.setLevel(100);
    9. toHeal.sendMessage(ChatColor.GOLD + "You have been Healed, Feed, and Given XP!");
    10. }
     
  10. Offline

    cheer60823

    Thank you very much for your effort :D
     
  11. Offline

    Warreo


    No problem! Sorry about screwing that up xD
     
  12. Offline

    cheer60823

    Its ok! Without your help I wouldn't have it! :D
     
  13. Offline

    MgMaor

    Player t = Bukkit.getPlayer(args[0]);
     
    cheer60823 likes this.
Thread Status:
Not open for further replies.

Share This Page