食物营养计算公式

基本信息

属性 值
触发类 ca.wescook.nutrition.nutrients.NutrientUtils
调用方 EventEatFood.onFoodEaten(吃饭) / EventTooltip.tooltipEvent(显示 tooltip) / EventWorldTick.handleNonFoodHungerChanges(非食物饥饿变化)
数据源 AppleCore FoodValues(含 hunger 字段)

功能

把 AppleCore 报告的"食物饥饿值(hunger)"按 Nutrient 配置转换为每个 Nutrient 应获得的营养值。

主公式

public static float getNutrientValue(int hungerValue, int numNutrients) {
    float adjustedFoodValue = (float) (hungerValue * 0.5);                          // 半饥饿基础值
    adjustedFoodValue = adjustedFoodValue * NutritionConfig.nutrition.nutritionMultiplier;  // 全局倍率
    float lossPercentage = (float) NutritionConfig.nutrition.lossPerNutrient / 100;  // 多 Nutrient 损失率
    float foodLoss = (adjustedFoodValue * lossPercentage * (numNutrients - 1));     // 损失量
    return Math.max(0, adjustedFoodValue - foodLoss);                               // 不低于 0
}

公式拆解

base = hunger × 0.5
scaled = base × nutritionMultiplier
foodLoss = scaled × (lossPerNutrient / 100) × (numNutrients - 1)
result = max(0, scaled - foodLoss)

数值示例(默认配置 nutritionMultiplier=1.0, lossPerNutrient=15)

食物 hunger 属于 Nutrient 数 scaled foodLoss result
苹果(hunger=4,1 Nutrient) 4 1 2.0 2.0 × 0.15 × 0 = 0 2.0
蛋糕(hunger=14,3 Nutrient) 14 3 7.0 7.0 × 0.15 × 2 = 2.1 4.9
熟牛肉(hunger=8,1 Nutrient) 8 1 4.0 0 4.0
南瓜派(hunger=8,2 Nutrient) 8 2 4.0 4.0 × 0.15 × 1 = 0.6 3.4

调用链

1. EventEatFood.onFoodEaten

List<Nutrient> foundNutrients = NutrientUtils.getFoodNutrients(event.food);
float nutritionValue = NutrientUtils.calculateNutrition(event.foodValues, foundNutrients);
NutritionManager.instance().addAll(event.player, foundNutrients, nutritionValue);
  • getFoodNutrients 扫描所有 Nutrient 的 foodItems(ItemStack equal)+ foodOreDict(OreDictionary)匹配
  • calculateNutrition = getNutrientValue(foodValues.hunger, nutrients.size())
  • addAll 把同一数值一次性累加到所有匹配的 Nutrient

2. EventTooltip.tooltipEvent

tooltip 显示时调用:

FoodValues foodValues = AppleCoreAPI.accessor.getFoodValuesForPlayer(itemStack, event.entityPlayer);
nutritionValue = NutrientUtils.calculateNutrition(foodValues, foundNutrients);

用玩家个体 FoodValues(受 AppleCore 食物修饰影响)来预览数值。

3. EventWorldTick.handleNonFoodHungerChanges

针对直接修改饥饿(非吃饭)的场景,按"全部 Nutrient"维度 normalize 到 50:

float amountToChange = NutrientUtils.getNutrientValue(hungerChange, NutrientList.get().size());
NutritionManager.instance().normalize(player, 50.0f, amountToChange);

相关条目