1.8.8 Always says "health is full"

Discussion in 'Spigot Plugin Development' started by wastelander9, Feb 27, 2020.

  1. I tried to use more than way to fix this but it always says health full while my health isn't full
    (btw its a souppvp code)
    Code (Java):
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            if ((e.getMaterial().getId() == 282) && ((e.getAction().equals(Action.RIGHT_CLICK_AIR))  || (e.getAction().equals(Action.LEFT_CLICK_AIR))  || (e.getAction().equals(Action.LEFT_CLICK_BLOCK)) || (e.getAction().equals(Action.RIGHT_CLICK_BLOCK))) && (p.getHealth() <= 20) || (p.getFoodLevel() <= 20)) {
              p.sendMessage(ChatColor.GREEN + "Your health is full!");
              return;
            }
            if ((e.getMaterial().getId() == 282) && ((e.getAction().equals(Action.RIGHT_CLICK_AIR))  || (e.getAction().equals(Action.RIGHT_CLICK_BLOCK))) && ((p.getHealth() != 20.0 || (p.getFoodLevel() != 20)))) {
            if(p.getItemInHand().getType() == Material.MUSHROOM_SOUP) {
                p.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 2*1, 0));
                p.getInventory().setItemInHand(new ItemStack(Material.valueOf("BOWL")));
                p.updateInventory();

            }
        }
    edit: Just found another error, this applys to all items pls fix
     
    #1 wastelander9, Feb 27, 2020
    Last edited by a moderator: Feb 27, 2020
  2. Clean up your code so we may actually be able to read it without doing it on our own. In your first if (after a ton of questionable conditions) you check if the players health or food level is lower than or equal to 20 and if so you say that the players health is full and return which is probably your problem.
     
  3. try to replace "(p.getHealth() <= 20) || (p.getFoodLevel() <= 20)" with "(p.getHealth() == 20) || (p.getFoodLevel() == 20)"?
     
  4. You've gotten confused with < and >

    Wouldn't you only want the health to state its full when its directly equal to 20? Not more or less than?
     
  5. didn't work
     
  6. Choco

    Moderator

    Please never do this
    • Create some variables. You're making your code unreadable. event.getMaterial() and event.getAction() should both be variables because you get them more than once
    • Compare constants (Material & Action are both constants) with ==, not .equals()
    • Your PotionEffect should be constant
      • And 2 * 1 is still 2. Remove the multiplication
    • The call to player.updateInventory() is almost always unnecessary, this case included
    The root of your issue is likely because of order of operations and because you're considering all values <= 20 (from full to empty) when telling the player that their health or hunger is full. You should be checking only their max health and max hunger.

    Also, I've edited your original post to surround the code with [CODE=java][/CODE] tags. Please do this in the future.
     
  7. Code (Text):
    @EventHandler
    public void onInteract(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if (e.getMaterial() == Material.MUSHROOM_STEW  && (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) && ((p.getHealth() == 20) || (p.getFoodLevel() == 20))) {
            p.sendMessage(ChatColor.GREEN + "Your health is full!");
            return;
        } else {
            //
            //do effects here?
            //
        }
    }
    maybe something like this?
     
    #7 aufgosh, Feb 27, 2020
    Last edited by a moderator: Feb 27, 2020
  8. didn't work
     
  9. 1. What's even the point of checking for the action in this scenario? You are covering all possible outcomes anyways.

    2. Depending on the server this will be used on i'd recommend to actually get the players max health attribute instead of just assuming it's fixed 20.
     
  10. Spend more time learning Java and the Spigot API.
    I don't wanna be 'that guy' and I very rarely state this.

    Some of these things you've done are very, very weird and shouldn't be normal practise if you had a general idea of what you should be doing.
     
  11. Choco

    Moderator

    I'm not certain 1.8 had an attribute for maximum health. Player#getMaximumHealth() was the solution instead. That's what happens when we use outdated software ¯\_(ツ)_/¯
     
    • Agree Agree x 1
  12. Code (Text):
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if (e.getMaterial().getId() == 282  &&  ((p.getHealth() == 20) || (p.getFoodLevel() == 20))) {
        p.sendMessage(ChatColor.GREEN + "Your health is full!");
        return;
        } else {
            double a = p.getHealth() + 4;
            int b = p.getFoodLevel() + 4;
            p.setHealth(a > p.getMaxHealth() ? p.getMaxHealth() : a);
            p.setFoodLevel(b > p.getFoodLevel() ? p.getFoodLevel() : b);
            //
            //do effects here?
            //
        }
        }
    this must work