From 5680e60464e63adf8bdec01795afed976579f291 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 8 Oct 2022 14:46:07 +0200 Subject: [PATCH] Rewrite mana/food/health consumption to be more forgiving --- .../entity/player/ManaConsumptionUtil.java | 82 +++++++++++++++++++ .../unicopia/entity/player/Pony.java | 31 +------ 2 files changed, 84 insertions(+), 29 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/entity/player/ManaConsumptionUtil.java diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/ManaConsumptionUtil.java b/src/main/java/com/minelittlepony/unicopia/entity/player/ManaConsumptionUtil.java new file mode 100644 index 00000000..af2b067d --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/ManaConsumptionUtil.java @@ -0,0 +1,82 @@ +package com.minelittlepony.unicopia.entity.player; + +import com.minelittlepony.unicopia.util.MagicalDamageSource; + +import net.minecraft.entity.player.HungerManager; +import net.minecraft.entity.player.PlayerEntity; + +public interface ManaConsumptionUtil { + float FOOD_PER_MANA = 0.1F; + float HEARTS_PER_FOOD = 0.5F; + float SATURATION_PER_FOOD = 0.8F; + + static float consumeMana(MagicReserves.Bar mana, double foodSubtract) { + + if (foodSubtract <= 0) { + return 0; + } + + float availableMana = mana.get(); + float consumedMana = (float)foodSubtract / FOOD_PER_MANA; + + if (consumedMana <= availableMana) { + mana.set(availableMana - consumedMana); + return 0; + } + + mana.set(0); + return (float)foodSubtract - (availableMana * FOOD_PER_MANA); + } + + static float burnFood(PlayerEntity entity, float foodSubtract) { + HungerManager hunger = entity.getHungerManager(); + + if (foodSubtract <= 0) { + return 0; + } + + float availableSaturation = hunger.getSaturationLevel(); + + if (availableSaturation > 0 && foodSubtract > 0) { + float consumedSaturation = Math.min(foodSubtract * SATURATION_PER_FOOD, availableSaturation); + foodSubtract = addExhaustion(hunger, foodSubtract); + if (consumedSaturation > 0) { + foodSubtract -= (consumedSaturation / SATURATION_PER_FOOD); + hunger.setSaturationLevel(availableSaturation - consumedSaturation); + } + } + + int availableFood = hunger.getFoodLevel(); + if (availableFood > 0 && foodSubtract > 0) { + int consumedFood = Math.min((int)Math.floor(foodSubtract), availableFood); + foodSubtract = addExhaustion(hunger, foodSubtract); + if (consumedFood > 0) { + foodSubtract -= consumedFood; + hunger.add(-consumedFood, 0.3F); + } + } + + float availableHearts = entity.getHealth(); + if (foodSubtract > 0) { + float consumedHearts = Math.max(0.001F, Math.min(availableHearts - 1, foodSubtract * HEARTS_PER_FOOD)); + foodSubtract = addExhaustion(hunger, foodSubtract); + foodSubtract -= (consumedHearts / HEARTS_PER_FOOD); + entity.damage(MagicalDamageSource.EXHAUSTION, consumedHearts); + } + + return Math.max(0, foodSubtract); + } + + static float addExhaustion(HungerManager hunger, float foodSubtract) { + hunger.addExhaustion(0.1F); + return Math.max(0, foodSubtract - 0.1F); + } + + interface FloatSupplier { + float get(); + } + + interface FloatConsumer { + void accept(float f); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java index 1bba15e7..a6e46aa6 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java @@ -330,7 +330,7 @@ public class Pony extends Living implements Transmittable, Copieab } } - magicExhaustion = burnFood(magicExhaustion); + magicExhaustion = ManaConsumptionUtil.burnFood(entity, magicExhaustion); powers.tick(); @@ -502,37 +502,10 @@ public class Pony extends Living implements Transmittable, Copieab protected void directTakeEnergy(double foodSubtract) { if (!entity.isCreative() && !entity.world.isClient) { - - float currentMana = mana.getMana().get(); - float foodManaRatio = 10; - - if (currentMana >= foodSubtract * foodManaRatio) { - mana.getMana().set(currentMana - (float)foodSubtract * foodManaRatio); - } else { - mana.getMana().set(0); - foodSubtract -= currentMana / foodManaRatio; - - magicExhaustion += foodSubtract; - } + magicExhaustion += ManaConsumptionUtil.consumeMana(mana.getMana(), foodSubtract); } } - private float burnFood(float foodSubtract) { - int lostLevels = (int)Math.floor(foodSubtract); - if (lostLevels > 0) { - int food = entity.getHungerManager().getFoodLevel() - lostLevels; - - if (food < 0) { - entity.getHungerManager().add(-entity.getHungerManager().getFoodLevel(), 0); - entity.damage(MagicalDamageSource.EXHAUSTION, -food/2); - } else { - entity.getHungerManager().add(-lostLevels, 0); - } - } - - return foodSubtract - lostLevels; - } - @Override protected Stream getInventoryStacks() { return Streams.concat(