Food level not setting correctly

Discussion in 'Plugin Development' started by jameskmonger, Jun 22, 2014.

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

    jameskmonger

    Hi guys. I have this code:
    Code:java
    1. @EventHandler
    2. public void ScaledHungerDepletion(FoodLevelChangeEvent e) {
    3. Player p = (Player) e.getEntity();
    4. p.sendMessage("before: " + p.getFoodLevel());
    5.  
    6. int before = p.getFoodLevel();
    7. p.setFoodLevel(before - 4);
    8.  
    9. p.sendMessage("after: " + p.getFoodLevel());
    10. }


    And the output I should expect would be something like:

    But instead, I'm getting this output, so it appears that the food level is not setting permanently:

    I have no other plugins on the server.
     
  2. Offline

    xTigerRebornx

    jameskmonger Try cancelling the event, you set it in there, but since the event is never canceled, it will still run the code that it uses to change the food (I think :p)
    or
    Use the event's setFoodLevel() method
     
  3. Offline

    jameskmonger

    Great. Cancelling the event worked fine. Do you know how I can work out whether or not the net change in food levels is positive or negative? I don't want the effect to happen when a player eats, for obvious reasons :p

    xTigerRebornx Do you know how I can work out whether or not the net change in food levels is positive or negative? I don't want the effect to happen when a player eats, for obvious reasons

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

    AoH_Ruthless

    jameskmonger
    Don't bump more than once every 24 hours.

    Check if the FoodLevelChangeEvent#getFoodLevel() is higher than the Player#getFoodLevel(), and if so, return.
     
Thread Status:
Not open for further replies.

Share This Page