squeek502 / AppleCore

An API for modifying the food and hunger mechanics of Minecraft
The Unlicense
55 stars 24 forks source link

FoodStats.addStats does not get saturation from the FoodValues getters #121

Closed primetoxinz closed 6 years ago

primetoxinz commented 6 years ago

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.

squeek502 commented 6 years ago

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:

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);
}
primetoxinz commented 6 years ago

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 :)