[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

@ -86,18 +86,15 @@ public class CantCounterControlledEffect extends ReplacementEffectImpl<CantCount
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
if (event.getType() == EventType.COUNTER) {
filterTarget.getControllerId().clear();
filterTarget.getControllerId().add(source.getControllerId());
Spell spell = game.getStack().getSpell(event.getTargetId());
if (spell != null) {
if (filterTarget.match(spell, game)) {
if (filterSource == null)
if (spell != null && spell.getControllerId().equals(source.getControllerId())
&& filterTarget.match(spell, game)) {
if (filterSource == null)
return true;
else {
MageObject sourceObject = game.getObject(source.getSourceId());
if (sourceObject != null && filterSource.match(sourceObject, game)) {
return true;
else {
MageObject sourceObject = game.getObject(source.getSourceId());
if (sourceObject != null && filterSource.match(sourceObject, game)) {
return true;
}
}
}
}

View file

@ -5,6 +5,7 @@ import mage.Constants;
import mage.abilities.common.SimpleStaticAbility;
import mage.abilities.effects.common.CantTargetSourceEffect;
import mage.filter.FilterStackObject;
import mage.filter.predicate.permanent.ControllerPredicate;
/**
* Hexproof
@ -19,7 +20,7 @@ public class HexproofAbility extends SimpleStaticAbility {
static {
filter = new FilterStackObject("spells or abilities your opponents control");
filter.setTargetController(Constants.TargetController.OPPONENT);
filter.add(new ControllerPredicate(Constants.TargetController.OPPONENT));
fINSTANCE = new HexproofAbility();
}

View file

@ -28,21 +28,13 @@
package mage.filter;
import mage.Constants.Zone;
import mage.game.Game;
import mage.game.stack.Spell;
import mage.game.stack.StackObject;
import java.util.UUID;
/**
*
* @author BetaSteward_at_googlemail.com
* @author North
*/
public class FilterSpell extends FilterStackObject {
protected Zone fromZone = Zone.ALL;
protected boolean notFromZone = false;
public class FilterSpell extends FilterStackObject<Spell> {
public FilterSpell() {
super("spell");
@ -54,43 +46,10 @@ public class FilterSpell extends FilterStackObject {
public FilterSpell(final FilterSpell filter) {
super(filter);
for (Object cId: filter.getControllerId()) {
this.controllerId.add((UUID)cId);
}
this.notController = filter.notController;
this.controller = filter.controller;
}
@Override
public boolean match(StackObject spell, Game game) {
if (!(spell instanceof Spell))
return notFilter;
if (((Spell)spell).getFromZone().match(fromZone) == notFromZone)
return notFilter;
return super.match(spell, game);
}
@Override
public boolean match(StackObject spell, UUID playerId, Game game) {
if (!this.match(spell, game))
return notFilter;
return super.match(spell, playerId, game);
}
@Override
public FilterSpell copy() {
return new FilterSpell(this);
}
public void setFromZone(Zone fromZone) {
this.fromZone = fromZone;
}
public void setNotFromZone(boolean notFromZone) {
this.notFromZone = notFromZone;
}
}

View file

@ -31,24 +31,20 @@ package mage.filter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import mage.Constants.TargetController;
import mage.filter.predicate.ObjectPlayer;
import mage.filter.predicate.ObjectPlayerPredicate;
import mage.filter.predicate.Predicates;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.game.stack.StackObject;
/**
*
* @author BetaSteward_at_googlemail.com
* @author North
*/
public class FilterStackObject extends FilterObject<StackObject> {
public class FilterStackObject<T extends StackObject> extends FilterObject<T> {
protected List<ObjectPlayerPredicate<ObjectPlayer<Permanent>>> extraPredicates = new ArrayList<ObjectPlayerPredicate<ObjectPlayer<Permanent>>>();
protected List<UUID> controllerId = new ArrayList<UUID>();
protected boolean notController = false;
protected TargetController controller = TargetController.ANY;
public FilterStackObject() {
super("spell or ability");
@ -60,64 +56,21 @@ public class FilterStackObject extends FilterObject<StackObject> {
public FilterStackObject(final FilterStackObject filter) {
super(filter);
this.controllerId.addAll(filter.controllerId);
this.notController = filter.notController;
this.controller = filter.controller;
this.extraPredicates = new ArrayList<ObjectPlayerPredicate<ObjectPlayer<Permanent>>>(extraPredicates);
}
@Override
public boolean match(StackObject spell, Game game) {
if (!super.match(spell, game))
return notFilter;
if (controllerId.size() > 0 && controllerId.contains(spell.getControllerId()) == notController)
return notFilter;
return !notFilter;
}
public boolean match(StackObject spell, UUID playerId, Game game) {
if (!this.match(spell, game))
return notFilter;
if (controller != TargetController.ANY && playerId != null) {
switch(controller) {
case YOU:
if (!spell.getControllerId().equals(playerId))
return notFilter;
break;
case OPPONENT:
if (!game.getOpponents(playerId).contains(spell.getControllerId()))
return notFilter;
break;
case NOT_YOU:
if (spell.getControllerId().equals(playerId))
return notFilter;
break;
}
public boolean match(T stackObject, UUID playerId, Game game) {
if (!this.match(stackObject, game)) {
return false;
}
return !notFilter;
return Predicates.and(extraPredicates).apply(new ObjectPlayer(stackObject, playerId), game);
}
public void add(ObjectPlayerPredicate predicate) {
extraPredicates.add(predicate);
}
public List<UUID> getControllerId() {
return controllerId;
}
public void setNotController(boolean notController) {
this.notController = notController;
}
public void setTargetController(TargetController controller) {
this.controller = controller;
}
@Override
public FilterStackObject copy() {
return new FilterStackObject(this);