getHealth(): How do I add health to a player?

Discussion in 'Plugin Development' started by Serventor, Jul 3, 2013.

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

    Serventor

    Hey Bukkit!
    This is my first actual post on this forum. So, I want to add health if you right-click a paper (bandage). I want to add 5 hearts (= 10 health points). I tried to code this, but Eclipse says it's an error. This is the whole file:
    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
     
    public class BandageUse implements Listener {
     
    @EventHandler
    public void onBandageUse(PlayerInteractEvent e) {
     
    if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
     
    if (e.getPlayer().getItemInHand() == new ItemStack(Material.PAPER)) {
     
    e.getPlayer().getInventory().removeItem(new ItemStack(Material.PAPER, 1));
    e.getPlayer().setHealth(e.getPlayer().getHealth() + 10);
    e.getPlayer().sendMessage(ChatColor.GREEN + "You used a bandage and got 5 hearts.");
     
    }
     
    }
     
    }
     
    }
    This line is the problem: e.getPlayer().setHealth(e.getPlayer().getHealth() + 10);. Eclipse underlines getHealth() red, and gives this error: The method getHealth is ambiguous for the type Player.
    Thanks!

    Regards,
    Serventor
     
  2. Offline

    Sessional

  3. Offline

    stirante

    Can you give some really simple example?

    Btw. I mean how to GET health
     
  4. Offline

    Sessional

    e.getPlayer().setHealth((double)e.getPlayer().getHealth() + (double)10)
    e.getPlayer().setHealth(e.getPlayer().getHealth() + 10.0d) (Read this somewhere, no idea if it works)

    Which version of bukkit are you building against? And this may not be the problem, just makes sense for the changes that happened.
     
  5. Offline

    ERROR372

    Change:
    e.getPlayer().setHealth(e.getPlayer().getHealth() + 10);
    To:
    e.getPlayer().setHealth((float)e.getPlayer().getHealth() + (float)10);
    or
    e.getPlayer().setHealth((double)e.getPlayer().getHealth() + (double)10);

    Either should work.
     
  6. Offline

    Wingzzz

    Make sure to check if newHealth >= 20, if it is, then just set health to 20 because you can't add 10 to 20 as it would then be 30, unless using a custom health system minecraft's limitations are 0-20, I'm sure you're already aware though ;)
     
  7. Offline

    Sagacious_Zed Bukkit Docs

    Serventor You NEED to compile against the api. (Commonly named bukkit.jar)
     
    dark navi likes this.
Thread Status:
Not open for further replies.

Share This Page