diff --git a/Mage/src/mage/abilities/effects/common/PutOnLibraryTargetEffect.java b/Mage/src/mage/abilities/effects/common/PutOnLibraryTargetEffect.java index e51740c8bc..7ec24da9db 100644 --- a/Mage/src/mage/abilities/effects/common/PutOnLibraryTargetEffect.java +++ b/Mage/src/mage/abilities/effects/common/PutOnLibraryTargetEffect.java @@ -27,6 +27,9 @@ */ package mage.abilities.effects.common; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; import mage.constants.Outcome; import mage.constants.Zone; import mage.abilities.Ability; @@ -38,6 +41,8 @@ import mage.game.permanent.Permanent; import mage.players.Player; import java.util.UUID; +import mage.cards.Cards; +import mage.cards.CardsImpl; import mage.target.Target; import mage.util.CardUtil; @@ -71,27 +76,61 @@ public class PutOnLibraryTargetEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { - boolean result = false; Player controller = game.getPlayer(source.getControllerId()); if (controller != null) { + List cards = new ArrayList<>(); + List permanents = new ArrayList<>(); for (UUID targetId : targetPointer.getTargets(game, source)) { switch (game.getState().getZone(targetId)) { case BATTLEFIELD: Permanent permanent = game.getPermanent(targetId); if (permanent != null) { - result |= controller.moveCardToLibraryWithInfo(permanent, source.getSourceId(), game, Zone.BATTLEFIELD, onTop, true); + permanents.add(permanent); } break; case GRAVEYARD: Card card = game.getCard(targetId); if (card != null && game.getState().getZone(targetId).equals(Zone.GRAVEYARD)) { - result |= controller.moveCardToLibraryWithInfo(card, source.getSourceId(), game, Zone.GRAVEYARD, onTop, true); + cards.add(card); } break; } } + // Plow Under + // 10/4/2004 The owner decides the order the two lands are stacked there. + while (!cards.isEmpty()) { + Card card = cards.iterator().next(); + if (card != null) { + Player owner = game.getPlayer(card.getOwnerId()); + Cards cardsPlayer = new CardsImpl(); + for (Iterator iterator = cards.iterator(); iterator.hasNext();) { + Card next = iterator.next(); + if (next.getOwnerId().equals(owner.getId())) { + cardsPlayer.add(next); + iterator.remove(); + } + } + owner.putCardsOnTopOfLibrary(cardsPlayer, game, source, true); + } + } + while (!permanents.isEmpty()) { + Permanent permanent = permanents.iterator().next(); + if (permanent != null) { + Player owner = game.getPlayer(permanent.getOwnerId()); + Cards cardsPlayer = new CardsImpl(); + for (Iterator iterator = permanents.iterator(); iterator.hasNext();) { + Permanent next = iterator.next(); + if (next.getOwnerId().equals(owner.getId())) { + cardsPlayer.add(next); + iterator.remove(); + } + } + owner.putCardsOnTopOfLibrary(cardsPlayer, game, source, true); + } + } + return true; } - return result; + return false; } diff --git a/Mage/src/mage/players/PlayerImpl.java b/Mage/src/mage/players/PlayerImpl.java index 485a277843..2cef90f5bc 100644 --- a/Mage/src/mage/players/PlayerImpl.java +++ b/Mage/src/mage/players/PlayerImpl.java @@ -854,14 +854,26 @@ public abstract class PlayerImpl implements Player, Serializable { if (chosenCard != null) { cards.remove(chosenCard); Zone fromZone = game.getState().getZone(chosenCard.getId()); - this.moveCardToLibraryWithInfo(chosenCard, source.getSourceId(), game, fromZone, true, false); + if (fromZone.equals(Zone.BATTLEFIELD)) { + Permanent permanent = game.getPermanent(chosenCard.getId()); + this.moveCardToLibraryWithInfo(permanent, source.getSourceId(), game, fromZone, true, false); + } else { + this.moveCardToLibraryWithInfo(chosenCard, source.getSourceId(), game, fromZone, true, false); + } } target.clearChosen(); } if (cards.size() == 1) { - Card chosenCard = cards.get(cards.iterator().next(), game); - Zone fromZone = game.getState().getZone(chosenCard.getId()); - this.moveCardToLibraryWithInfo(chosenCard, source.getSourceId(), game, fromZone, true, false); + // Card chosenCard = cards.get(cards.iterator().next(), game); + UUID cardId = cards.iterator().next(); + Zone fromZone = game.getState().getZone(cardId); + if (fromZone.equals(Zone.BATTLEFIELD)) { + Permanent permanent = game.getPermanent(cardId); + this.moveCardToLibraryWithInfo(permanent, source.getSourceId(), game, fromZone, true, false); + } else { + Card chosenCard = cards.get(cardId, game); + this.moveCardToLibraryWithInfo(chosenCard, source.getSourceId(), game, fromZone, true, false); + } } } }