AppleCore

AppleCore

56M Downloads

FoodStats.addStats does not get saturation from the FoodValues getters

primetoxinz opened this issue ยท 2 comments

commented

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.

commented

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 the FoodStats fields yourself (essentially reimplement/override FoodStats.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);
}
commented

Yeah, you're right. I overlooked that this was the int,float version. Thought it was still the itemstack one, my bad.
The event will do.
Thanks :)