Search in sources :

Example 1 with CanBeEnchantedByPredicate

use of mage.filter.predicate.permanent.CanBeEnchantedByPredicate in project mage by magefree.

the class NettlevineBlightEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    Permanent nettlevineBlight = game.getPermanent(source.getSourceId());
    Player newController = null;
    if (controller != null && nettlevineBlight != null) {
        Permanent enchantedPermanent = game.getPermanent(nettlevineBlight.getAttachedTo());
        if (enchantedPermanent != null) {
            newController = game.getPlayer(enchantedPermanent.getControllerId());
            enchantedPermanent.sacrifice(source, game);
        }
        if (newController != null) {
            FilterPermanent filter = new FilterPermanent("creature or land permanent you control");
            filter.add(Predicates.or(CardType.CREATURE.getPredicate(), CardType.LAND.getPredicate()));
            filter.add(new ControllerIdPredicate(newController.getId()));
            filter.add(new CanBeEnchantedByPredicate(nettlevineBlight));
            Target target = new TargetPermanent(filter);
            target.setNotTarget(true);
            if (target.canChoose(source.getSourceId(), newController.getId(), game) && newController.choose(outcome, target, source.getSourceId(), game)) {
                Permanent chosenPermanent = game.getPermanent(target.getFirstTarget());
                if (chosenPermanent != null) {
                    Card nettlevineBlightCard = game.getCard(source.getSourceId());
                    if (nettlevineBlightCard != null) {
                        game.getState().setValue("attachTo:" + nettlevineBlight.getId(), chosenPermanent);
                        chosenPermanent.addAttachment(nettlevineBlight.getId(), source, game);
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) FilterPermanent(mage.filter.FilterPermanent) FilterPermanent(mage.filter.FilterPermanent) Permanent(mage.game.permanent.Permanent) TargetPermanent(mage.target.TargetPermanent) ControllerIdPredicate(mage.filter.predicate.permanent.ControllerIdPredicate) TargetPermanent(mage.target.TargetPermanent) CanBeEnchantedByPredicate(mage.filter.predicate.permanent.CanBeEnchantedByPredicate) Card(mage.cards.Card)

Example 2 with CanBeEnchantedByPredicate

use of mage.filter.predicate.permanent.CanBeEnchantedByPredicate in project mage by magefree.

the class RetetherEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        Map<Card, Permanent> auraMap = new HashMap<>();
        auraCardsInGraveyard: for (Card aura : controller.getGraveyard().getCards(filterAura, source.getSourceId(), source.getControllerId(), game)) {
            if (aura != null) {
                FilterCreaturePermanent filter = new FilterCreaturePermanent("creature to enchant (" + aura.getLogName() + ')');
                filter.add(new CanBeEnchantedByPredicate(aura));
                Target target = null;
                auraLegalitySearch: for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
                    for (Permanent permanent : game.getBattlefield().getAllActivePermanents(filter, playerId, game)) {
                        if (permanent != null) {
                            for (Ability ability : aura.getAbilities()) {
                                if (ability instanceof SpellAbility) {
                                    for (Target abilityTarget : ability.getTargets()) {
                                        if (abilityTarget.possibleTargets(controller.getId(), game).contains(permanent.getId())) {
                                            target = abilityTarget.copy();
                                            break auraLegalitySearch;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                if (target != null) {
                    target.getFilter().add(CardType.CREATURE.getPredicate());
                    target.setNotTarget(true);
                    if (target.canChoose(source.getSourceId(), controller.getId(), game)) {
                        target.setTargetName("creature to enchant (" + aura.getLogName() + ')');
                        if (controller.choose(Outcome.PutCardInPlay, target, source.getSourceId(), game)) {
                            Permanent permanent = game.getPermanent(target.getFirstTarget());
                            if (permanent != null && !permanent.cantBeAttachedBy(aura, source, game, true)) {
                                auraMap.put(aura, permanent);
                                game.getState().setValue("attachTo:" + aura.getId(), permanent);
                                continue auraCardsInGraveyard;
                            }
                        }
                    }
                }
                game.informPlayers("No valid creature targets for " + aura.getLogName());
            }
        }
        controller.moveCards(auraMap.keySet(), Zone.BATTLEFIELD, source, game);
        for (Entry<Card, Permanent> entry : auraMap.entrySet()) {
            Card aura = entry.getKey();
            Permanent permanent = entry.getValue();
            if (aura != null && permanent != null) {
                permanent.addAttachment(aura.getId(), source, game);
            }
        }
        return true;
    }
    return false;
}
Also used : SpellAbility(mage.abilities.SpellAbility) Ability(mage.abilities.Ability) Player(mage.players.Player) Target(mage.target.Target) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) Permanent(mage.game.permanent.Permanent) FilterCreaturePermanent(mage.filter.common.FilterCreaturePermanent) HashMap(java.util.HashMap) SpellAbility(mage.abilities.SpellAbility) CanBeEnchantedByPredicate(mage.filter.predicate.permanent.CanBeEnchantedByPredicate) UUID(java.util.UUID) FilterCard(mage.filter.FilterCard) Card(mage.cards.Card)

Example 3 with CanBeEnchantedByPredicate

use of mage.filter.predicate.permanent.CanBeEnchantedByPredicate in project mage by magefree.

the class BreathOfFuryEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Permanent enchantment = game.getPermanent(source.getSourceId());
    if (enchantment == null) {
        return false;
    }
    Permanent enchantedCreature = game.getPermanent((UUID) getValue("TriggeringCreatureId"));
    Player controller = game.getPlayer(source.getControllerId());
    FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("creature you control that could be enchanted by " + enchantment.getName());
    filter.add(new CanBeEnchantedByPredicate(enchantment));
    Target target = new TargetControlledCreaturePermanent(filter);
    target.setNotTarget(true);
    // Commanders going to the command zone and Rest in Peace style replacement effects don't make Permanent.sacrifice return false.
    if (enchantedCreature != null && controller != null && enchantedCreature.sacrifice(source, game) && target.canChoose(source.getSourceId(), controller.getId(), game)) {
        controller.choose(outcome, target, source.getSourceId(), game);
        Permanent newCreature = game.getPermanent(target.getFirstTarget());
        boolean success = false;
        if (newCreature != null) {
            Permanent oldCreature = game.getPermanent(enchantment.getAttachedTo());
            if (oldCreature != null) {
                if (oldCreature.getId().equals(newCreature.getId())) {
                    success = true;
                } else {
                    if (oldCreature.removeAttachment(enchantment.getId(), source, game) && newCreature.addAttachment(enchantment.getId(), source, game)) {
                        game.informPlayers(enchantment.getLogName() + " was unattached from " + oldCreature.getLogName() + " and attached to " + newCreature.getLogName());
                        success = true;
                    }
                }
            } else if (newCreature.addAttachment(enchantment.getId(), source, game)) {
                game.informPlayers(enchantment.getLogName() + " was attached to " + newCreature.getLogName());
                success = true;
            }
        }
        if (success) {
            for (Permanent permanent : game.getBattlefield().getAllActivePermanents(new FilterControlledCreaturePermanent(), controller.getId(), game)) {
                permanent.untap(game);
            }
            game.getState().getTurnMods().add(new TurnMod(source.getControllerId(), TurnPhase.COMBAT, null, false));
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) Target(mage.target.Target) Permanent(mage.game.permanent.Permanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent) TargetPermanent(mage.target.TargetPermanent) FilterControlledCreaturePermanent(mage.filter.common.FilterControlledCreaturePermanent) TurnMod(mage.game.turn.TurnMod) CanBeEnchantedByPredicate(mage.filter.predicate.permanent.CanBeEnchantedByPredicate) TargetControlledCreaturePermanent(mage.target.common.TargetControlledCreaturePermanent)

Aggregations

CanBeEnchantedByPredicate (mage.filter.predicate.permanent.CanBeEnchantedByPredicate)3 Permanent (mage.game.permanent.Permanent)3 Player (mage.players.Player)3 Target (mage.target.Target)3 Card (mage.cards.Card)2 TargetPermanent (mage.target.TargetPermanent)2 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 Ability (mage.abilities.Ability)1 SpellAbility (mage.abilities.SpellAbility)1 FilterCard (mage.filter.FilterCard)1 FilterPermanent (mage.filter.FilterPermanent)1 FilterControlledCreaturePermanent (mage.filter.common.FilterControlledCreaturePermanent)1 FilterCreaturePermanent (mage.filter.common.FilterCreaturePermanent)1 ControllerIdPredicate (mage.filter.predicate.permanent.ControllerIdPredicate)1 TurnMod (mage.game.turn.TurnMod)1 TargetControlledCreaturePermanent (mage.target.common.TargetControlledCreaturePermanent)1