Permanent permanent = game.getPermanent(source.getTargets().getFirstTarget());
if (permanent != null) {
if (permanent.sacrifice(source.getSourceId(), game)) {
Player permanentController = game.getPlayer(permanent.getControllerId());
if (permanentController != null) {
Library library = permanentController.getLibrary();
if (library.size() > 0) {
Cards cards = new CardsImpl();
Card card = null;
boolean cardFound = false;
if (library.size() > 0) {
do {
card = library.removeFromTop(game);
cards.add(card);
if (filter.match(card, game)) {
for (CardType cardType : permanent.getCardType()) {
if (card.getCardType().contains(cardType)) {
// a permanent card
cardFound = true;
break;
}
}
}
} while (!cardFound && library.size() > 0);
permanentController.putOntoBattlefieldWithInfo(card, game, Zone.LIBRARY, source.getSourceId());
}
if (cards.size() > 0) {
permanentController.revealCards("Reweave", cards, game);
if (cardFound && card != null) {
cards.remove(card);
}
library.addAll(cards.getCards(game), game);
permanentController.shuffleLibrary(game);
}
}
return true;
}