Fixed incorrect percentage breakdown for pony diets in tooltips

This commit is contained in:
Sollace 2024-02-21 23:21:12 +00:00
parent 5acebc270c
commit a5132859ab
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
3 changed files with 21 additions and 4 deletions

View file

@ -10,6 +10,7 @@ import java.util.stream.Collectors;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.item.ItemDuck; import com.minelittlepony.unicopia.item.ItemDuck;
import com.mojang.datafixers.util.Pair; import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec; import com.mojang.serialization.Codec;
@ -104,6 +105,7 @@ public record DietProfile(
public void appendTooltip(ItemStack stack, @Nullable PlayerEntity user, List<Text> tooltip, TooltipContext context) { public void appendTooltip(ItemStack stack, @Nullable PlayerEntity user, List<Text> tooltip, TooltipContext context) {
var food = stack.getItem().getFoodComponent(); var food = stack.getItem().getFoodComponent();
var ratios = getRatios(stack); var ratios = getRatios(stack);
if (food == null || isInedible(ratios)) { if (food == null || isInedible(ratios)) {
if (stack.getUseAction() != UseAction.DRINK) { if (stack.getUseAction() != UseAction.DRINK) {
@ -115,15 +117,30 @@ public record DietProfile(
float baseMultiplier = (isForaged(stack) ? foragingMultiplier() : defaultMultiplier()); float baseMultiplier = (isForaged(stack) ? foragingMultiplier() : defaultMultiplier());
if (context.isAdvanced()) { if (context.isAdvanced()) {
var nonAdjustedFood = getNonAdjustedFoodComponent(stack, user).orElse(food);
tooltip.add(Text.literal(" ").append(Text.translatable("unicopia.diet.base_multiplier", baseMultiplier).formatted(Formatting.DARK_GRAY))); tooltip.add(Text.literal(" ").append(Text.translatable("unicopia.diet.base_multiplier", baseMultiplier).formatted(Formatting.DARK_GRAY)));
tooltip.add(Text.literal(" ").append(Text.translatable("unicopia.diet.hunger.detailed", Math.max(1, (int)(ratios.getFirst() * food.getHunger())), food.getHunger(), (int)(ratios.getFirst() * 100))).formatted(Formatting.DARK_GRAY)); tooltip.add(Text.literal(" ").append(Text.translatable("unicopia.diet.hunger.detailed", food.getHunger(), nonAdjustedFood.getHunger(), (int)(ratios.getFirst() * 100))).formatted(Formatting.DARK_GRAY));
tooltip.add(Text.literal(" ").append(Text.translatable("unicopia.diet.saturation.detailed", String.format("%.2f", ratios.getSecond() * food.getSaturationModifier()), (int)(ratios.getSecond() * 100))).formatted(Formatting.DARK_GRAY)); tooltip.add(Text.literal(" ").append(Text.translatable("unicopia.diet.saturation.detailed", food.getSaturationModifier(), nonAdjustedFood.getSaturationModifier(), (int)(ratios.getSecond() * 100))).formatted(Formatting.DARK_GRAY));
} else { } else {
tooltip.add(Text.literal(" ").append(Text.translatable("unicopia.diet.hunger", (int)(ratios.getFirst() * 100))).formatted(Formatting.DARK_GRAY)); tooltip.add(Text.literal(" ").append(Text.translatable("unicopia.diet.hunger", (int)(ratios.getFirst() * 100))).formatted(Formatting.DARK_GRAY));
tooltip.add(Text.literal(" ").append(Text.translatable("unicopia.diet.saturation", (int)(ratios.getSecond() * 100))).formatted(Formatting.DARK_GRAY)); tooltip.add(Text.literal(" ").append(Text.translatable("unicopia.diet.saturation", (int)(ratios.getSecond() * 100))).formatted(Formatting.DARK_GRAY));
} }
} }
private Optional<FoodComponent> getNonAdjustedFoodComponent(ItemStack stack, @Nullable PlayerEntity user) {
@Nullable
Pony pony = Pony.of(user);
Optional<FoodComponent> food = ((ItemDuck)stack.getItem()).getOriginalFoodComponent();
if (food.isEmpty() && pony.getObservedSpecies().hasIronGut()) {
return findEffect(stack)
.flatMap(Effect::foodComponent)
.or(() -> PonyDiets.getInstance().getEffects(stack).foodComponent());
}
return food;
}
public record Multiplier( public record Multiplier(
Set<TagKey<Item>> tags, Set<TagKey<Item>> tags,
float hunger, float hunger,

View file

@ -53,7 +53,7 @@ public class PonyDiets implements DietView {
return Optional.ofNullable(diets.get(pony.getObservedSpecies())).orElse(DietProfile.EMPTY); return Optional.ofNullable(diets.get(pony.getObservedSpecies())).orElse(DietProfile.EMPTY);
} }
private Effect getEffects(ItemStack stack) { Effect getEffects(ItemStack stack) {
return effects.stream().filter(effect -> effect.test(stack)).findFirst().orElse(Effect.EMPTY); return effects.stream().filter(effect -> effect.test(stack)).findFirst().orElse(Effect.EMPTY);
} }

View file

@ -594,7 +594,7 @@
"unicopia.diet.not_edible": "Item is not edible", "unicopia.diet.not_edible": "Item is not edible",
"unicopia.diet.base_multiplier": "Base Multiplier: %s%%", "unicopia.diet.base_multiplier": "Base Multiplier: %s%%",
"unicopia.diet.hunger.detailed": "Hunger gained: %s of %s (%s%%)", "unicopia.diet.hunger.detailed": "Hunger gained: %s of %s (%s%%)",
"unicopia.diet.saturation.detailed": "Saturation gained: %s (%s%%)", "unicopia.diet.saturation.detailed": "Saturation gained: %s of %s (%s%%)",
"unicopia.diet.hunger": "Hunger Ratio: %s%%", "unicopia.diet.hunger": "Hunger Ratio: %s%%",
"unicopia.diet.saturation": "Saturation Ratio: %s%%", "unicopia.diet.saturation": "Saturation Ratio: %s%%",