use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.
the class ShimianSpecterEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player targetPlayer = game.getPlayer(getTargetPointer().getFirst(game, source));
Player controller = game.getPlayer(source.getControllerId());
MageObject sourceObject = source.getSourceObject(game);
if (targetPlayer != null && sourceObject != null && controller != null) {
// reveal hand of target player
targetPlayer.revealCards(sourceObject.getName(), targetPlayer.getHand(), game);
// You choose a nonland card from it
TargetCard target = new TargetCard(Zone.HAND, new FilterNonlandCard());
target.setNotTarget(true);
Card chosenCard = null;
if (target.canChoose(source.getSourceId(), controller.getId(), game) && controller.chooseTarget(Outcome.Benefit, targetPlayer.getHand(), target, source, game)) {
chosenCard = game.getCard(target.getFirstTarget());
}
// Exile all cards with the same name
// Building a card filter with the name
FilterCard filterNamedCards = new FilterCard();
// so no card matches
String nameToSearch = "---";
if (chosenCard != null) {
nameToSearch = CardUtil.getCardNameForSameNameSearch(chosenCard);
}
filterNamedCards.add(new NamePredicate(nameToSearch));
// search cards in graveyard
if (chosenCard != null) {
for (Card checkCard : targetPlayer.getGraveyard().getCards(game)) {
if (checkCard.getName().equals(chosenCard.getName())) {
controller.moveCardToExileWithInfo(checkCard, null, "", source, game, Zone.GRAVEYARD, true);
}
}
// search cards in hand
TargetCard targetHandCards = new TargetCard(0, Integer.MAX_VALUE, Zone.HAND, filterNamedCards);
controller.chooseTarget(Outcome.Benefit, targetPlayer.getHand(), targetHandCards, source, game);
for (UUID cardId : targetHandCards.getTargets()) {
Card card = game.getCard(cardId);
if (card != null) {
controller.moveCardToExileWithInfo(card, null, "", source, game, Zone.HAND, true);
}
}
}
// that player's library and have that player shuffle it.
if (chosenCard != null || controller.chooseUse(outcome, "Search library anyway?", source, game)) {
TargetCardInLibrary targetCardsLibrary = new TargetCardInLibrary(0, Integer.MAX_VALUE, filterNamedCards);
controller.searchLibrary(targetCardsLibrary, source, game, targetPlayer.getId());
for (UUID cardId : targetCardsLibrary.getTargets()) {
Card card = game.getCard(cardId);
if (card != null) {
controller.moveCardToExileWithInfo(card, null, "", source, game, Zone.LIBRARY, true);
}
}
targetPlayer.shuffleLibrary(source, game);
}
return true;
}
return false;
}
use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.
the class TwinningGlassEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
List<Spell> spells = new ArrayList<>();
Permanent twinningGlass = game.getPermanent(source.getSourceId());
Player controller = game.getPlayer(source.getControllerId());
SpellsCastWatcher watcher = game.getState().getWatcher(SpellsCastWatcher.class);
if (twinningGlass == null) {
twinningGlass = (Permanent) game.getLastKnownInformation(source.getSourceId(), Zone.BATTLEFIELD);
}
if (twinningGlass != null && controller != null && watcher != null) {
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
if (watcher.getSpellsCastThisTurn(playerId) != null) {
for (Spell spell : watcher.getSpellsCastThisTurn(playerId)) {
spells.add(spell);
}
}
}
if (spells.isEmpty()) {
return false;
}
List<NamePredicate> predicates = spells.stream().map(Spell::getName).filter(Objects::nonNull).filter(s -> !s.isEmpty()).map(NamePredicate::new).collect(Collectors.toList());
FilterNonlandCard filterCard = new FilterNonlandCard("nonland card that was cast this turn");
filterCard.add(Predicates.or(predicates));
TargetCard target = new TargetCard(0, 1, Zone.HAND, filterCard);
target.withChooseHint("free cast");
if (controller.choose(Outcome.PlayForFree, controller.getHand(), target, game)) {
Card chosenCard = game.getCard(target.getFirstTarget());
if (chosenCard != null) {
game.getState().setValue("PlayFromNotOwnHandZone" + chosenCard.getId(), Boolean.TRUE);
Boolean cardWasCast = controller.cast(controller.chooseAbilityForCast(chosenCard, game, true), game, true, new ApprovingObject(source, game));
game.getState().setValue("PlayFromNotOwnHandZone" + chosenCard.getId(), null);
return cardWasCast;
}
}
}
return false;
}
use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.
the class WinnowEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Permanent target = game.getPermanent(source.getFirstTarget());
if (target != null) {
FilterPermanent filter = new FilterPermanent();
filter.add(new NamePredicate(target.getName()));
if (new PermanentsOnBattlefieldCount(filter).calculate(game, source, this) > 1) {
super.apply(game, source);
}
return true;
}
return false;
}
use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.
the class GainAbilityDependenciesTest method test_GenerationByFilters.
@Test
public void test_GenerationByFilters() {
// auto-dependency must find subtype predicate and add dependecy on it
FilterPermanent filterEmpty = new FilterPermanent("empty");
FilterPermanent filterSubtype = new FilterPermanent(SubType.HUMAN, "single");
FilterPermanent filterOr = new FilterPermanent("or");
filterOr.add(Predicates.or(SubType.HUMAN.getPredicate(), SubType.ORC.getPredicate()));
FilterPermanent filterTree = new FilterPermanent("tree");
filterTree.add(Predicates.and(new NamePredicate("test"), Predicates.or(SubType.HUMAN.getPredicate(), SubType.ORC.getPredicate())));
FilterPermanent filterNotTree = new FilterPermanent("tree");
filterNotTree.add(Predicates.not(Predicates.or(SubType.HUMAN.getPredicate(), SubType.ORC.getPredicate())));
ContinuousEffectImpl effectEmpty = new GainAbilityAllEffect(HasteAbility.getInstance(), Duration.EndOfTurn, filterEmpty);
ContinuousEffectImpl effectSubtype = new GainAbilityAllEffect(HasteAbility.getInstance(), Duration.EndOfTurn, filterSubtype);
ContinuousEffectImpl effectOr = new GainAbilityAllEffect(HasteAbility.getInstance(), Duration.EndOfTurn, filterOr);
ContinuousEffectImpl effectTree = new GainAbilityAllEffect(HasteAbility.getInstance(), Duration.EndOfTurn, filterTree);
ContinuousEffectImpl effectNotTree = new GainAbilityAllEffect(HasteAbility.getInstance(), Duration.EndOfTurn, filterNotTree);
Assert.assertFalse("must haven't depends with empty filter", effectEmpty.getDependedToTypes().contains(DependencyType.AddingCreatureType));
Assert.assertTrue("must have depend from subtype predicate", effectSubtype.getDependedToTypes().contains(DependencyType.AddingCreatureType));
Assert.assertTrue("must have depend from or predicate", effectOr.getDependedToTypes().contains(DependencyType.AddingCreatureType));
Assert.assertTrue("must have depend from tree predicate", effectTree.getDependedToTypes().contains(DependencyType.AddingCreatureType));
Assert.assertTrue("must have depend from not-tree predicate", effectNotTree.getDependedToTypes().contains(DependencyType.AddingCreatureType));
}
use of mage.filter.predicate.mageobject.NamePredicate in project mage by magefree.
the class InfernalTutorEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
MageObject sourceObject = game.getObject(source.getSourceId());
if (controller != null && sourceObject != null) {
if (!controller.getHand().isEmpty()) {
Card cardToReveal = null;
if (controller.getHand().size() > 1) {
Target target = new TargetCardInHand(StaticFilters.FILTER_CARD);
target.setNotTarget(true);
if (controller.chooseTarget(outcome, target, source, game)) {
cardToReveal = game.getCard(target.getFirstTarget());
}
} else {
cardToReveal = controller.getHand().getRandom(game);
}
FilterCard filterCard;
if (cardToReveal != null) {
controller.revealCards("from hand :" + sourceObject.getName(), new CardsImpl(cardToReveal), game);
String nameToSearch = CardUtil.getCardNameForSameNameSearch(cardToReveal);
filterCard = new FilterCard("card named " + nameToSearch);
filterCard.add(new NamePredicate(nameToSearch));
} else {
filterCard = new FilterCard();
}
return new SearchLibraryPutInHandEffect(new TargetCardInLibrary(filterCard), true, true).apply(game, source);
}
return true;
}
return false;
}
Aggregations