mirror of
https://github.com/magefree/mage.git
synced 2025-12-26 13:32:06 -08:00
simplified and consolidated effects which check cards put into graveyards from the battlefield
This commit is contained in:
parent
c01e1cd133
commit
35be23537f
15 changed files with 311 additions and 594 deletions
|
|
@ -1,53 +1,89 @@
|
|||
package mage.abilities.effects.common;
|
||||
|
||||
import java.util.UUID;
|
||||
import mage.abilities.Ability;
|
||||
import mage.abilities.Mode;
|
||||
import mage.abilities.effects.OneShotEffect;
|
||||
import mage.constants.Outcome;
|
||||
import mage.constants.TargetController;
|
||||
import mage.constants.Zone;
|
||||
import mage.filter.FilterCard;
|
||||
import mage.game.Game;
|
||||
import mage.players.Player;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author LevelX2
|
||||
*/
|
||||
public class ReturnToHandFromGraveyardAllEffect extends OneShotEffect {
|
||||
|
||||
private final FilterCard filter;
|
||||
private final TargetController targetController;
|
||||
|
||||
public ReturnToHandFromGraveyardAllEffect(FilterCard filter) {
|
||||
super(Outcome.ReturnToHand);
|
||||
this.filter = filter;
|
||||
staticText = "Each player returns all " + filter.getMessage() + " from their graveyard to their hand";
|
||||
this(filter, TargetController.EACH_PLAYER);
|
||||
}
|
||||
|
||||
public ReturnToHandFromGraveyardAllEffect(final ReturnToHandFromGraveyardAllEffect effect) {
|
||||
public ReturnToHandFromGraveyardAllEffect(FilterCard filter, TargetController targetController) {
|
||||
super(Outcome.ReturnToHand);
|
||||
this.filter = filter;
|
||||
this.targetController = targetController;
|
||||
}
|
||||
|
||||
private ReturnToHandFromGraveyardAllEffect(final ReturnToHandFromGraveyardAllEffect effect) {
|
||||
super(effect);
|
||||
this.filter = effect.filter;
|
||||
this.targetController = effect.targetController;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean apply(Game game, Ability source) {
|
||||
Player controller = game.getPlayer(source.getControllerId());
|
||||
if (controller != null) {
|
||||
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
|
||||
Player player = game.getPlayer(playerId);
|
||||
if (player != null) {
|
||||
player.moveCards(player.getGraveyard()
|
||||
.getCards(filter, source.getSourceId(), player.getId(), game),
|
||||
Zone.HAND, source, game);
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
if (controller == null) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
|
||||
switch (targetController) {
|
||||
case ANY:
|
||||
case EACH_PLAYER:
|
||||
break;
|
||||
case OPPONENT:
|
||||
if (!controller.hasOpponent(playerId, game)) {
|
||||
continue;
|
||||
}
|
||||
case YOU:
|
||||
if (!controller.getId().equals(playerId)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
Player player = game.getPlayer(playerId);
|
||||
if (player == null) {
|
||||
continue;
|
||||
}
|
||||
player.moveCards(player.getGraveyard().getCards(
|
||||
filter, source.getSourceId(), player.getId(), game
|
||||
), Zone.HAND, source, game);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnToHandFromGraveyardAllEffect copy() {
|
||||
return new ReturnToHandFromGraveyardAllEffect(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText(Mode mode) {
|
||||
if(staticText!=null&&!staticText.isEmpty()){
|
||||
return staticText;
|
||||
}
|
||||
String rule="";
|
||||
switch (targetController){
|
||||
case EACH_PLAYER:
|
||||
rule+="each player";break;
|
||||
case OPPONENT:rule+="opponent";break;
|
||||
case YOU:
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
package mage.filter.predicate.card;
|
||||
|
||||
import mage.cards.Card;
|
||||
import mage.filter.predicate.Predicate;
|
||||
import mage.game.Game;
|
||||
import mage.watchers.common.CardsPutIntoGraveyardWatcher;
|
||||
|
||||
/**
|
||||
* @author TheElk801
|
||||
*/
|
||||
public enum PutIntoGraveFromBattlefieldThisTurnPredicate implements Predicate<Card> {
|
||||
instance;
|
||||
|
||||
@Override
|
||||
public boolean apply(Card input, Game game) {
|
||||
CardsPutIntoGraveyardWatcher watcher = game.getState().getWatcher(CardsPutIntoGraveyardWatcher.class);
|
||||
return watcher != null && watcher.checkCardFromBattlefield(input, game);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,7 @@
|
|||
|
||||
package mage.watchers.common;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import mage.MageObjectReference;
|
||||
import mage.cards.Card;
|
||||
import mage.constants.WatcherScope;
|
||||
import mage.constants.Zone;
|
||||
import mage.game.Game;
|
||||
|
|
@ -15,6 +9,9 @@ import mage.game.events.GameEvent;
|
|||
import mage.game.events.ZoneChangeEvent;
|
||||
import mage.watchers.Watcher;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Counts amount of cards put into graveyards of players during the current
|
||||
* turn. Also the UUIDs of cards that went to graveyard from Battlefield this
|
||||
|
|
@ -33,19 +30,17 @@ public class CardsPutIntoGraveyardWatcher extends Watcher {
|
|||
|
||||
@Override
|
||||
public void watch(GameEvent event, Game game) {
|
||||
if (event.getType() == GameEvent.EventType.UNTAP_STEP_PRE) {
|
||||
reset();
|
||||
if (event.getType() != GameEvent.EventType.ZONE_CHANGE
|
||||
|| ((ZoneChangeEvent) event).getToZone() != Zone.GRAVEYARD) {
|
||||
return;
|
||||
}
|
||||
if (event.getType() == GameEvent.EventType.ZONE_CHANGE && ((ZoneChangeEvent) event).getToZone() == Zone.GRAVEYARD) {
|
||||
UUID playerId = event.getPlayerId();
|
||||
if (playerId != null && game.getCard(event.getTargetId()) != null) {
|
||||
amountOfCardsThisTurn.putIfAbsent(playerId, 0);
|
||||
amountOfCardsThisTurn.compute(playerId, (k, amount) -> amount += 1);
|
||||
|
||||
if (((ZoneChangeEvent) event).getFromZone() == Zone.BATTLEFIELD) {
|
||||
cardsPutToGraveyardFromBattlefield.add(new MageObjectReference(event.getTargetId(), game));
|
||||
}
|
||||
}
|
||||
UUID playerId = event.getPlayerId();
|
||||
if (playerId == null || game.getCard(event.getTargetId()) == null) {
|
||||
return;
|
||||
}
|
||||
amountOfCardsThisTurn.compute(playerId, (k, amount) -> amount == null ? 1 : Integer.sum(amount, 1));
|
||||
if (((ZoneChangeEvent) event).getFromZone() == Zone.BATTLEFIELD) {
|
||||
cardsPutToGraveyardFromBattlefield.add(new MageObjectReference(((ZoneChangeEvent) event).getTarget(), game, 1));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -53,8 +48,12 @@ public class CardsPutIntoGraveyardWatcher extends Watcher {
|
|||
return amountOfCardsThisTurn.getOrDefault(playerId, 0);
|
||||
}
|
||||
|
||||
public Set<MageObjectReference> getCardsPutToGraveyardFromBattlefield() {
|
||||
return cardsPutToGraveyardFromBattlefield;
|
||||
public Set<Card> getCardsPutToGraveyardFromBattlefield(Game game) {
|
||||
return cardsPutToGraveyardFromBattlefield.stream().map(mor -> mor.getCard(game)).filter(Objects::nonNull).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public boolean checkCardFromBattlefield(Card card, Game game) {
|
||||
return cardsPutToGraveyardFromBattlefield.stream().anyMatch(mor -> mor.refersTo(card, game));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue