Lose Hearts (Health) on Command

Discussion in 'Plugin Development' started by Tolerance, Jul 12, 2013.

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

    Tolerance

    Well, I'm learning coding in Minecraft and I learn by my problems/questions being explained. With that being said, I'm just trying to do basic stuff. Don't make fun of me because I probably sound like a retard or am doing something completely wrong. xD I'm trying to make it so when a user does /health they lose lots of there health - currently, when I do it I get an error on '.getPlayer' and '.setHealth' is crossed out. :\

    Here's my fail:
    Code:
            public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if (cmd.getName().equalsIgnoreCase("health")){
                Player player = (Player)sender;
                Player target = player.getWorld().getPlayer(args[0]);
                target.setHealth(50);
                    }
                    return false;
    This is also in my main... should it not be?
    remember no mean comments lol, and thanks for all help :)

    Bump.

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

    SnipsRevival

    It has only been 20 minutes, so there is no need to bump.

    From what I understand, setHealth() requires a double now instead of an int. And try Bukkit.getServer().getPlayer(args[0]) instead, but make sure you check how many args are in your command before using args[0] otherwise you may get an ArrayIndexOutOfBoundsException.
     
  3. Offline

    Tolerance

    SnipsRevival

    What do you mean by a double?

    This is how it looks now:
    Code:
            public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if (cmd.getName().equalsIgnoreCase("health")){
                Player player = (Player)sender;
                Bukkit.getServer().getPlayer(args[0]);
                target.setHealth(50);
    and the only thing I'm getting an error on is 'target' on the last line
     
  4. Offline

    foodyling

    What variable is set as 'target'? Are you forgetting to set Bukkit.getServer().getPlayer(args[0]); as the target?
    And to answer your question on doubles, Entity health is now stored as doubles, which allow fractional health. eg. target.setHealth(2.5D);
     
  5. Offline

    Tolerance

    I replaced 'target' with player and now I'm getting no errors.. :)

    Code:
            public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if (cmd.getName().equalsIgnoreCase("health")){
                Player player = (Player)sender;
                Bukkit.getServer().getPlayer(args[0]);
                player.setHealth(2.5D);
                   }
           return false;
    [SIZE=15px][FONT=Georgia]}
    [/FONT][/SIZE]
    So will this work now? If a user types /health is will lower there hearts, correct?
     
  6. Offline

    foodyling

    Whats the point of Bukkit.getServer().getPlayer(args[0]) then? Are you trying to do /health player and that will heal the target player?
     
  7. Offline

    Tolerance

    Nah, I'm trying to make it so when they /health they lose health.
     
  8. Offline

    foodyling

    If you're doing that then do something like this:
    Code:
    if (label.equalsIgnoreCase("health")) {
      if (sender instanceof Player) {
        Player player = (Player) sender;
        player.setHealth(Math.max(0, player.getHealth() - 2D));
      } else {
        sender.sendMessage("Only players can execute this command");
      }
    }
    Which would remove 1 heart from the player
     
  9. Offline

    Tolerance

    Thanks for your help btw.
    When I do that I get an error on .getHealth()
     
  10. Offline

    foodyling

    Then post the error
     
  11. Offline

    Tolerance

    The method getHealth() is ambiguous for the type Player
     
  12. Offline

    foodyling

  13. Offline

    Tolerance

    foodyling

    so like:
    Code:
     double hp = getHealth();

    ?
    But where would I put that on:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (label.equalsIgnoreCase("health")) {
                  if (sender instanceof Player) {
                    Player player = (Player) sender;
                    player.setHealth(Math.max(0, player.getHealth() - 20));


    @Cirno
    Can you help me? :( You seem like you know what you're doing lol

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

    ZeusAllMighty11 Retired Staff

    Please stop bumping your posts.


    You need to learn java, and read up on the plugin tutorial.


    Health is ambiguous, you must compile against Bukkit
     
    foodyling likes this.
  15. Offline

    Tolerance

    LOL dafuq
    Well actually I was asking about health specifically because I didn't understand the change in the recent update? And i've bumped my post twice. Sue me.
     
  16. Offline

    ZeusAllMighty11 Retired Staff

    Tolerance

    Nobody will help you with your attitude. My signature explains this so well.

    In most simple words:
    Show Spoiler

    - Learn Java
    - Learn Java
    - Learn More Java
    - Learn even more Java
    - Read some bukkit stuff
    - Learn MORE java
    - When you think you know enough java, read more
    - Test some bukkit stuff
    - Debug any issues
    - Post any issues which can not be debugged
    - Learn java
     
    foodyling likes this.
  17. Offline

    Tolerance

    Show Spoiler

    Yes, I cannot understand why my health problem is not working. I've read the post and I still do not understand how to fix it. It's not like I'm asking someone to write me a program lol it's like 5 lines of code...
     
  18. Offline

    ZeusAllMighty11 Retired Staff

    Then put this in plugin requests.
     
Thread Status:
Not open for further replies.

Share This Page