Search in sources :

Example 26 with TurnMod

use of mage.game.turn.TurnMod 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

TurnMod (mage.game.turn.TurnMod)26 Player (mage.players.Player)13 UUID (java.util.UUID)6 Ability (mage.abilities.Ability)5 Permanent (mage.game.permanent.Permanent)5 SimpleStaticAbility (mage.abilities.common.SimpleStaticAbility)3 InfoEffect (mage.abilities.effects.common.InfoEffect)3 TargetPlayer (mage.target.TargetPlayer)3 CardInfo (mage.cards.repository.CardInfo)2 MomirEmblem (mage.game.command.emblems.MomirEmblem)2 TargetPermanent (mage.target.TargetPermanent)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 EntersBattlefieldTriggeredAbility (mage.abilities.common.EntersBattlefieldTriggeredAbility)1 Cost (mage.abilities.costs.Cost)1 RemoveAllCountersSourceCost (mage.abilities.costs.common.RemoveAllCountersSourceCost)1 ManaCosts (mage.abilities.costs.mana.ManaCosts)1 ManaCostsImpl (mage.abilities.costs.mana.ManaCostsImpl)1