Regenerating Hunger?

Discussion in 'Plugin Development' started by spiroulis, Jan 29, 2014.

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


    Hey so i tried making a regerating hunger bar but i failed hard. I used FoodLevelChangeEvent and i did this so far but i have no idea on how to make it regenerate one by one. Heres my code:
    1. @EventHandler
    2. public void Hunger(FoodLevelChangeEvent event){
    3. if(event.getEntity() instanceof Player){
    4. Entity player = event.getEntity();
    5. int cfl = ((Player) player).getFoodLevel();
    6. if(cfl <= 20){
  2. Offline


    What do you exactly want, do you want it to have the bar regenerate on a certain event, command? Please provide more information :)
  3. Offline


    wouterrr i dont want it to regenerate on a command/event i want it to regenerate on itself once it falls down. You see im making an rpg plugin and when the players cast spells their hunger falls down. But the point is i wanna know how to regenerate hunger one by one. I hope you can understand what i mean, i aint the best at explaining :)
  4. Offline


    You first need to regenerate their exhaustion or whatever it's called, then their food level
  5. Offline


    Okay I'll explain it in pseudo code:

    You use: FoodLevelChangeEvent
    On that moment you create a runnable (repeating task) which fills the hungerbar with xhunger.
    Cancel the event on the moment the bar is full.

    Note this a foodlevelchangeevent is called on the moment the foodbar changes, so when the foodbar changes only a little bit it will regenerate, so it's not like a regeneration after the foodbar is only half full.
  6. Offline


    wouterrr i did this but it aint working take a look at my code :)
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void Hunger(FoodLevelChangeEvent event){
    4. if(event.getEntity() instanceof Player){
    5. final Entity player = event.getEntity();
    6. final int cfl = ((Player) player).getFoodLevel();
    7. if(cfl <= 20){
    8. this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
    9. public void run() {
    10. if(cfl <= 20){
    11. ((Player) player).setFoodLevel(cfl + 1);
    12. }
    13. }
    14. }, 40L, 40L);
    15. }
    16. }
    17. }
  7. Offline


    spiroulis Well I guess it does work actually but the problem is that on smallest change in the foodlevel (not visible for a player) this event will be called en it will be regenerated (so again invisible because its soo small).
  8. Offline


    wouterrr it actually works xd but i didnt notice it because it regenerates hunger only if you lose it from running, jumping etc. It does not regenerate after i use some spells that set the players hunger down. I used the player.setFoodLevel(player.getFoodLevel() - 4); at the spell.
  9. Offline


    There are no invisible food level changes, you can see all of them, just saying.
    I'm pretty sure when you directly change the player's hunger, it does not call the event, so instead of that, on that same spell when you set the player's food level, put the player's name into an arraylist, and work it from there.
  10. Offline


    and you did
    1. if (e instanceof Player)
    2. Entity player = event.getEntity();

    if you check if it is a player, you might as well assign it the Player variable
    1. if (e instanceof Player)
    2. Player player = (Player) event.getPlayer();
Thread Status:
Not open for further replies.

Share This Page