Checking if they have full health?

Discussion in 'Plugin Development' started by SirLittlemonster7, Oct 26, 2012.

Thread Status:
Not open for further replies.
  1. Hey guys, I was wondering why I can still eat the soup when I have full health. I was wondering if I could check if they have full health and if they do, it doesn't use this code.



    1 more thing, I can eat my sword when i'm low on health, why is that?
    Code:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK && player.hasPermission("LittleSoup.Use") && player.getItemInHand().getType() == Material.MUSHROOM_SOUP && player.getHealth() < player.getMaxHealth()) {
            int heal = 8;
            player.setHealth(player.getHealth() + heal > player.getMaxHealth() ? player.getMaxHealth() : player.getHealth() + heal);
            event.getPlayer().getItemInHand().setType(Material.BOWL);
        }
    }
    }
    Please, I need help :(

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

    davejavu

    Code:java
    1. if (player.getHealth() == 20) {
    2. //Do something
    3. } else {
    4. //Do something else
    5. }

    Basically, 1 "health" is half a heart, so 20 health is 10 hearts (max health) whereas 1 health is half a heart.
    Hope this helps.
     
  3. Thanks trying right now!

    I can still eat whenever :/ I can also eat my sword? whats happening? heres the code:
    PHP:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        
    Player player event.getPlayer();
        if (
    player.getHealth() == 20) {
            if (
    event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK && player.hasPermission("LittleSoup.Use") && player.getItemInHand().getType() == Material.MUSHROOM_SOUP && player.getHealth() < player.getMaxHealth()) {
                
    int heal 8;
                
    player.setHealth(player.getHealth() +  heal player.getMaxHealth() ? player.getMaxHealth() : player.getHealth() + heal);
                
    event.getPlayer().getItemInHand().setType(Material.BOWL);
                
    player.setFoodLevel(20);
                
    player.setSaturation(20);
            } else {
            
    //Do something else
            
    }
        }
    }
    }
    Ohhhh Nvm, I put it in the: If Health = 20 part sorry, stupid me :p
    1 more thing, I can eat my sword when i'm low on health, why is that?

    1 more thing, I can eat my sword when i'm low on health, why is that?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  4. Offline

    kabbage

    I'm going to guess and say:

    if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK && other stuff…

    should be

    if ((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) && other stuff…
     
  5. Offline

    devilquak


    Because of this line of code:
    PHP:
    if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK && player.hasPermission("LittleSoup.Use") && player.getItemInHand().getType() == Material.MUSHROOM_SOUP && player.getHealth() < player.getMaxHealth())
    Take a closer look at it. The way "or" statements work is very specific, you're asking for either a right-click of a block with soup in the player's hand, with permissions, and if they have under 20 health, or JUST a right click of air. Literally, you're asking (right click air)OR(right click block + permission + holding soup + has under 20 health).
    Instead, do something like this:
    HTML:
    if(event.getAction() == Action.RIGHT_CLICK_AIR||event.getAction() == Action.RIGHT_CLICK_BLOCK) //If player right clicks a block or air, continue
    {
    if(//permissions, soup, and health code here) //If player has all these things as well, continue
    {
    //Code to be performed
    }
    }
     
  6. I dont realy get it :(
     
  7. Offline

    devilquak


    You should try to learn more Java in general, it will really help.

    I don't really know how to explain it any clearer, but I'll try this. You want this to happen:

    Your code will fire when a player right-clicks the air or a block, has the right permissions, is holding soup, and has under 20 health.


    But, the way you have coded it, this is what happens:

    Your code will fire on two scenarios:

    1.) A player right-clicks the air

    OR

    2.) A player right-clicks a block, has the right permissions, is holding soup, and has under 20 health.


    Your code will fire when any player right-clicks the air. Not just when they right-click a block, have the right permissions, are holding soup, and have under 20 health, but also when they only right click the air. Do you understand what's happening now?
     
  8. Sorry for the hassle, I already fixed it a long time ago :p
     
Thread Status:
Not open for further replies.

Share This Page