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; return type;
} }
@Override
public SpellTraits getTraits() {
return getDelegates().stream().map(Spell::getTraits).reduce(SpellTraits.EMPTY, SpellTraits::union);
}
@Override @Override
public UUID getUuid() { public UUID getUuid() {
return uuid; return uuid;
@ -113,6 +117,7 @@ public abstract class AbstractDelegatingSpell implements ProjectileSpell {
@Override @Override
public void toNBT(NbtCompound compound) { public void toNBT(NbtCompound compound) {
compound.putUuid("uuid", uuid); compound.putUuid("uuid", uuid);
saveDelegates(compound);
} }
@Override @Override
@ -121,8 +126,13 @@ public abstract class AbstractDelegatingSpell implements ProjectileSpell {
if (compound.contains("uuid")) { if (compound.contains("uuid")) {
uuid = compound.getUuid("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) { private static boolean execute(Stream<Spell> spells, Function<Spell, Boolean> action) {
return spells.reduce(false, (u, a) -> action.apply(a), (a, b) -> a || b); 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.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
public class CompoundSpell extends AbstractDelegatingSpell { public class CompoundSpell extends AbstractDelegatingSpell {
private final List<Spell> spells = new ArrayList<>(); private final List<Spell> spells = new ArrayList<>();
@ -35,22 +33,15 @@ public class CompoundSpell extends AbstractDelegatingSpell {
} }
@Override @Override
public void toNBT(NbtCompound compound) { protected void loadDelegates(NbtCompound compound) {
super.toNBT(compound); spells.clear();
NbtList spells = new NbtList(); if (compound.contains("spells", NbtElement.LIST_TYPE)) {
this.spells.stream().map(Spell::writeNbt).forEach(spells::add); spells.addAll(Spell.SERIALIZER.readAll(compound.getList("spells", NbtElement.COMPOUND_TYPE)).toList());
compound.put("spells", spells); }
} }
@Override @Override
public void fromNBT(NbtCompound compound) { protected void saveDelegates(NbtCompound compound) {
super.fromNBT(compound); compound.put("spells", Spell.SERIALIZER.writeAll(spells));
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());
}
} }
} }

View file

@ -1,8 +1,6 @@
package com.minelittlepony.unicopia.ability.magic.spell; package com.minelittlepony.unicopia.ability.magic.spell;
import java.util.Collection; import java.util.*;
import java.util.List;
import java.util.Optional;
import org.jetbrains.annotations.Nullable; 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.ParticleHandle;
import com.minelittlepony.unicopia.particle.UParticles; import com.minelittlepony.unicopia.particle.UParticles;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.*;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
@ -134,7 +132,7 @@ public class PlaceableSpell extends AbstractDelegatingSpell {
compound.putString("dimension", dimension.getValue().toString()); compound.putString("dimension", dimension.getValue().toString());
} }
compound.put("castEntity", castEntity.toNBT()); compound.put("castEntity", castEntity.toNBT());
compound.put("spell", Spell.writeNbt(spell));
} }
@Override @Override
@ -149,7 +147,17 @@ public class PlaceableSpell extends AbstractDelegatingSpell {
if (compound.contains("castEntity")) { if (compound.contains("castEntity")) {
castEntity.fromNBT(compound.getCompound("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 @Override

View file

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

View file

@ -78,15 +78,13 @@ public final class ThrowableSpell extends AbstractDelegatingSpell {
} }
@Override @Override
public void toNBT(NbtCompound compound) { protected void loadDelegates(NbtCompound compound) {
super.toNBT(compound); spell = Spell.SERIALIZER.read(compound.getCompound("spell"));
compound.put("spell", Spell.writeNbt(spell));
} }
@Override @Override
public void fromNBT(NbtCompound compound) { protected void saveDelegates(NbtCompound compound) {
super.fromNBT(compound); compound.put("spell", Spell.SERIALIZER.write(spell));
spell = Spell.readNbt(compound.getCompound("spell"));
} }
@Override @Override

View file

@ -1,11 +1,10 @@
package com.minelittlepony.unicopia.util; 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.*;
import net.minecraft.nbt.NbtDouble;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
@ -49,4 +48,38 @@ public interface NbtSerialisable {
static Optional<BlockPos> readBlockPos(String name, NbtCompound nbt) { static Optional<BlockPos> readBlockPos(String name, NbtCompound nbt) {
return nbt.contains(name) ? Optional.ofNullable(NbtHelper.toBlockPos(nbt.getCompound(name))) : Optional.empty(); 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);
}
};
}
}
} }