Solved Healing problem

Discussion in 'Plugin Development' started by Threehundredcc2, Jan 19, 2013.

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

    Threehundredcc2

    I was adding instant healing soup to my plugin and I realized I could heal fully and noticed that the reason was because I can't add over 20 halfhearts.

    My code:

    Code:
      @EventHandler
      public void onRightClickSoup(PlayerInteractEvent event) { Player player = event.getPlayer();
        int health = player.getHealth();
        if (((player.hasPermission("pvpmc.instantheal")) || (player.isOp())) &&
          ((event.getAction().equals(Action.RIGHT_CLICK_AIR)) || (event.getAction().equals(Action.RIGHT_CLICK_BLOCK))) &&
          (player.getInventory().getItemInHand().getTypeId() == 282)) {
          player.setHealth(health + 8);
          player.getInventory().getItemInHand().setType(Material.BOWL);
    I can't figure out how I can make it so they can still get healed from the soup even if they have over 8 halfhearts(4 hearts). If someone could help that would be great!
     
  2. Offline

    ZeusAllMighty11

    if(player.getHealth() <= 12){
    player.setHealth(health + 8);
    } else {
    player.setHealth(20);
    }


    I had this issue too the otehr day, this worked for me. There was also a ternary function thing to help with this... maybe fireblast709 knows
     
  3. Offline

    Threehundredcc2


    I got it to work like a charm with this:
    Code:
      @EventHandler
      public void onRightClickSoup(PlayerInteractEvent event) { Player player = event.getPlayer();
        int health = player.getHealth();
        if (((player.hasPermission("pvpmc.instantheal")) || (player.isOp())) &&
          ((event.getAction().equals(Action.RIGHT_CLICK_AIR)) || (event.getAction().equals(Action.RIGHT_CLICK_BLOCK))) &&
          (player.getInventory().getItemInHand().getTypeId() == 282) && (player.getHealth() <= 12)) {
          player.setHealth(health + 8);
          player.getInventory().getItemInHand().setType(Material.BOWL);
        } else {
          player.setHealth(20);
          player.getInventory().getItemInHand().setType(Material.BOWL);
    But I still use the soup even if I have 20 health, anyone know how to prevent this?
     
  4. Offline

    ImTheFool

    Isn't your else block setting everyone's health to 20 who doesn't have that permission, or right clicks with a random item in their hand, or left clicks with any item?
     
  5. Offline

    fireblast709

    player.getHealth() <= 12 ? player.setHealth(player.getHealth()+8) : player.setHealth(20); is what you meant
    Yes, yes he does by trying to combine all the if-statements.

    Threehundredcc2 try splitting the if-statements in smaller parts, with the middle one being the health check (or the ternary in this post)
     
    Threehundredcc2 likes this.
  6. Offline

    Threehundredcc2

    Thanks! That helped me fix the healing with other items problem. It works perfect except for one thing, it still lets me eat even when I have full health, I tried this but it still doesn't work properly

    Code:
      @EventHandler
      public void onRightClickSoup(PlayerInteractEvent event) { Player player = event.getPlayer();
        if (((player.hasPermission("pvpmc.instantheal")) || (player.isOp())))
        if ((event.getAction().equals(Action.RIGHT_CLICK_AIR)) || (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)))
          if (player.getInventory().getItemInHand().getTypeId() == 282)
          if  (player.getHealth() <= 12) {
          player.setHealth(player.getHealth()+8);
          player.getInventory().getItemInHand().setType(Material.BOWL);
        } else {
          if (player.getHealth() == 20)
              player.sendMessage(ChatColor.AQUA + "You health is already max!");
              event.setCancelled(true);
        } else {
            if (((player.hasPermission("pvpmc.instantheal")) || (player.isOp())))
            if ((event.getAction().equals(Action.RIGHT_CLICK_AIR)) || (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)))
              if (player.getInventory().getItemInHand().getTypeId() == 282)
              if (player.getHealth() >= 12) {
                player.setHealth(20);
                player.getInventory().getItemInHand().setType(Material.BOWL);
    bump ^^

    Nevermind solved it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
Thread Status:
Not open for further replies.

Share This Page