From edad4678cad64b3a4559f3c126e0f7656157d5bb Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 4 Sep 2022 14:16:47 +0200 Subject: [PATCH] Fix some things that I missed --- .../magic/spell/AbstractDelegatingSpell.java | 10 +++++ .../ability/magic/spell/CompoundSpell.java | 23 +++------- .../ability/magic/spell/PlaceableSpell.java | 20 ++++++--- .../unicopia/ability/magic/spell/Spell.java | 1 + .../ability/magic/spell/ThrowableSpell.java | 10 ++--- .../unicopia/util/NbtSerialisable.java | 43 ++++++++++++++++--- 6 files changed, 74 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDelegatingSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDelegatingSpell.java index 79879fba..52dad1f5 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDelegatingSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/AbstractDelegatingSpell.java @@ -52,6 +52,10 @@ public abstract class AbstractDelegatingSpell implements ProjectileSpell { return type; } + @Override + public SpellTraits getTraits() { + return getDelegates().stream().map(Spell::getTraits).reduce(SpellTraits.EMPTY, SpellTraits::union); + } @Override public UUID getUuid() { return uuid; @@ -113,6 +117,7 @@ public abstract class AbstractDelegatingSpell implements ProjectileSpell { @Override public void toNBT(NbtCompound compound) { compound.putUuid("uuid", uuid); + saveDelegates(compound); } @Override @@ -121,8 +126,13 @@ public abstract class AbstractDelegatingSpell implements ProjectileSpell { if (compound.contains("uuid")) { uuid = compound.getUuid("uuid"); } + loadDelegates(compound); } + protected abstract void loadDelegates(NbtCompound compound); + + protected abstract void saveDelegates(NbtCompound compound); + private static boolean execute(Stream spells, Function action) { return spells.reduce(false, (u, a) -> action.apply(a), (a, b) -> a || b); } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/CompoundSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/CompoundSpell.java index 7eb71f16..92d56ba3 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/CompoundSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/CompoundSpell.java @@ -3,14 +3,12 @@ package com.minelittlepony.unicopia.ability.magic.spell; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.Objects; import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtElement; -import net.minecraft.nbt.NbtList; public class CompoundSpell extends AbstractDelegatingSpell { private final List spells = new ArrayList<>(); @@ -35,22 +33,15 @@ public class CompoundSpell extends AbstractDelegatingSpell { } @Override - public void toNBT(NbtCompound compound) { - super.toNBT(compound); - NbtList spells = new NbtList(); - this.spells.stream().map(Spell::writeNbt).forEach(spells::add); - compound.put("spells", spells); + protected void loadDelegates(NbtCompound compound) { + spells.clear(); + if (compound.contains("spells", NbtElement.LIST_TYPE)) { + spells.addAll(Spell.SERIALIZER.readAll(compound.getList("spells", NbtElement.COMPOUND_TYPE)).toList()); + } } @Override - public void fromNBT(NbtCompound compound) { - super.fromNBT(compound); - spells.clear(); - if (compound.contains("spells", NbtElement.LIST_TYPE)) { - spells.addAll(compound.getList("spells", NbtElement.COMPOUND_TYPE).stream() - .map(el -> Spell.readNbt((NbtCompound)el)) - .filter(Objects::nonNull) - .toList()); - } + protected void saveDelegates(NbtCompound compound) { + compound.put("spells", Spell.SERIALIZER.writeAll(spells)); } } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java index 00163209..2d2e3ed9 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/PlaceableSpell.java @@ -1,8 +1,6 @@ package com.minelittlepony.unicopia.ability.magic.spell; -import java.util.Collection; -import java.util.List; -import java.util.Optional; +import java.util.*; import org.jetbrains.annotations.Nullable; @@ -16,7 +14,7 @@ import com.minelittlepony.unicopia.particle.OrientedBillboardParticleEffect; import com.minelittlepony.unicopia.particle.ParticleHandle; import com.minelittlepony.unicopia.particle.UParticles; -import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.*; import net.minecraft.util.Identifier; import net.minecraft.util.math.Vec3d; import net.minecraft.util.registry.Registry; @@ -134,7 +132,7 @@ public class PlaceableSpell extends AbstractDelegatingSpell { compound.putString("dimension", dimension.getValue().toString()); } compound.put("castEntity", castEntity.toNBT()); - compound.put("spell", Spell.writeNbt(spell)); + } @Override @@ -149,7 +147,17 @@ public class PlaceableSpell extends AbstractDelegatingSpell { if (compound.contains("castEntity")) { castEntity.fromNBT(compound.getCompound("castEntity")); } - spell = Spell.readNbt(compound.getCompound("spell")); + + } + + @Override + protected void loadDelegates(NbtCompound compound) { + spell = Spell.SERIALIZER.read(compound.getCompound("spell")); + } + + @Override + protected void saveDelegates(NbtCompound compound) { + compound.put("spell", Spell.SERIALIZER.write(spell)); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/Spell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/Spell.java index 4f9526b2..8ff10e08 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/Spell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/Spell.java @@ -19,6 +19,7 @@ import net.minecraft.nbt.NbtCompound; * Interface for a magic spells */ public interface Spell extends NbtSerialisable, Affine { + Serializer SERIALIZER = Serializer.of(Spell::readNbt, Spell::writeNbt); /** * Returns the registered type of this spell. diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/ThrowableSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/ThrowableSpell.java index 66f3e2c4..14175a07 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/ThrowableSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/ThrowableSpell.java @@ -78,15 +78,13 @@ public final class ThrowableSpell extends AbstractDelegatingSpell { } @Override - public void toNBT(NbtCompound compound) { - super.toNBT(compound); - compound.put("spell", Spell.writeNbt(spell)); + protected void loadDelegates(NbtCompound compound) { + spell = Spell.SERIALIZER.read(compound.getCompound("spell")); } @Override - public void fromNBT(NbtCompound compound) { - super.fromNBT(compound); - spell = Spell.readNbt(compound.getCompound("spell")); + protected void saveDelegates(NbtCompound compound) { + compound.put("spell", Spell.SERIALIZER.write(spell)); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/util/NbtSerialisable.java b/src/main/java/com/minelittlepony/unicopia/util/NbtSerialisable.java index 69f5f6ec..4270a440 100644 --- a/src/main/java/com/minelittlepony/unicopia/util/NbtSerialisable.java +++ b/src/main/java/com/minelittlepony/unicopia/util/NbtSerialisable.java @@ -1,11 +1,10 @@ package com.minelittlepony.unicopia.util; -import java.util.Optional; +import java.util.*; +import java.util.function.Function; +import java.util.stream.Stream; -import net.minecraft.nbt.NbtCompound; -import net.minecraft.nbt.NbtDouble; -import net.minecraft.nbt.NbtHelper; -import net.minecraft.nbt.NbtList; +import net.minecraft.nbt.*; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; @@ -49,4 +48,38 @@ public interface NbtSerialisable { static Optional readBlockPos(String name, NbtCompound nbt) { return nbt.contains(name) ? Optional.ofNullable(NbtHelper.toBlockPos(nbt.getCompound(name))) : Optional.empty(); } + + interface Serializer { + T read(NbtCompound compound); + + NbtCompound write(T t); + + default T read(NbtElement element) { + return read((NbtCompound)element); + } + + default NbtList writeAll(Collection ts) { + NbtList list = new NbtList(); + ts.stream().map(this::write).forEach(list::add); + return list; + } + + default Stream readAll(NbtList list) { + return list.stream().map(this::read).filter(Objects::nonNull); + } + + static Serializer of(Function read, Function write) { + return new Serializer<>() { + @Override + public T read(NbtCompound compound) { + return read.apply(compound); + } + + @Override + public NbtCompound write(T t) { + return write.apply(t); + } + }; + } + } }