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 c3094400..2ea5d1e4 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/Levelled.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/Levelled.java @@ -1,33 +1,32 @@ package com.minelittlepony.unicopia.ability.magic; +import java.util.function.IntConsumer; import java.util.function.IntSupplier; import net.minecraft.nbt.NbtCompound; +import net.minecraft.util.math.MathHelper; /** * Object with levelling capabilities. */ public interface Levelled { - LevelStore EMPTY = fixed(0); + LevelStore ZERO = of(0, 1); - static LevelStore fixed(int level) { - return of(() -> level); - } - - static LevelStore of(IntSupplier supplier) { + static LevelStore of(IntSupplier getter, IntConsumer setter, IntSupplier max) { return new LevelStore() { @Override public int get() { - return supplier.getAsInt(); + return getter.getAsInt(); } @Override public void set(int level) { + setter.accept(level); } @Override public int getMax() { - return get(); + return max.getAsInt(); } }; } @@ -37,7 +36,9 @@ public interface Levelled { } static LevelStore fromNbt(NbtCompound compound) { - return of(compound.getInt("value"), compound.getInt("max")); + int max = Math.max(1, compound.getInt("max")); + int value = MathHelper.clamp(compound.getInt("value"), 0, max); + return of(value, max); } static LevelStore of(int level, int max) { @@ -70,6 +71,9 @@ public interface Levelled { void set(int level); default float getScaled(float max) { + if (getMax() == 0) { + return max; + } return ((float)get() / getMax()) * max; } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Creature.java b/src/main/java/com/minelittlepony/unicopia/entity/Creature.java index bd0929c6..eb7e27b9 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Creature.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Creature.java @@ -279,12 +279,12 @@ public class Creature extends Living implements WeaklyOwned.Mutabl @Override public LevelStore getLevel() { - return Levelled.EMPTY; + return Levelled.ZERO; } @Override public LevelStore getCorruption() { - return Levelled.EMPTY; + return Levelled.ZERO; } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java b/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java index c401c1ce..49fa886e 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java @@ -134,8 +134,8 @@ public class EntityReference implements NbtSerialisable { entity.getPos(), entity.getId(), entity instanceof PlayerEntity, !entity.isAlive(), - Caster.of(entity).map(Caster::getLevel).map(Levelled::copyOf).orElse(Levelled.EMPTY), - Caster.of(entity).map(Caster::getCorruption).map(Levelled::copyOf).orElse(Levelled.EMPTY) + Caster.of(entity).map(Caster::getLevel).map(Levelled::copyOf).orElse(Levelled.ZERO), + Caster.of(entity).map(Caster::getCorruption).map(Levelled::copyOf).orElse(Levelled.ZERO) ); } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/mob/CastSpellEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/mob/CastSpellEntity.java index b5ec2dd4..9c7312df 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/mob/CastSpellEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/mob/CastSpellEntity.java @@ -17,11 +17,18 @@ import net.minecraft.entity.EntityDimensions; import net.minecraft.entity.EntityPose; import net.minecraft.entity.EntityType; import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.data.DataTracker; +import net.minecraft.entity.data.TrackedData; +import net.minecraft.entity.data.TrackedDataHandlerRegistry; import net.minecraft.nbt.NbtCompound; import net.minecraft.text.Text; import net.minecraft.world.World; public class CastSpellEntity extends LightEmittingEntity implements Caster, WeaklyOwned.Mutable, MagicImmune { + private static final TrackedData LEVEL = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.INTEGER); + private static final TrackedData MAX_LEVEL = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.INTEGER); + private static final TrackedData CORRUPTION = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.INTEGER); + private static final TrackedData MAX_CORRUPTION = DataTracker.registerData(CastSpellEntity.class, TrackedDataHandlerRegistry.INTEGER); private final EntityPhysics physics = new EntityPhysics<>(this); @@ -29,8 +36,16 @@ public class CastSpellEntity extends LightEmittingEntity implements Caster owner = new EntityReference<>(); - private LevelStore level = Levelled.EMPTY; - private LevelStore corruption = Levelled.EMPTY; + private final LevelStore level = Levelled.of( + () -> dataTracker.get(LEVEL), + l -> dataTracker.set(LEVEL, l), + () -> dataTracker.get(MAX_LEVEL) + ); + private final LevelStore corruption = Levelled.of( + () -> dataTracker.get(CORRUPTION), + l -> dataTracker.set(CORRUPTION, l), + () -> dataTracker.get(MAX_CORRUPTION) + ); public CastSpellEntity(EntityType type, World world) { super(type, world); @@ -39,6 +54,10 @@ public class CastSpellEntity extends LightEmittingEntity implements Caster caster) { - this.level = Levelled.copyOf(caster.getLevel()); - this.corruption = Levelled.copyOf(caster.getCorruption()); + dataTracker.set(LEVEL, caster.getLevel().get()); + dataTracker.set(MAX_LEVEL, caster.getLevel().getMax()); + dataTracker.set(CORRUPTION, caster.getCorruption().get()); + dataTracker.set(MAX_CORRUPTION, caster.getCorruption().getMax()); setMaster(caster); } @@ -144,7 +165,11 @@ public class CastSpellEntity extends LightEmittingEntity implements Caster, MagicImmune { private static final TrackedData HYDROPHOBIC = DataTracker.registerData(MagicBeamEntity.class, TrackedDataHandlerRegistry.BOOLEAN); + private static final TrackedData LEVEL = DataTracker.registerData(MagicBeamEntity.class, TrackedDataHandlerRegistry.INTEGER); + private static final TrackedData MAX_LEVEL = DataTracker.registerData(MagicBeamEntity.class, TrackedDataHandlerRegistry.INTEGER); + private static final TrackedData CORRUPTION = DataTracker.registerData(MagicBeamEntity.class, TrackedDataHandlerRegistry.INTEGER); + private static final TrackedData MAX_CORRUPTION = DataTracker.registerData(MagicBeamEntity.class, TrackedDataHandlerRegistry.INTEGER); private final SpellInventory spells = SpellSlots.ofSingle(this); private final EntityPhysics physics = new EntityPhysics<>(this); + private final LevelStore level = Levelled.of( + () -> dataTracker.get(LEVEL), + l -> dataTracker.set(LEVEL, l), + () -> dataTracker.get(MAX_LEVEL) + ); + private final LevelStore corruption = Levelled.of( + () -> dataTracker.get(CORRUPTION), + l -> dataTracker.set(CORRUPTION, l), + () -> dataTracker.get(MAX_CORRUPTION) + ); + public MagicBeamEntity(EntityType type, World world) { super(type, world); } @@ -51,7 +68,11 @@ public class MagicBeamEntity extends MagicProjectileEntity implements Caster { + dataTracker.set(LEVEL, caster.getLevel().get()); + dataTracker.set(MAX_LEVEL, caster.getLevel().getMax()); + dataTracker.set(CORRUPTION, caster.getCorruption().get()); + dataTracker.set(MAX_CORRUPTION, caster.getCorruption().getMax()); + }); + } + @Override public LevelStore getLevel() { - return getMasterReference().getTarget().map(target -> target.level()).orElse(Levelled.EMPTY); + return level; } @Override public LevelStore getCorruption() { - return getMasterReference().getTarget().map(target -> target.corruption()).orElse(Levelled.EMPTY); + return corruption; } @Override @@ -149,11 +181,19 @@ public class MagicBeamEntity extends MagicProjectileEntity implements Caster