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


    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:
    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 {
    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.

  2. Offline


  3. Offline


    Can you give some really simple example?

    Btw. I mean how to GET health
  4. Offline


    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


    e.getPlayer().setHealth(e.getPlayer().getHealth() + 10);
    e.getPlayer().setHealth((float)e.getPlayer().getHealth() + (float)10);
    e.getPlayer().setHealth((double)e.getPlayer().getHealth() + (double)10);

    Either should work.
  6. Offline


    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