* Mitotic Manipulation - Fixed a bug that no card could be put onto the battlefield.

This commit is contained in:
LevelX2 2018-02-12 22:57:09 +01:00
parent 2cb86e2fc0
commit a6bde313e3

View file

@ -27,6 +27,12 @@
*/
package mage.cards.m;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.effects.OneShotEffect;
import mage.cards.*;
@ -34,17 +40,13 @@ import mage.constants.CardType;
import mage.constants.Outcome;
import mage.constants.Zone;
import mage.filter.FilterCard;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.NamePredicate;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
import mage.target.TargetCard;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
/**
*
* @author North
@ -52,8 +54,7 @@ import java.util.UUID;
public class MitoticManipulation extends CardImpl {
public MitoticManipulation(UUID ownerId, CardSetInfo setInfo) {
super(ownerId,setInfo,new CardType[]{CardType.SORCERY},"{1}{U}{U}");
super(ownerId, setInfo, new CardType[]{CardType.SORCERY}, "{1}{U}{U}");
this.getSpellAbility().addEffect(new MitoticManipulationEffect());
}
@ -86,45 +87,39 @@ class MitoticManipulationEffect extends OneShotEffect {
@Override
public boolean apply(Game game, Ability source) {
List<Permanent> permanents = game.getBattlefield().getActivePermanents(source.getControllerId(), game);
Set<String> permanentNames = new HashSet<>();
FilterCard filter = new FilterCard("card to put onto the battlefield");
for (Permanent permanent : permanents) {
permanentNames.add(permanent.getName());
filter.add(new NamePredicate(permanent.getName()));
}
Player controller = game.getPlayer(source.getControllerId());
MageObject sourceObject = source.getSourceObject(game);
if (controller != null && sourceObject != null) {
Set<String> permanentNames = new HashSet<>();
for (Permanent permanent : game.getBattlefield().getActivePermanents(source.getControllerId(), game)) {
permanentNames.add(permanent.getName());
}
Player player = game.getPlayer(source.getControllerId());
if (player == null) {
return false;
}
Cards cards = new CardsImpl();
Cards cardsFound = new CardsImpl();
int count = Math.min(player.getLibrary().size(), 7);
for (int i = 0; i < count; i++) {
Card card = player.getLibrary().removeFromTop(game);
if (card != null) {
cards.add(card);
if (permanentNames.contains(card.getName())) {
cardsFound.add(card);
Cards cardsFromTop = new CardsImpl();
cardsFromTop.addAll(controller.getLibrary().getTopCards(game, 7));
controller.lookAtCards(sourceObject.getIdName(), cardsFromTop, game);
FilterCard filter = new FilterCard("card to put onto the battlefield");
List<NamePredicate> namePredicates = new ArrayList<>();
for (String name : permanentNames) {
namePredicates.add(new NamePredicate(name));
}
if (!namePredicates.isEmpty() && !cardsFromTop.isEmpty()) {
filter.add(Predicates.or(namePredicates));
TargetCard target = new TargetCard(Zone.LIBRARY, filter);
if (cardsFromTop.count(filter, source.getSourceId(), source.getControllerId(), game) > 0
&& controller.chooseUse(Outcome.PutCardInPlay, "Do you wish to put a card on the battlefield?", source, game)) {
if (controller.choose(Outcome.PutCardInPlay, cardsFromTop, target, game)) {
Card card = cardsFromTop.get(target.getFirstTarget(), game);
if (card != null) {
controller.moveCards(card, Zone.BATTLEFIELD, source, game);
cardsFromTop.remove(card);
}
}
}
}
controller.putCardsOnBottomOfLibrary(cardsFromTop, game, source, true);
return true;
}
player.lookAtCards("Mitotic Manipulation", cards, game);
if (!cardsFound.isEmpty() && player.chooseUse(Outcome.PutCardInPlay, "Do you wish to put a card on the battlefield?", source, game)) {
TargetCard target = new TargetCard(Zone.LIBRARY, filter);
if (player.choose(Outcome.PutCardInPlay, cardsFound, target, game)) {
Card card = cards.get(target.getFirstTarget(), game);
if (card != null) {
cards.remove(card);
card.putOntoBattlefield(game, Zone.LIBRARY, source.getSourceId(), source.getControllerId());
}
}
}
player.putCardsOnBottomOfLibrary(cards, game, source, true);
return true;
return false;
}
}