Search in sources :

Example 1 with StackObjectCopyApplier

use of mage.util.functions.StackObjectCopyApplier in project mage by magefree.

the class ChefsKissApplier method apply.

@Override
public boolean apply(Game game, Ability source) {
    Spell spell = game.getSpell(source.getFirstTarget());
    if (spell == null) {
        return false;
    }
    spell.setControllerId(source.getControllerId());
    List<UUID> possibleTargets = new ArrayList<>(game.getState().getPlayersInRange(source.getControllerId(), game));
    possibleTargets.remove(source.getControllerId());
    game.getBattlefield().getActivePermanents(source.getControllerId(), game).stream().filter(p -> !p.isControlledBy(source.getControllerId())).map(MageItem::getId).forEach(possibleTargets::add);
    possibleTargets.removeIf(uuid -> !spell.canTarget(game, uuid));
    StackObjectCopyApplier applier;
    MageObjectReferencePredicate predicate;
    if (possibleTargets.isEmpty()) {
        applier = null;
        predicate = null;
    } else {
        applier = new ChefsKissApplier(possibleTargets.get(RandomUtil.nextInt(possibleTargets.size())), game);
        predicate = new MageObjectReferencePredicate(new MageObjectReference(possibleTargets.get(RandomUtil.nextInt(possibleTargets.size())), game));
    }
    spell.createCopyOnStack(game, source, source.getControllerId(), false, 1, applier);
    if (predicate != null) {
        spell.chooseNewTargets(game, source.getControllerId(), true, true, predicate);
    }
    return true;
}
Also used : MageObjectReferencePredicate(mage.filter.predicate.mageobject.MageObjectReferencePredicate) StackObjectCopyApplier(mage.util.functions.StackObjectCopyApplier) FilterSpell(mage.filter.FilterSpell) Spell(mage.game.stack.Spell) TargetSpell(mage.target.TargetSpell) MageObjectReference(mage.MageObjectReference)

Aggregations

MageObjectReference (mage.MageObjectReference)1 FilterSpell (mage.filter.FilterSpell)1 MageObjectReferencePredicate (mage.filter.predicate.mageobject.MageObjectReferencePredicate)1 Spell (mage.game.stack.Spell)1 TargetSpell (mage.target.TargetSpell)1 StackObjectCopyApplier (mage.util.functions.StackObjectCopyApplier)1