Hunger Depleting Help

Discussion in 'Plugin Development' started by The Fancy Whale, Jul 21, 2014.

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

    The Fancy Whale

    I'm working on a plugin that needs hunger to be lost at half speed. I am trying to accomplish this through using a configuration that stores a fake food level. The fake food level is out of 40 so that they lost it twice as slowly. For some reason I don't seem to lose hunger though. Here is the code (When I should lose hunger I get no numbers and nothing happens. When I gain/eat food the chat says 1, and 7):
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent event){
    3. if (this.getConfig().contains(event.getPlayer().getUniqueId().toString()))return;
    4. this.getConfig().set(event.getPlayer().getUniqueId().toString(), 40);
    5. this.saveConfig();
    6. }
    7.  
    8.  
    9.  
    10. @EventHandler
    11. public void onFood(FoodLevelChangeEvent event){
    12. if (!(event.getEntity() instanceof Player)) return;
    13.  
    14. Player player = (Player) event.getEntity();
    15. player.sendMessage("1");
    16. int oldFoodLevel = player.getFoodLevel();
    17. int newFoodLevel = event.getFoodLevel();
    18. if (oldFoodLevel > newFoodLevel){
    19. player.sendMessage("2");
    20.  
    21. if ( (this.getConfig().getInt(player.getUniqueId().toString()) % 2) == 0 ) {
    22. player.sendMessage("3");
    23. int a = this.getConfig().getInt(player.getUniqueId().toString()) - 1;
    24. this.getConfig().set(player.getUniqueId().toString(), a);
    25. this.saveConfig();
    26. player.setFoodLevel(this.getConfig().getInt(player.getUniqueId().toString()) % 2);
    27. }
    28. else {
    29. player.sendMessage("4");
    30. if (this.getConfig().getInt( player.getUniqueId().toString()) <= 1){
    31. player.sendMessage("5");
    32. player.setFoodLevel(0);
    33. this.getConfig().set(player.getUniqueId().toString(), 0);
    34. this.saveConfig();
    35. }
    36. else{
    37. player.sendMessage("6");
    38. player.setFoodLevel(this.getConfig().getInt(player.getUniqueId().toString()) % 2);
    39. int a = this.getConfig().getInt(player.getUniqueId().toString()) - 1;
    40. this.getConfig().set(player.getUniqueId().toString(), a);
    41. this.saveConfig();
    42. }
    43. }
    44. }
    45. else {
    46. player.sendMessage("7");
    47. player.setFoodLevel(newFoodLevel);
    48. int a = newFoodLevel * 2;
    49. this.getConfig().set(player.getUniqueId().toString(), a);
    50. }
    51. }

    Any ideas how to get 2-6 to trigger?
     
  2. Offline

    Zettelkasten

    I would just cancel every second event for each player (you will need to filter out when a player eats).
     
  3. Offline

    mythbusterma

    The Fancy Whale

    What is ("" + player.getUniqueId())? Perhaps you should go back and review some Java.

    Other than this, why not cancel every other FoodLevelChangeEvent? Wouldn't that make orders of magnitude more sense?
     
  4. Offline

    hintss

    ("" + player.getUniqueId()) is ok, I wouldn't do it myself, but using the implicit toString() might look nicer to some people, and it makes little difference.
     
  5. Offline

    mythbusterma

    hintss

    Pretty sure the explicit toString() is what everyone and their mother would recommend due to readability, this way is confusing and frankly silly, and I see way too much of it here.
     
  6. Offline

    stormneo7

    This may not be half the speed but it is my original way of nerfing hunger.

    Code:java
    1. @EventHandler
    2. public void onHunger(FoodLevelChangeEvent evt){
    3. if(evt.getFoodLevel() < ((Player)evt.getEntity()).getFoodLevel()){
    4. if(new Random().nextInt(2) == 0)
    5. evt.setCancelled(true);
    6. }
    7. }


    It might interest you too... turns your 40 lines of code down to 6.

    What it basically does is whenever the player loses hunger, it'll generate a random number between 0 and 1. If that number is 0, the event is cancelled.
     
  7. Offline

    The Fancy Whale

    But that way is also random.
    mythbusterma Personally, don't care too much about how pretty the code looks. More about getting the job done. I'd prefer help with the actual problem if that's okay with you.

    I'll try doing that. And I'll just use the same oldFood and newFood filer to check if food is eaten.
     
Thread Status:
Not open for further replies.

Share This Page