Fixed mana regeneration and energy consumption and fixed mana not being loaded correctly between saves

This commit is contained in:
Sollace 2022-09-16 11:15:07 +02:00
parent 89879494df
commit 17233f425a
4 changed files with 57 additions and 17 deletions

View file

@ -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() {

View file

@ -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;

View file

@ -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<Float> 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"));
}
}
}

View file

@ -527,16 +527,14 @@ public class Pony extends Living<PlayerEntity> 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<PlayerEntity> 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");