Fixed not being able to toggle delegated spells

This commit is contained in:
Sollace 2021-12-26 11:13:32 +02:00
parent 613b39436f
commit df2706cfc6
6 changed files with 26 additions and 8 deletions

View file

@ -129,6 +129,10 @@ public interface SpellContainer {
public enum Operation {
SKIP,
KEEP,
REMOVE
REMOVE;
public static Operation ofBoolean(boolean result) {
return result ? KEEP : REMOVE;
}
}
}

View file

@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.ability.magic.spell;
import java.util.Collection;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import com.minelittlepony.unicopia.Affinity;
@ -36,6 +37,11 @@ public abstract class AbstractDelegatingSpell implements ProjectileSpell {
return ProjectileSpell.super.equalsOrContains(id) || getDelegates().stream().anyMatch(s -> s.equalsOrContains(id));
}
@Override
public Stream<Spell> findMatches(Predicate<Spell> predicate) {
return Stream.concat(ProjectileSpell.super.findMatches(predicate), getDelegates().stream().flatMap(s -> s.findMatches(predicate)));
}
@Override
public Affinity getAffinity() {
return Affinity.NEUTRAL;

View file

@ -1,6 +1,8 @@
package com.minelittlepony.unicopia.ability.magic.spell;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.spongepowered.include.com.google.common.base.Objects;
@ -32,6 +34,13 @@ public interface Spell extends NbtSerialisable, Affine {
return Objects.equal(getUuid(), id);
}
/**
* Returns an optional containing the spell that matched the given predicate.
*/
default Stream<Spell> findMatches(Predicate<Spell> predicate) {
return predicate.test(this) ? Stream.of(this) : Stream.empty();
}
/**
* Sets this effect as dead.
*/

View file

@ -74,6 +74,7 @@ public class CastSpellEntity extends Entity implements Caster<LivingEntity> {
return super.getName();
}
@SuppressWarnings("deprecation")
@Override
public void tick() {
super.tick();
@ -113,10 +114,7 @@ public class CastSpellEntity extends Entity implements Caster<LivingEntity> {
if (!dataTracker.get(SPELL).filter(spellId -> {
return getSpellSlot().forEach(spell -> {
if (!spell.getUuid().equals(spellId)) {
return Operation.SKIP;
}
return spell.tick(this, Situation.GROUND_ENTITY) ? Operation.KEEP: Operation.REMOVE;
return spell.getUuid().equals(spellId) ? Operation.ofBoolean(spell.tick(this, Situation.GROUND_ENTITY)) : Operation.SKIP;
}, true);
}).isPresent()) {
discard();

View file

@ -8,6 +8,7 @@ import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.ability.magic.SpellContainer;
import com.minelittlepony.unicopia.ability.magic.SpellPredicate;
import com.minelittlepony.unicopia.ability.magic.SpellContainer.Operation;
import com.minelittlepony.unicopia.ability.magic.spell.Situation;
import com.minelittlepony.unicopia.item.UItems;
import com.minelittlepony.unicopia.network.datasync.EffectSync;
@ -85,7 +86,7 @@ public abstract class Living<T extends LivingEntity> implements Equine<T>, Caste
@Override
public void tick() {
getSpellSlot().removeIf(effect -> !effect.tick(this, Situation.BODY), true);
getSpellSlot().forEach(spell -> Operation.ofBoolean(spell.tick(this, Situation.BODY)), true);
if (invinsibilityTicks > 0) {
invinsibilityTicks--;

View file

@ -68,7 +68,7 @@ public class EffectSync implements SpellContainer {
@Override
public boolean removeIf(Predicate<Spell> test, boolean update) {
return reduce((initial, effect) -> {
if (!test.test(effect)) {
if (!effect.findMatches(test).findFirst().isPresent()) {
return initial;
}
spells.removeReference(effect);
@ -107,7 +107,7 @@ public class EffectSync implements SpellContainer {
if (type == null) {
return spells.getReferences();
}
return spells.getReferences().filter(type);
return spells.getReferences().flatMap(s -> s.findMatches(type));
}
public boolean reduce(Alteration alteration) {