From 17233f425a6f7e853f1d2045dfe9e2ccbfb3b9b2 Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 16 Sep 2022 11:15:07 +0200 Subject: [PATCH] Fixed mana regeneration and energy consumption and fixed mana not being loaded correctly between saves --- .../unicopia/ability/magic/Levelled.java | 2 +- .../magic/spell/effect/ShieldSpell.java | 4 +- .../unicopia/entity/player/ManaContainer.java | 62 ++++++++++++++++--- .../unicopia/entity/player/Pony.java | 6 +- 4 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/Levelled.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/Levelled.java index 2af23627..9bc914b3 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/Levelled.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/Levelled.java @@ -35,7 +35,7 @@ public interface Levelled { void set(int level); default float getScaled(float max) { - return (1 + ((float)get() / getMax())) * max; + return ((float)get() / getMax()) * max; } default boolean canLevelUp() { diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/ShieldSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/ShieldSpell.java index 806ce8e7..1296f3c2 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/ShieldSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/ShieldSpell.java @@ -102,8 +102,8 @@ public class ShieldSpell extends AbstractSpell { double cost = 2 - source.getLevel().getScaled(2); cost *= costMultiplier / ((1 + source.getLevel().get()) * 3F); - cost /= 2.725D; cost /= knowledge; + cost += getDrawDropOffRange(source) / 10F; if (!source.subtractEnergyCost(cost)) { setDead(); @@ -120,7 +120,7 @@ public class ShieldSpell extends AbstractSpell { float multiplier = source instanceof Pony pony && pony.getMaster().isSneaking() ? 1 : 2; float min = 4 + getTraits().get(Trait.POWER); double range = (min + (source.getLevel().getScaled(4) * 2)) / multiplier; - if (source instanceof Pony && range > 2) { + if (source instanceof Pony && range >= 4) { range = Math.sqrt(range); } return range; diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/ManaContainer.java b/src/main/java/com/minelittlepony/unicopia/entity/player/ManaContainer.java index 7e7044c3..40c80e09 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/ManaContainer.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/ManaContainer.java @@ -1,11 +1,13 @@ package com.minelittlepony.unicopia.entity.player; +import com.minelittlepony.unicopia.util.NbtSerialisable; import com.minelittlepony.unicopia.util.Tickable; import net.minecraft.entity.data.TrackedData; +import net.minecraft.nbt.NbtCompound; import net.minecraft.util.math.MathHelper; -public class ManaContainer implements MagicReserves, Tickable { +public class ManaContainer implements MagicReserves, Tickable, NbtSerialisable { private final Pony pony; private final BarInst energy; @@ -23,6 +25,24 @@ public class ManaContainer implements MagicReserves, Tickable { this.mana = new XpCollectingBar(Pony.MANA, 100F, 100F); } + @Override + public void toNBT(NbtCompound compound) { + compound.put("energy", energy.toNBT()); + compound.put("exhaustion", exhaustion.toNBT()); + compound.put("exertion", exertion.toNBT()); + compound.put("mana", mana.toNBT()); + compound.put("xp", xp.toNBT()); + } + + @Override + public void fromNBT(NbtCompound compound) { + energy.fromNBT(compound.getCompound("energy")); + exhaustion.fromNBT(compound.getCompound("exhaustion")); + exertion.fromNBT(compound.getCompound("exertion")); + mana.fromNBT(compound.getCompound("mana")); + xp.fromNBT(compound.getCompound("xp")); + } + @Override public Bar getExertion() { return exertion; @@ -70,8 +90,8 @@ public class ManaContainer implements MagicReserves, Tickable { } if (!pony.getSpecies().canFly() || !pony.getPhysics().isFlying()) { - if (mana.getShadowFill() <= mana.getPercentFill()) { - mana.add(18 * pony.getLevel().get()); + if (mana.getPercentFill() < 1 && mana.getShadowFill() == mana.getPercentFill()) { + mana.add((mana.getMax() / 10F) * Math.max(1, pony.getLevel().get() * 4)); } } } @@ -102,11 +122,13 @@ public class ManaContainer implements MagicReserves, Tickable { value = get() + diff / (1 + pony.getLevel().get()); } + System.out.println("Setting mana to: " + value); super.set(value); + System.out.println("Mana set to: " + get()); } } - class BarInst implements Bar { + class BarInst implements Bar, NbtSerialisable { private final TrackedData marker; private final float max; @@ -121,7 +143,11 @@ public class ManaContainer implements MagicReserves, Tickable { @Override public float get() { - return pony.getMaster().getDataTracker().get(marker); + float value = pony.getMaster().getDataTracker().get(marker); + if (this == mana) { + System.out.println("Mana is: " + value); + } + return value; } @Override @@ -131,7 +157,11 @@ public class ManaContainer implements MagicReserves, Tickable { @Override public void set(float value) { - pony.getMaster().getDataTracker().set(marker, MathHelper.clamp(value, 0, getMax())); + load(MathHelper.clamp(value, 0, getMax())); + } + + private void load(float value) { + pony.getMaster().getDataTracker().set(marker, value); } @Override @@ -141,17 +171,29 @@ public class ManaContainer implements MagicReserves, Tickable { void tick() { float fill = getPercentFill(); - float tralingIncrement = 0.003F; + float trailingIncrement = 0.003F; - if (trailingValue > (fill - tralingIncrement) && trailingValue < (fill + tralingIncrement)) { + if (trailingValue > (fill - trailingIncrement) && trailingValue < (fill + trailingIncrement)) { trailingValue = fill; } if (trailingValue < fill) { - trailingValue += tralingIncrement; + trailingValue += trailingIncrement; } if (trailingValue > fill) { - trailingValue -= tralingIncrement; + trailingValue -= trailingIncrement; } } + + @Override + public void toNBT(NbtCompound compound) { + compound.putFloat("shadow", trailingValue); + compound.putFloat("value", get()); + } + + @Override + public void fromNBT(NbtCompound compound) { + trailingValue = compound.getFloat("shadow"); + load(compound.getFloat("value")); + } } } 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 e2fa279c..02b4f481 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java @@ -527,16 +527,14 @@ public class Pony extends Living implements Transmittable, Copieab public void toNBT(NbtCompound compound) { super.toNBT(compound); compound.putString("playerSpecies", Race.REGISTRY.getId(getSpecies()).toString()); - compound.putFloat("magicExhaustion", magicExhaustion); - compound.put("powers", powers.toNBT()); compound.put("gravity", gravity.toNBT()); compound.put("charms", charms.toNBT()); compound.put("discoveries", discoveries.toNBT()); + compound.put("mana", mana.toNBT()); compound.putInt("levels", levels.get()); compound.putInt("corruption", corruption.get()); - compound.putFloat("magicXp", mana.getXp().get()); getSpellSlot().get(true).ifPresent(effect ->{ compound.put("effect", Spell.writeNbt(effect)); @@ -561,7 +559,7 @@ public class Pony extends Living implements Transmittable, Copieab discoveries.fromNBT(compound.getCompound("discoveries")); levels.set(compound.getInt("levels")); corruption.set(compound.getInt("corruption")); - mana.getXp().set(compound.getFloat("magicXp")); + mana.fromNBT(compound.getCompound("mana")); magicExhaustion = compound.getFloat("magicExhaustion");