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;
}
Aggregations