@Override
public boolean apply(Game game, Ability source) {
game.getPlayerList();
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
PlayerList playerList = game.getPlayerList();
playerList.setCurrent(game.getActivePlayerId());
Player player = game.getPlayer(game.getActivePlayerId());
do {
ArrayList<Permanent> permanentsToTop = new ArrayList<>();
ArrayList<Permanent> permanentsToBottom = new ArrayList<>();
for (Permanent permanent:game.getState().getBattlefield().getActivePermanents(new FilterAttackingCreature(), player.getId(), source.getSourceId(), game)) {
if (permanent.getOwnerId().equals(player.getId())) {
if (player.chooseUse(outcome, "Put " + permanent.getLogName() + " to the top? (else it goes to bottom)", game)) {
permanentsToTop.add(permanent);
game.informPlayers(permanent.getLogName() + " goes to the top of " + player.getName() + "'s library");
} else {
permanentsToBottom.add(permanent);
game.informPlayers(permanent.getLogName() + " goes to the bottom of " + player.getName() + "'s library");
}
}
}
// cards to top
Cards cards = new CardsImpl();
ArrayList<Permanent> toLibrary = new ArrayList<>();
for (Permanent permanent: permanentsToTop) {
if (permanent instanceof PermanentToken) {
toLibrary.add(permanent);
} else {
Card card = game.getCard(permanent.getId());
if (card != null) {
cards.add(card);
}
}
}
TargetCard target = new TargetCard(Zone.BATTLEFIELD, new FilterCard("order to put on the top of library (last choosen will be the top most)"));
while (player.isInGame() && cards.size() > 1) {
player.choose(Outcome.Neutral, cards, target, game);
Card card = cards.get(target.getFirstTarget(), game);
if (card != null) {
cards.remove(card);
Permanent permanent = game.getPermanent(card.getId());
if (permanent != null) {
toLibrary.add(permanent);
}
}
target.clearChosen();
}
if (cards.size() == 1) {
Card card = cards.get(cards.iterator().next(), game);
Permanent permanent = game.getPermanent(card.getId());
if (permanent != null) {
toLibrary.add(permanent);
}
}
// move all permanents to lib at the same time
for(Permanent permanent: toLibrary) {
player.moveCardToLibraryWithInfo(permanent, source.getSourceId(), game, Zone.BATTLEFIELD, true, false);
}
// cards to bottom
cards.clear();
toLibrary.clear();
for (Permanent permanent: permanentsToBottom) {
if (permanent instanceof PermanentToken) {
toLibrary.add(permanent);
} else {
Card card = game.getCard(permanent.getId());
if (card != null) {
cards.add(card);
}
}
}
target = new TargetCard(Zone.BATTLEFIELD, new FilterCard("order to put on bottom of library (last choosen will be bottommost card)"));
while (player.isInGame() && cards.size() > 1) {
player.choose(Outcome.Neutral, cards, target, game);
Card card = cards.get(target.getFirstTarget(), game);
if (card != null) {
cards.remove(card);
Permanent permanent = game.getPermanent(card.getId());
if (permanent != null) {
toLibrary.add(permanent);
}
}
target.clearChosen();
}
if (cards.size() == 1) {
Card card = cards.get(cards.iterator().next(), game);
Permanent permanent = game.getPermanent(card.getId());
if (permanent != null) {
toLibrary.add(permanent);
}
}
// move all permanents to lib at the same time
for(Permanent permanent: toLibrary) {
player.moveCardToLibraryWithInfo(permanent, source.getSourceId(), game, Zone.BATTLEFIELD, false, false);
}
player = playerList.getNext(game);
} while (player != null && !player.getId().equals(game.getActivePlayerId()));
return true;
}
return false;
}