From 5f40b88fb4f4839603b5bdc999d48a8775316374 Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 16 Aug 2023 01:00:26 +0100 Subject: [PATCH] Fixed mana bar not being reset to full when respawning / pegasi having no mana after relogging --- .../unicopia/entity/player/ManaContainer.java | 35 +++++++++++++++---- .../unicopia/entity/player/Pony.java | 8 ++--- 2 files changed, 30 insertions(+), 13 deletions(-) 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 1ad7441f..9a22a1ae 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/ManaContainer.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/ManaContainer.java @@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.entity.player; import java.util.HashMap; import java.util.Map; +import com.minelittlepony.unicopia.util.Copyable; import com.minelittlepony.unicopia.util.NbtSerialisable; import com.minelittlepony.unicopia.util.Tickable; @@ -10,7 +11,7 @@ import net.minecraft.entity.data.TrackedData; import net.minecraft.nbt.NbtCompound; import net.minecraft.util.math.MathHelper; -public class ManaContainer implements MagicReserves, Tickable, NbtSerialisable { +public class ManaContainer implements MagicReserves, Tickable, NbtSerialisable, Copyable { private final Pony pony; private final Map bars = new HashMap<>(); @@ -27,8 +28,8 @@ public class ManaContainer implements MagicReserves, Tickable, NbtSerialisable { this.energy = addBar("energy", new BarInst(Pony.ENERGY, 100F, 0)); this.exhaustion = addBar("exhaustion", new BarInst(Pony.EXHAUSTION, 100F, 0)); this.exertion = addBar("exertion", new BarInst(Pony.EXERTION, 10F, 0)); - this.xp = addBar("xp", new BarInst(Pony.XP, 1, 0)); - this.mana = addBar("mana", new XpCollectingBar(Pony.MANA, 100F, 100F)); + this.xp = addBar("xp", new BarInst(Pony.XP, 1F, 0)); + this.mana = addBar("mana", new XpCollectingBar(Pony.MANA, 100F, 1)); this.charge = addBar("charge", new BarInst(Pony.CHARGE, 10F, 0) { @Override protected float applyLimits(float value) { @@ -112,6 +113,18 @@ public class ManaContainer implements MagicReserves, Tickable, NbtSerialisable { } } + @Override + public void copyFrom(ManaContainer other, boolean alive) { + if (alive) { + mana.resetTo(mana.getMax()); + xp.resetTo(other.xp.get()); + } else { + energy.resetTo(0.6F); + exhaustion.resetTo(0); + exertion.resetTo(0); + } + } + class XpCollectingBar extends BarInst { XpCollectingBar(TrackedData marker, float max, float initial) { @@ -148,7 +161,8 @@ public class ManaContainer implements MagicReserves, Tickable, NbtSerialisable { BarInst(TrackedData marker, float max, float initial) { this.marker = marker; this.max = max; - pony.asEntity().getDataTracker().startTracking(marker, initial); + this.trailingValue = initial; + pony.asEntity().getDataTracker().startTracking(marker, getMax() * initial); } @Override @@ -167,15 +181,22 @@ public class ManaContainer implements MagicReserves, Tickable, NbtSerialisable { } private void load(float value) { - if (!pony.isClient()) { - pony.asEntity().getDataTracker().set(marker, value); - } + pony.asEntity().getDataTracker().set(marker, value); + } + + protected float getInitial(float initial) { + return initial; } protected float applyLimits(float value) { return MathHelper.clamp(value, 0, getMax()); } + void resetTo(float value) { + trailingValue = MathHelper.clamp(value / getMax(), 0, 1); + load(value); + } + @Override public float getMax() { return max; 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 b0e614ee..ec534ebe 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Pony.java @@ -115,9 +115,9 @@ public class Pony extends Living implements Copyable, Update public Pony(PlayerEntity player) { super(player, EFFECT); - this.mana = addTicker(new ManaContainer(this)); this.levels = new PlayerLevelStore(this, LEVEL, true, SoundEvents.ENTITY_PLAYER_LEVELUP); this.corruption = new PlayerLevelStore(this, CORRUPTION, false, SoundEvents.PARTICLE_SOUL_ESCAPE); + this.mana = addTicker(new ManaContainer(this)); player.getDataTracker().startTracking(RACE, Race.DEFAULT_ID); player.getDataTracker().startTracking(HANGING_POSITION, Optional.empty()); @@ -863,13 +863,9 @@ public class Pony extends Living implements Copyable, Update getCharms().equipSpell(Hand.OFF_HAND, oldPlayer.getCharms().getEquippedSpell(Hand.OFF_HAND)); corruption.set(oldPlayer.getCorruption().get()); levels.set(oldPlayer.getLevel().get()); - mana.getXp().set(oldPlayer.getMagicalReserves().getXp().get()); - } else { - mana.getEnergy().set(0.6F); - mana.getExhaustion().set(0); - mana.getExertion().set(0); } + mana.copyFrom(oldPlayer.mana, !forcedSwap); advancementProgress.putAll(oldPlayer.getAdvancementProgress()); setDirty();