[filters] Replaced conditions in StackObject filters with Predicates

This commit is contained in:
North 2012-07-17 18:58:18 +03:00
parent 8598f9ead1
commit b7f57c8a23
9 changed files with 63 additions and 167 deletions

View file

@ -27,7 +27,6 @@
*/
package mage.sets.avacynrestored;
import mage.Constants;
import mage.Constants.CardType;
import mage.Constants.Rarity;
import mage.abilities.effects.common.CounterTargetEffect;
@ -35,7 +34,7 @@ import mage.cards.CardImpl;
import mage.filter.FilterSpell;
import mage.game.Game;
import mage.game.stack.Spell;
import mage.game.stack.StackObject;
import mage.filter.predicate.Predicate;
import mage.target.TargetSpell;
import mage.watchers.common.CastSpellLastTurnWatcher;
@ -47,6 +46,12 @@ import java.util.UUID;
*/
public class SecondGuess extends CardImpl<SecondGuess> {
private static final FilterSpell filter = new FilterSpell("spell that's the second spell cast this turn");
static {
filter.add(new SecondSpellPredicate());
}
public SecondGuess(UUID ownerId) {
super(ownerId, 74, "Second Guess", Rarity.UNCOMMON, new CardType[]{CardType.INSTANT}, "{1}{U}");
this.expansionSetCode = "AVR";
@ -55,7 +60,7 @@ public class SecondGuess extends CardImpl<SecondGuess> {
// Counter target spell that's the second spell cast this turn.
this.getSpellAbility().addEffect(new CounterTargetEffect());
this.getSpellAbility().addTarget(new TargetSpell(new FilterSecondSpell()));
this.getSpellAbility().addTarget(new TargetSpell(filter));
}
public SecondGuess(final SecondGuess card) {
@ -68,65 +73,21 @@ public class SecondGuess extends CardImpl<SecondGuess> {
}
}
class FilterSecondSpell extends FilterSpell {
public FilterSecondSpell() {
super("spell that's the second spell cast this turn");
}
public FilterSecondSpell(final FilterSecondSpell filter) {
super(filter);
}
class SecondSpellPredicate implements Predicate<Spell> {
@Override
public boolean match(StackObject spell, Game game) {
if (!super.match(spell, game))
return notFilter;
public boolean apply(Spell input, Game game) {
CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get("CastSpellLastTurnWatcher");
if (spell instanceof Spell) {
CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get("CastSpellLastTurnWatcher");
int index = watcher.getSpellOrder((Spell)spell);
if (index == 2) {
return !notFilter;
}
if (watcher.getSpellOrder(input) == 2) {
return true;
}
return notFilter;
return false;
}
@Override
public boolean match(StackObject spell, UUID playerId, Game game) {
if (!super.match(spell, playerId, game))
return notFilter;
if (spell instanceof Spell) {
CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get("CastSpellLastTurnWatcher");
int index = watcher.getSpellOrder((Spell)spell);
if (index == 2) {
return notFilter;
}
}
return !notFilter;
public String toString() {
return "SecondSpellThisTurn";
}
@Override
public FilterSecondSpell copy() {
return new FilterSecondSpell(this);
}
@Override
public void setFromZone(Constants.Zone fromZone) {
this.fromZone = fromZone;
}
@Override
public void setNotFromZone(boolean notFromZone) {
this.notFromZone = notFromZone;
}
}

View file

@ -39,6 +39,7 @@ import mage.cards.CardImpl;
import mage.filter.FilterSpell;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.CardTypePredicate;
import mage.filter.predicate.permanent.ControllerPredicate;
import mage.game.Game;
import mage.game.stack.Spell;
import mage.target.TargetSpell;
@ -55,7 +56,7 @@ public class IncreasingVengeance extends CardImpl<IncreasingVengeance> {
filter.add(Predicates.or(
new CardTypePredicate(CardType.INSTANT),
new CardTypePredicate(CardType.SORCERY)));
filter.setTargetController(Constants.TargetController.YOU);
filter.add(new ControllerPredicate(Constants.TargetController.YOU));
}
public IncreasingVengeance(UUID ownerId) {

View file

@ -28,13 +28,16 @@
package mage.sets.darkascension;
import java.util.UUID;
import mage.Constants;
import mage.Constants.CardType;
import mage.Constants.Rarity;
import mage.Constants.Zone;
import mage.abilities.common.SpellCastTriggeredAbility;
import mage.abilities.effects.common.DrawCardControllerEffect;
import mage.cards.CardImpl;
import mage.filter.FilterSpell;
import mage.filter.predicate.Predicate;
import mage.game.Game;
import mage.game.stack.Spell;
/**
*
@ -45,7 +48,7 @@ public class SecretsOfTheDead extends CardImpl<SecretsOfTheDead> {
private final static FilterSpell filter = new FilterSpell("a spell from your graveyard");
static {
filter.setFromZone(Constants.Zone.GRAVEYARD);
filter.add(new SpellZonePredicate(Zone.GRAVEYARD));
}
public SecretsOfTheDead(UUID ownerId) {
@ -67,3 +70,22 @@ public class SecretsOfTheDead extends CardImpl<SecretsOfTheDead> {
return new SecretsOfTheDead(this);
}
}
class SpellZonePredicate implements Predicate<Spell> {
private final Zone zone;
public SpellZonePredicate(Zone zone) {
this.zone = zone;
}
@Override
public boolean apply(Spell input, Game game) {
return input.getFromZone().match(zone);
}
@Override
public String toString() {
return "SpellZone(" + zone + ')';
}
}

View file

@ -39,6 +39,7 @@ import mage.abilities.keyword.HexproofAbility;
import mage.abilities.keyword.LeylineAbility;
import mage.cards.CardImpl;
import mage.filter.FilterStackObject;
import mage.filter.predicate.permanent.ControllerPredicate;
/**
*
@ -49,7 +50,7 @@ public class LeylineOfSanctity extends CardImpl<LeylineOfSanctity> {
private static final FilterStackObject filter = new FilterStackObject("spells or abilities your opponents control");
static {
filter.setTargetController(TargetController.OPPONENT);
filter.add(new ControllerPredicate(TargetController.OPPONENT));
}
public LeylineOfSanctity(UUID ownerId) {

View file

@ -39,6 +39,7 @@ import mage.abilities.effects.ReplacementEffectImpl;
import mage.abilities.effects.common.RegenerateTargetEffect;
import mage.cards.CardImpl;
import mage.filter.FilterStackObject;
import mage.filter.predicate.permanent.ControllerPredicate;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
@ -55,7 +56,7 @@ public class Asceticism extends CardImpl<Asceticism> {
private static final FilterStackObject filter = new FilterStackObject("spells or abilities your opponents control");
static {
filter.setTargetController(Constants.TargetController.OPPONENT);
filter.add(new ControllerPredicate(Constants.TargetController.OPPONENT));
}
public Asceticism(UUID ownerId) {