diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/SpellContainer.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/SpellContainer.java index 8ed89ab0..87800d62 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/SpellContainer.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/SpellContainer.java @@ -129,6 +129,10 @@ public interface SpellContainer { public enum Operation { SKIP, KEEP, - REMOVE + REMOVE; + + public static Operation ofBoolean(boolean result) { + return result ? KEEP : REMOVE; + } } } 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 0e314d9d..def71387 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 @@ -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 findMatches(Predicate predicate) { + return Stream.concat(ProjectileSpell.super.findMatches(predicate), getDelegates().stream().flatMap(s -> s.findMatches(predicate))); + } + @Override public Affinity getAffinity() { return Affinity.NEUTRAL; 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 ee5b3820..ce527677 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 @@ -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 findMatches(Predicate predicate) { + return predicate.test(this) ? Stream.of(this) : Stream.empty(); + } + /** * Sets this effect as dead. */ diff --git a/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java index 20e90914..23527504 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/CastSpellEntity.java @@ -74,6 +74,7 @@ public class CastSpellEntity extends Entity implements Caster { return super.getName(); } + @SuppressWarnings("deprecation") @Override public void tick() { super.tick(); @@ -113,10 +114,7 @@ public class CastSpellEntity extends Entity implements Caster { 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(); diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Living.java b/src/main/java/com/minelittlepony/unicopia/entity/Living.java index 1453cabb..0fb631a6 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Living.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Living.java @@ -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 implements Equine, 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--; diff --git a/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java b/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java index 41ad2122..0a439e11 100644 --- a/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java +++ b/src/main/java/com/minelittlepony/unicopia/network/datasync/EffectSync.java @@ -68,7 +68,7 @@ public class EffectSync implements SpellContainer { @Override public boolean removeIf(Predicate 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) {