How do you target players?

Discussion in 'Spigot Plugin Development' started by RedNinjaPlaysMC, May 16, 2016.

  1. Hey guys, I'm working on a plugin right now, but I can't seem to make /heal <player> work. I'm not sure how to target <player> and run the command to him. Please help me!
     
    • Optimistic Optimistic x 1
  2. Could I have the snippet of code you are working with
     
  3. I want to know how to heal other players.
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    Player player = (Player) sender;
    if(commandLabel.equalsIgnoreCase("heal"))
    {
    if(player.hasPermission("heal.heal"))
    {
    if(args.length == 0)
    {
    player.setHealth(20);
    player.setFoodLevel(20);
    player.sendMessage(ChatColor.AQUA + "You have been healed!");
    }
    if(args.length == 1) {
    player.sendMessage(ChatColor.RED + "Your command conatains invald arguments!");
    }
    }
    else
    {
    player.sendMessage(ChatColor.RED + "You do not have permission to use this command!");
    }
    }
     
    • Useful Useful x 1
  4. Code (Text):
    if (!(getServer().getOnlinePlayers().contains(getServer().getOfflinePlayer(args[0])))) {
                                p.sendMessage("§cThe player wasn't found.");
                                return true;
                            }
    Player target = getServer().getPlayer(args[0]);
     
  5. Check if the name matches a player that is currently online, using Server.getOnlinePlayers , i think it is called. There may even be a getPlayer(String) method, but i am not aware of it.
     
  6. To obtain and get other online players you will have to do the following.

    • Check command arguments and make sure a player is being listed
    • Use method #getPlayerExact, which allows for an input string
    • Make a condition checking if #getPlayerExact returns null and follow up with additional modifications after
    An example would be as shown:

    Code (Text):

    Player target = Bukkit.getPlayerExact(...);
    if (target != null)
    {
        //player is presumed to be online
    }
    else if (target == null)
    {
        //player is presumed to be offline
    }
     
     
    • Agree Agree x 2
  7. make the <player to heal> into an arg[], and have it sent for onlinePlayers... and if that player is online, have arg[] set as the players name (.getName()), and then set health as full (or 20 levels...) I don't know if this will help at all XD
     
  8. Use arguments
    (Look up in internet, its an highly asked question)
     
  9. Why getPlayerExact() when you can use getPlayer()?