FoodStats.addStats does not get saturation from the FoodValues getters
primetoxinz opened this issue ยท 2 comments
I was attempting to override how saturation values are retrieved for a HCHunger bug and noticed that at this line, the additional saturation value is calculated from the FoodValues field rather than the getSaturationIncrement
method.
By the time the int, float
version of addStats
is called, we no longer have a reference to the FoodValues instance, so there's not really a getSaturationIncrement
function that can be called (except on a new instance of FoodValues, but that'd bypass any customization unless I'm thinking about this wrong).
I see two possibilities:
- What is currently possible is listening for
FoodEvent.FoodStatsAddition
, canceling it, and then modifying theFoodStats
fields yourself (essentially reimplement/overrideFoodStats.addStats
) - I could add a new event specifically for this purpose, but given the above should work that might not be necessary.
Untested example code:
@SubscribeEvent
public void onFoodStatsAddition(FoodEvent.FoodStatsAddition event)
{
// cancel the default
event.setCanceled(true);
int maxHunger = AppleCoreAPI.accessor.getMaxHunger(event.player);
int newHunger = Math.min(event.player.getFoodStats().getFoodLevel() + event.foodValuesToBeAdded.hunger, maxHunger);
event.player.getFoodStats().setFoodLevel(newHunger);
float saturationIncrement = event.foodValuesToBeAdded.hunger * event.foodValuesToBeAdded.saturationModifier * 2f;
float newSaturation = Math.min(event.player.getFoodStats().getSaturationLevel() + saturationIncrement, newHunger);
AppleCoreAPI.mutator.setSaturation(event.player, newSaturation);
}