Skip to content

Commit

Permalink
Rework CardsLeaveGraveyardTriggeredAbility to ZoneChangeBatchEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
jimga150 committed Aug 20, 2024
1 parent 47972dc commit af0c60e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void testGraveyardExile() {
addCard(Zone.BATTLEFIELD, playerA, "Mountain", 1);
addCard(Zone.GRAVEYARD, playerA, "Living Hive");
addCard(Zone.GRAVEYARD, playerA, "Saber Ants");
addCard(Zone.GRAVEYARD, playerA, "Blasphemous Act"); // test non-permanent card type
addCard(Zone.HAND, playerA, "Rakdos Charm");

castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Rakdos Charm", true);
Expand All @@ -68,6 +69,6 @@ public void testGraveyardExile() {
execute();

assertPermanentCount(playerA, "Insect Token", 1);
assertCounterCount(playerA, "Insect Token", CounterType.P1P1, 8);
assertCounterCount(playerA, "Insect Token", CounterType.P1P1, 9);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import mage.filter.StaticFilters;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.events.ZoneChangeGroupEvent;
import mage.game.events.ZoneChangeBatchEvent;

import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -38,19 +38,23 @@ private CardsLeaveGraveyardTriggeredAbility(final CardsLeaveGraveyardTriggeredAb

@Override
public boolean checkEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.ZONE_CHANGE_GROUP;
return event.getType() == GameEvent.EventType.ZONE_CHANGE_BATCH;
}

@Override
public boolean checkTrigger(GameEvent event, Game game) {
ZoneChangeGroupEvent zEvent = (ZoneChangeGroupEvent) event;
if (zEvent == null || Zone.GRAVEYARD != zEvent.getFromZone()
|| Zone.GRAVEYARD == zEvent.getToZone() || zEvent.getCards() == null){
ZoneChangeBatchEvent zEvent = (ZoneChangeBatchEvent) event;
if (zEvent == null){
return false;
}
Set<Card> cards = zEvent.getCards()
Set<Card> cards = zEvent.getEvents()
.stream()
.filter(Objects::nonNull)
.filter(ev -> ev.getFromZone() == Zone.GRAVEYARD)
.filter(ev -> ev.getToZone() != Zone.GRAVEYARD)
.map(GameEvent::getTargetId)
.map(game::getCard)
.filter(Objects::nonNull)
.filter(card -> filter.match(card, getControllerId(), this, game))
.filter(card -> this.isControlledBy(card.getOwnerId()))
.collect(Collectors.toSet());
Expand Down
8 changes: 8 additions & 0 deletions Mage/src/main/java/mage/game/ZonesHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import mage.constants.Zone;
import mage.filter.FilterCard;
import mage.game.command.Commander;
import mage.game.events.ZoneChangeBatchEvent;
import mage.game.events.ZoneChangeEvent;
import mage.game.events.ZoneChangeGroupEvent;
import mage.game.permanent.Permanent;
Expand All @@ -27,6 +28,7 @@ public final class ZonesHandler {
public static boolean cast(ZoneChangeInfo info, Ability source, Game game) {
if (maybeRemoveFromSourceZone(info, game, source)) {
placeInDestinationZone(info, 0, source, game);

// create a group zone change event if a card is moved to stack for casting (it's always only one card, but some effects check for group events (one or more xxx))
Set<Card> cards = new HashSet<>();
Set<PermanentToken> tokens = new HashSet<>();
Expand All @@ -44,6 +46,12 @@ public static boolean cast(ZoneChangeInfo info, Ability source, Game game) {
info.event.getPlayerId(),
info.event.getFromZone(),
info.event.getToZone()));

// Fire batch event for cards moving to stack as well
ZoneChangeBatchEvent batchEvent = new ZoneChangeBatchEvent();
batchEvent.addEvent(info.event);
game.fireEvent(batchEvent);

// normal movement
game.fireEvent(info.event);
return true;
Expand Down

0 comments on commit af0c60e

Please sign in to comment.