How to figure out a players max food level?

I’ve been trying to figure out how to figure out a players max food level. I can’t find it probably due to a lack of knowledge for the Sponge API (I’m Probably missing something really simple). I’m pretty sure the max for food level is 20, but I’d rather not use that in case in a future Minecraft version something changes. Plus having a variable for it shows what it represents in the code, rather than having to guess that 20 is the max food level.

I found how to get the max health (Keys.MAX_HEALTH) via Sponge docs, but I cannot find out how to get the max level for food. I’m making simple commands at the moment to figure out how the Sponge API works, and I’m pretty stumped right now. I’m using version 4.1.0 of Sponge on Minecraft 1.8.9. Any help or direction would be appreciated.

Additionally, if there are any other tips you can give in terms of figuring out the max level of other attributes that would be appreciated.

You can get the max value of a Value object like so:

Integer max = player.get(FoodData.class).get().foodLevel().getMaxValue();

According to that, the max is 2147483647 and using getDefault() returns 20

2 Likes

Wow, I knew I was missing something simple. Thank you. Side note if you happen to have time that is. From the API design perspective is there a reason why there is a Keys.MAX_HEALTH but not max values for other things? Such as the food level?

It’s because for some entities you can change the max health, but getMaxValue is only for getting, not setting. Generally, keys can be both get and set on applicable data holders.

Oh, I see. That makes sense. By the way how do you change the max health of an entity

By offering it the data. e.g.

entity.offer(Keys.MAX_HEALTH, 20D);
1 Like

Oh, duh. Thanks I thought Keys.MAX_HEALTH was the max health, and was immutable. Thank you