Fix some things that I missed

This commit is contained in:
Sollace 2022-09-04 14:16:47 +02:00
parent 3e848e86ed
commit edad4678ca
6 changed files with 74 additions and 33 deletions

View file

@ -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<Spell> spells, Function<Spell, Boolean> action) {
return spells.reduce(false, (u, a) -> action.apply(a), (a, b) -> a || b);
}

View file

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

View file

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

View file

@ -19,6 +19,7 @@ import net.minecraft.nbt.NbtCompound;
* Interface for a magic spells
*/
public interface Spell extends NbtSerialisable, Affine {
Serializer<Spell> SERIALIZER = Serializer.of(Spell::readNbt, Spell::writeNbt);
/**
* Returns the registered type of this spell.

View file

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

View file

@ -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<BlockPos> readBlockPos(String name, NbtCompound nbt) {
return nbt.contains(name) ? Optional.ofNullable(NbtHelper.toBlockPos(nbt.getCompound(name))) : Optional.empty();
}
interface Serializer<T> {
T read(NbtCompound compound);
NbtCompound write(T t);
default T read(NbtElement element) {
return read((NbtCompound)element);
}
default NbtList writeAll(Collection<T> ts) {
NbtList list = new NbtList();
ts.stream().map(this::write).forEach(list::add);
return list;
}
default Stream<T> readAll(NbtList list) {
return list.stream().map(this::read).filter(Objects::nonNull);
}
static <T> Serializer<T> of(Function<NbtCompound, T> read, Function<T, NbtCompound> write) {
return new Serializer<>() {
@Override
public T read(NbtCompound compound) {
return read.apply(compound);
}
@Override
public NbtCompound write(T t) {
return write.apply(t);
}
};
}
}
}