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!
I want to know how to heal other players. Spoiler: Code: 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!"); } }
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]);
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.
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 }
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