Search in sources :

Example 11 with PreventionEffectData

use of mage.abilities.effects.PreventionEffectData in project mage by magefree.

the class TurnTheTablesEffect method preventDamageAction.

@Override
protected PreventionEffectData preventDamageAction(GameEvent event, Ability source, Game game) {
    PreventionEffectData preventionEffectData = super.preventDamageAction(event, source, game);
    int damage = preventionEffectData.getPreventedDamage();
    if (damage > 0) {
        Permanent attackingCreature = game.getPermanent(getTargetPointer().getFirst(game, source));
        if (attackingCreature != null) {
            attackingCreature.damage(damage, source.getSourceId(), source, game, false, true);
        }
    }
    return preventionEffectData;
}
Also used : PreventionEffectData(mage.abilities.effects.PreventionEffectData) Permanent(mage.game.permanent.Permanent)

Example 12 with PreventionEffectData

use of mage.abilities.effects.PreventionEffectData in project mage by magefree.

the class GameImpl method preventDamage.

@Override
public PreventionEffectData preventDamage(GameEvent event, Ability source, Game game, int amountToPrevent) {
    PreventionEffectData result = new PreventionEffectData(amountToPrevent);
    if (!event.getFlag()) {
        // damage is not preventable
        return result;
    }
    if (!(event instanceof DamageEvent)) {
        result.setError(true);
        return result;
    }
    DamageEvent damageEvent = (DamageEvent) event;
    GameEvent preventEvent = new PreventDamageEvent(damageEvent.getTargetId(), damageEvent.getSourceId(), source, source.getControllerId(), damageEvent.getAmount(), damageEvent.isCombatDamage());
    if (game.replaceEvent(preventEvent)) {
        result.setReplaced(true);
        return result;
    }
    if (event.getAmount() > amountToPrevent) {
        result.setPreventedDamage(amountToPrevent);
        damageEvent.setAmount(event.getAmount() - amountToPrevent);
    } else {
        result.setPreventedDamage(event.getAmount());
        damageEvent.setAmount(0);
    }
    if (amountToPrevent != Integer.MAX_VALUE) {
        // set remaining amount
        result.setRemainingAmount(amountToPrevent - result.getPreventedDamage());
    }
    MageObject damageSource = game.getObject(damageEvent.getSourceId());
    MageObject preventionSource = game.getObject(source.getSourceId());
    if (damageSource != null && preventionSource != null) {
        MageObject targetObject = game.getObject(event.getTargetId());
        String targetName = "";
        if (targetObject == null) {
            Player targetPlayer = game.getPlayer(event.getTargetId());
            if (targetPlayer != null) {
                targetName = targetPlayer.getLogName();
            }
        } else {
            targetName = targetObject.getLogName();
        }
        if (!game.isSimulation()) {
            StringBuilder message = new StringBuilder(preventionSource.getLogName()).append(": Prevented ");
            message.append(result.getPreventedDamage()).append(" damage from ").append(damageSource.getLogName());
            if (!targetName.isEmpty()) {
                message.append(" to ").append(targetName);
            }
            game.informPlayers(message.toString());
        }
    }
    game.fireEvent(new PreventedDamageEvent(damageEvent.getTargetId(), source.getSourceId(), source, source.getControllerId(), result.getPreventedDamage()));
    return result;
}
Also used : PreventionEffectData(mage.abilities.effects.PreventionEffectData) TargetPlayer(mage.target.TargetPlayer) Player(mage.players.Player) MageObject(mage.MageObject)

Example 13 with PreventionEffectData

use of mage.abilities.effects.PreventionEffectData in project mage by magefree.

the class InterventionPactPreventDamageEffect method replaceEvent.

@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
    PreventionEffectData preventEffectData = preventDamageAction(event, source, game);
    if (preventEffectData.getPreventedDamage() > 0) {
        used = true;
        Player player = game.getPlayer(source.getControllerId());
        if (player != null) {
            player.gainLife(preventEffectData.getPreventedDamage(), game, source);
        }
    }
    return false;
}
Also used : PreventionEffectData(mage.abilities.effects.PreventionEffectData) Player(mage.players.Player)

Example 14 with PreventionEffectData

use of mage.abilities.effects.PreventionEffectData in project mage by magefree.

the class DivineDeflectionPreventDamageTargetEffect method replaceEvent.

@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
    /*
        If damage is dealt to multiple permanents you control, or is dealt to you and at least
        one permanent you control, you choose which of that damage to prevent if the chosen value
        for X won't prevent all the damage. For example, if 3 damage would be dealt to you and to
        each of two creatures you control, and Divine Deflection will prevent the next 3 damage,
        you might choose to prevent the next 2 damage it would deal to you and the next 1 damage
        it would deal to one of the creatures, among other choices. You don't decide until the
        point at which the damage would be dealt.
        TODO: Support to select which damage to prevent
        */
    PreventionEffectData preventionData = preventDamageAction(event, source, game);
    /*
        Divine Deflection's effect is not a redirection effect. If it prevents damage,
        Divine Deflection (not the original source) deals damage to the targeted creature
        or player as part of that prevention effect. Divine Deflection is the source of
        the new damage, so the characteristics of the original source (such as its color,
        or whether it had lifelink or deathtouch) don't affect this damage. The new damage
        is not combat damage, even if the prevented damage was. Since you control the source
        of the new damage, if you targeted an opponent with Divine Deflection, you may
        have Divine Deflection deal its damage to a planeswalker that opponent controls.
        */
    // deal damage now
    int prevented = preventionData.getPreventedDamage();
    if (prevented > 0) {
        UUID dealDamageTo = source.getFirstTarget();
        /*
          	Whether the targeted creature or player is still a legal target is not checked after
            Divine Deflection resolves. For example, if a creature targeted by Divine Deflection
            gains shroud after Divine Deflection resolves, Divine Deflection can still deal damage
            to that creature.
            */
        Permanent permanent = game.getPermanent(dealDamageTo);
        if (permanent != null) {
            game.informPlayers("Dealing " + prevented + " to " + permanent.getName() + " instead");
            permanent.damage(prevented, source.getSourceId(), source, game, false, true);
        }
        Player player = game.getPlayer(dealDamageTo);
        if (player != null) {
            game.informPlayers("Dealing " + prevented + " to " + player.getLogName() + " instead");
            player.damage(prevented, source.getSourceId(), source, game);
        }
    }
    return false;
}
Also used : PreventionEffectData(mage.abilities.effects.PreventionEffectData) Player(mage.players.Player) Permanent(mage.game.permanent.Permanent) UUID(java.util.UUID)

Example 15 with PreventionEffectData

use of mage.abilities.effects.PreventionEffectData in project mage by magefree.

the class PurityEffect method replaceEvent.

@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
    PreventionEffectData preventionData = preventDamageAction(event, source, game);
    Player player = game.getPlayer(source.getControllerId());
    if (player != null) {
        player.gainLife(preventionData.getPreventedDamage(), game, source);
    }
    return true;
}
Also used : PreventionEffectData(mage.abilities.effects.PreventionEffectData) Player(mage.players.Player)

Aggregations

PreventionEffectData (mage.abilities.effects.PreventionEffectData)20 Player (mage.players.Player)17 Permanent (mage.game.permanent.Permanent)8 MageObject (mage.MageObject)6 UUID (java.util.UUID)5 Spell (mage.game.stack.Spell)3 Ability (mage.abilities.Ability)2 Card (mage.cards.Card)2 TargetCreaturePermanent (mage.target.common.TargetCreaturePermanent)2 CommandObject (mage.game.command.CommandObject)1 DamageEvent (mage.game.events.DamageEvent)1 TargetPlayer (mage.target.TargetPlayer)1