Package all the complex stuff into a class so I can reuse it.

This commit is contained in:
Sollace 2019-02-06 20:35:59 +02:00
parent c2b4a07ecd
commit f95be4762e
2 changed files with 29 additions and 11 deletions

View file

@ -0,0 +1,24 @@
package com.minelittlepony.unicopia.particle;
import java.util.Optional;
import java.util.function.Supplier;
import com.minelittlepony.unicopia.spell.ICaster;
/**
* A connection class for updating and persisting an attached particle effect.
*/
public class ParticleConnection {
private Optional<IAttachableParticle> particleEffect = Optional.empty();
public Optional<IAttachableParticle> ifMissing(ICaster<?> source, Supplier<Optional<IAttachableParticle>> constructor) {
particleEffect.filter(IAttachableParticle::isStillAlive).orElseGet(() -> {
particleEffect = constructor.get();
particleEffect.ifPresent(p -> p.attachTo(source));
return null;
});
return particleEffect;
}
}

View file

@ -1,10 +1,8 @@
package com.minelittlepony.unicopia.spell; package com.minelittlepony.unicopia.spell;
import java.util.Optional;
import com.minelittlepony.unicopia.Predicates; import com.minelittlepony.unicopia.Predicates;
import com.minelittlepony.unicopia.UParticles; import com.minelittlepony.unicopia.UParticles;
import com.minelittlepony.unicopia.particle.IAttachableParticle; import com.minelittlepony.unicopia.particle.ParticleConnection;
import com.minelittlepony.unicopia.particle.Particles; import com.minelittlepony.unicopia.particle.Particles;
import com.minelittlepony.unicopia.player.IPlayer; import com.minelittlepony.unicopia.player.IPlayer;
import com.minelittlepony.unicopia.player.PlayerSpeciesList; import com.minelittlepony.unicopia.player.PlayerSpeciesList;
@ -20,7 +18,7 @@ import net.minecraft.util.math.Vec3d;
public class SpellShield extends AbstractSpell.RangedAreaSpell { public class SpellShield extends AbstractSpell.RangedAreaSpell {
private Optional<IAttachableParticle> particleEffect = Optional.empty(); private final ParticleConnection particlEffect = new ParticleConnection();
@Override @Override
public String getName() { public String getName() {
@ -45,13 +43,9 @@ public class SpellShield extends AbstractSpell.RangedAreaSpell {
Particles.instance().spawnParticle(UParticles.UNICORN_MAGIC, false, pos, 0, 0, 0); Particles.instance().spawnParticle(UParticles.UNICORN_MAGIC, false, pos, 0, 0, 0);
}); });
particleEffect.filter(IAttachableParticle::isStillAlive).orElseGet(() -> { particlEffect
particleEffect = Particles.instance().spawnParticle(UParticles.SPHERE, true, source.getOriginVector(), 0, 0, 0, radius, getTint(), 30); .ifMissing(source, () -> Particles.instance().spawnParticle(UParticles.SPHERE, true, source.getOriginVector(), 0, 0, 0, radius, getTint(), 30))
particleEffect.ifPresent(p -> p.attachTo(source)); .ifPresent(p -> p.setAttribute(0, radius));
return null;
});
particleEffect.ifPresent(p -> p.setAttribute(0, radius));
} }
@Override @Override