Making hunger degenerate faster

Discussion in 'Plugin Development' started by Appljuze, Nov 13, 2012.

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

    Appljuze

    I'm wondering how I would go about making hunger degenerate faster.. Is there a method to set the hunger degeneration rate? Or would I have to manually make the server decrease a player's hunger by X amount? I'm pretty stumped on how to do this...
     
  2. Offline

    kabbage

    Code:
    @EventHandler
        public void onFoodLevelChange(FoodLevelChangeEvent event)
        {
            if(event.isCancelled())
                return;
            event.setFoodLevel(event.getFoodLevel() - 1);
        }
    This would double the rate of hunger loss. Play around with it to speed up/slow down the rate from there.
     
    Appljuze likes this.
  3. Offline

    Eballer48

    I can help ya, just tell me when should the player's hunger degenerate faster when they doing something specific or just in general?
     
  4. Offline

    Appljuze

    I'd like their hunger to degenerate only when they run (double tap), and then gradually recover. I was also wondering how you would make it increase over time... would I use a while loop, then sleep for X amount of seconds, then restore hunger and repeat? Thanks for your help, I appreciate it.
     
  5. Offline

    Sagacious_Zed Bukkit Docs

    Do not use a while loop and sleep the tread, because when you sleep the tread, you sleep the server.

    All code that needs to be executed over time should be scheduled with the bukkit scheduler.
     
  6. Offline

    Terradominik

    you could do
    Code:
    @EventHandler
        public void onFoodLevelChange(FoodLevelChangeEvent event)
        {
            if(event.isCancelled()) return;
            if(((Player) event.getEntity()).isSprinting()) event.setFoodLevel(event.getFoodLevel() - 1);
            else event.setFoodLevel(event.getFoodLevel() + 1);
        }
     
  7. Offline

    Quackster


    You might want to check if it's a player first.

    Code:
    if (!(event.getEntityType() == EntityType.PLAYER)) {
    return;
    }
     
  8. Offline

    Bart

    I might be wrong but I think that getEntity will always return a player if it is used in the FoodLevelChangeEvent.. I don't think mobs have food levels.

    But yeah, best practice.
     
Thread Status:
Not open for further replies.

Share This Page