Fixed casting

This commit is contained in:
Sollace 2021-12-22 15:05:16 +02:00
parent eca5ab3e2b
commit c30eac20c3
4 changed files with 43 additions and 25 deletions

View file

@ -7,7 +7,6 @@ import org.jetbrains.annotations.Nullable;
import com.google.common.collect.Streams; import com.google.common.collect.Streams;
import com.minelittlepony.unicopia.Race; import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.ability.data.Hit; import com.minelittlepony.unicopia.ability.data.Hit;
import com.minelittlepony.unicopia.ability.magic.spell.Spell;
import com.minelittlepony.unicopia.ability.magic.spell.effect.CustomisedSpellType; import com.minelittlepony.unicopia.ability.magic.spell.effect.CustomisedSpellType;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
@ -117,11 +116,14 @@ public class UnicornCastingAbility implements Ability<Hit> {
if (newSpell.getResult() != ActionResult.FAIL) { if (newSpell.getResult() != ActionResult.FAIL) {
CustomisedSpellType<?> spell = newSpell.getValue(); CustomisedSpellType<?> spell = newSpell.getValue();
player.subtractEnergyCost(spell.isEmpty() ? 2 : 4); boolean remove = player.getSpellSlot().removeIf(spell, true);
player.subtractEnergyCost(remove ? 2 : 4);
if (!remove) {
spell.apply(player); spell.apply(player);
} }
} }
} }
}
private TypedActionResult<ItemStack> getAmulet(Pony player) { private TypedActionResult<ItemStack> getAmulet(Pony player) {
@ -139,12 +141,11 @@ public class UnicornCastingAbility implements Ability<Hit> {
} }
private TypedActionResult<CustomisedSpellType<?>> getNewSpell(Pony player) { private TypedActionResult<CustomisedSpellType<?>> getNewSpell(Pony player) {
final SpellType<?> current = player.getSpellSlot().get(true).map(Spell::getType).orElse(SpellType.empty());
return Streams.stream(player.getMaster().getItemsHand()) return Streams.stream(player.getMaster().getItemsHand())
.filter(GemstoneItem::isEnchanted) .filter(GemstoneItem::isEnchanted)
.map(stack -> GemstoneItem.consumeSpell(stack, player.getMaster(), current, null)) .map(stack -> GemstoneItem.consumeSpell(stack, player.getMaster(), null, null))
.findFirst() .findFirst()
.orElse(TypedActionResult.<CustomisedSpellType<?>>pass((current == SpellType.EMPTY_KEY ? SpellType.SHIELD : SpellType.EMPTY_KEY).withTraits())); .orElse(TypedActionResult.<CustomisedSpellType<?>>pass(SpellType.SHIELD.withTraits()));
} }
@Override @Override

View file

@ -22,7 +22,9 @@ public interface SpellContainer {
public void clear() { } public void clear() { }
@Override @Override
public void removeIf(Predicate<Spell> effect, boolean update) { } public boolean removeIf(Predicate<Spell> effect, boolean update) {
return false;
}
@Override @Override
public boolean forEach(Function<Spell, Operation> action, boolean update) { public boolean forEach(Function<Spell, Operation> action, boolean update) {
@ -56,8 +58,10 @@ public interface SpellContainer {
/** /**
* Removes all matching active effects. * Removes all matching active effects.
*
* @return True if the collection was changed
*/ */
void removeIf(Predicate<Spell> test, boolean update); boolean removeIf(Predicate<Spell> test, boolean update);
/** /**
* Iterates active spells and optionally removes matching ones. * Iterates active spells and optionally removes matching ones.
@ -86,8 +90,8 @@ public interface SpellContainer {
} }
@Override @Override
default void removeIf(Predicate<Spell> effect, boolean update) { default boolean removeIf(Predicate<Spell> effect, boolean update) {
delegate().removeIf(effect, update); return delegate().removeIf(effect, update);
} }
@Override @Override

View file

@ -3,11 +3,11 @@ package com.minelittlepony.unicopia.ability.magic.spell.effect;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.ability.magic.SpellPredicate;
import com.minelittlepony.unicopia.ability.magic.spell.Spell; import com.minelittlepony.unicopia.ability.magic.spell.Spell;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
public class CustomisedSpellType<T extends Spell> { public class CustomisedSpellType<T extends Spell> implements SpellPredicate<T> {
private final SpellType<T> type; private final SpellType<T> type;
private final SpellTraits traits; private final SpellTraits traits;
@ -28,4 +28,9 @@ public class CustomisedSpellType<T extends Spell> {
public T apply(Caster<?> caster) { public T apply(Caster<?> caster) {
return type.apply(caster, traits); return type.apply(caster, traits);
} }
@Override
public boolean test(Spell spell) {
return type.test(spell);
}
} }

View file

@ -1,7 +1,6 @@
package com.minelittlepony.unicopia.network.datasync; package com.minelittlepony.unicopia.network.datasync;
import java.util.Optional; import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -61,28 +60,27 @@ public class EffectSync implements SpellContainer {
} }
@Override @Override
public void removeIf(Predicate<Spell> test, boolean update) { public boolean removeIf(Predicate<Spell> test, boolean update) {
forEach(effect -> { return reduce((initial, effect) -> {
if (test.test(effect)) { if (!test.test(effect)) {
spells.removeReference(effect); return initial;
} }
spells.removeReference(effect);
return true;
}); });
} }
@Override @Override
public boolean forEach(Function<Spell, Operation> test, boolean update) { public boolean forEach(Function<Spell, Operation> test, boolean update) {
boolean[] matched = new boolean[1]; return reduce((initial, effect) -> {
forEach(effect -> {
Operation op = test.apply(effect); Operation op = test.apply(effect);
if (op == Operation.REMOVE) { if (op == Operation.REMOVE) {
spells.removeReference(effect); spells.removeReference(effect);
} else { } else {
matched[0] |= op != Operation.SKIP; initial |= op != Operation.SKIP;
} }
return initial;
}); });
return matched[0];
} }
@Override @Override
@ -103,10 +101,16 @@ public class EffectSync implements SpellContainer {
return spells.getReferences(); return spells.getReferences();
} }
public void forEach(Consumer<Spell> consumer) { public boolean reduce(Alteration alteration) {
spells.fromNbt(owner.getEntity().getDataTracker().get(param)); spells.fromNbt(owner.getEntity().getDataTracker().get(param));
spells.getReferences().toList().forEach(consumer);
boolean initial = false;
for (Spell i : spells.getReferences().toList()) {
initial = alteration.apply(initial, i);
}
write(); write();
return initial;
} }
private void write() { private void write() {
@ -118,4 +122,8 @@ public class EffectSync implements SpellContainer {
public interface UpdateCallback { public interface UpdateCallback {
void onSpellSet(@Nullable Spell spell); void onSpellSet(@Nullable Spell spell);
} }
private interface Alteration {
boolean apply(boolean initial, Spell item);
}
} }