Merge pull request #8394 from weirddan455/ward

Fixed Ward countering wrong ability (fixes #8378)
This commit is contained in:
Oleg Agafonov 2021-10-19 10:32:03 +04:00 committed by GitHub
commit fa79c1eba5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 2 deletions

View file

@ -0,0 +1,35 @@
package org.mage.test.cards.abilities.keywords;
import mage.constants.PhaseStep;
import mage.constants.Zone;
import org.junit.Test;
import org.mage.test.serverside.base.CardTestPlayerBase;
/**
*
* @author weirddan455
*/
public class WardTest extends CardTestPlayerBase {
@Test
public void wardMultipleAbilities() {
addCard(Zone.HAND, playerA, "Solitude"); // https://github.com/magefree/mage/issues/8378 Test that ward counters correct ability
addCard(Zone.HAND, playerA, "Healer's Hawk"); // Card to pitch to Solitude
addCard(Zone.BATTLEFIELD, playerB, "Waterfall Aerialist"); // Flying, Ward 2
setStrictChooseMode(true);
castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Solitude");
setChoice(playerA, "Yes"); // Use alternate casting cost
setChoice(playerA, "Healer's Hawk");
setChoice(playerA, "When {this} enters the battlefield, exile up to one other target creature"); // Put exile trigger on the stack first (evoke trigger will resolve first)
addTarget(playerA, "Waterfall Aerialist");
setChoice(playerA, "No"); // Do not pay Ward cost
setStopAt(1, PhaseStep.END_TURN);
execute();
assertExileCount("Healer's Hawk", 1);
assertGraveyardCount(playerA, "Solitude", 1);
assertPermanentCount(playerB, "Waterfall Aerialist", 1);
assertAllCommandsUsed();
}
}

View file

@ -8,6 +8,7 @@ import mage.constants.Zone;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.stack.StackObject;
import mage.target.Target;
import mage.target.targetpointer.FixedTarget;
import mage.util.CardUtil;
@ -33,16 +34,29 @@ public class WardAbility extends TriggeredAbilityImpl {
return event.getType() == GameEvent.EventType.TARGETED;
}
private StackObject getTargetingObject(GameEvent event, Game game) {
for (StackObject stackObject : game.getStack()) {
if (stackObject.getId().equals(event.getSourceId()) || stackObject.getSourceId().equals(event.getSourceId())) {
for (Target target : stackObject.getStackAbility().getTargets()) {
if (target.contains(getSourceId())) {
return stackObject;
}
}
}
}
return null;
}
@Override
public boolean checkTrigger(GameEvent event, Game game) {
if (!getSourceId().equals(event.getTargetId())) {
return false;
}
StackObject stackObject = game.getStack().getStackObject(event.getSourceId());
StackObject stackObject = getTargetingObject(event, game);
if (stackObject == null || !game.getOpponents(getControllerId()).contains(stackObject.getControllerId())) {
return false;
}
getEffects().setTargetPointer(new FixedTarget(event.getSourceId(), game));
getEffects().setTargetPointer(new FixedTarget(stackObject.getId(), game));
return true;
}