Solved Get a player's health using command

Discussion in 'Spigot Plugin Development' started by JackPlayzMC, Nov 14, 2016.

Thread Status:
Not open for further replies.
  1. .
    #1 JackPlayzMC, Nov 14, 2016
    Last edited: Dec 2, 2016
  2. Right, well you want to listen for the command /health. You then want to check the command arguments to make sure they have provided a player as a parameter. Once you have got the player they are trying to view the health of, you can send a message to the command sender with the players health. I'll provide an example:
    Code (Text):
        public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
            if (cmd.getName().equalsIgnoreCase("health")) {
                //Make sure they are providing the player in the arguments
                if (args.length == 0) {
                    sender.sendMessage("Please provide a player!");
                    return true;
                //Getting the player from the name the command sender provides
                Player target = Bukkit.getPlayer(args[0]);
                //Check if the player is online, if he isn't we can't get his health!
                if (target == null) {
                    sender.sendMessage("That player is not online!");
                    return true;
                //Finally, send the command sender the players health!
                sender.sendMessage(target.getName() + "'s health is: " + target.getHealth());
                return true;
            return false;
  3. Nice spoonfeed
    • Agree Agree x 2
    • Winner Winner x 1
  4. Thank you very much sir. I know I spoonfed the OP, however he said that he does not have much experience in coding, and I thought providing an example would be a good way for him to see how things work. He wanted assistance with writing a few lines of code for the example he provided, I'm not giving him the solution to a big plugin that he will be actively using (as I'm sure you can recognise, this is obviously an example for the OP to learn the Bukkit API), I am providing a basis for him to see how it works and play around with commands in Bukkit.
    • Like Like x 1
    • Agree Agree x 1
  5. Thank you! You are exactly right. I wanted to get into the Bukkit API and thank you very much for providing this example. It'll help me make bigger plugins! :D
    • Friendly Friendly x 1
  6. If Joe has answered your question, I would suggest marking the thread as solved :)
    • Friendly Friendly x 2
  7. He game comments for the op to understand so it isnt actually spoon feeding if you think about it and OP pls mark da thread as Solved
Thread Status:
Not open for further replies.