there were 3 enums to compare ints, refactored to 1

This commit is contained in:
ingmargoudt 2017-04-10 00:21:09 +02:00
parent 813d84274a
commit cb693b5826
339 changed files with 1263 additions and 1225 deletions

View file

@ -3,12 +3,23 @@ package mage.abilities;
/**
* Created by IGOUDT on 5-3-2017.
*/
public enum CountType {
MORE_THAN, FEWER_THAN, EQUAL_TO;
public enum CountType {
MORE_THAN(">"), FEWER_THAN("<"), EQUAL_TO("==");
String operator;
CountType(String op) {
operator = op;
}
@Override
public String toString() {
return operator;
}
public static boolean compare(int source, CountType comparison, int target){
switch (comparison){
public static boolean compare(int source, CountType comparison, int target) {
switch (comparison) {
case MORE_THAN:
return source > target;
case FEWER_THAN:
@ -16,7 +27,7 @@ public enum CountType {
case EQUAL_TO:
return source == target;
default:
throw new IllegalArgumentException("comparison rules for "+comparison + " missing");
throw new IllegalArgumentException("comparison rules for " + comparison + " missing");
}
}
}

View file

@ -27,10 +27,10 @@
*/
package mage.abilities.common;
import mage.abilities.CountType;
import mage.abilities.StateTriggeredAbility;
import mage.abilities.effects.Effect;
import mage.constants.Zone;
import mage.filter.Filter.ComparisonType;
import mage.filter.FilterPermanent;
import mage.game.Game;
import mage.game.events.GameEvent;
@ -44,10 +44,10 @@ import mage.game.events.GameEvent;
public class ControlsPermanentsControllerTriggeredAbility extends StateTriggeredAbility {
protected final FilterPermanent filter;
protected final ComparisonType type;
protected final CountType type;
protected final int value;
public ControlsPermanentsControllerTriggeredAbility(FilterPermanent filter, ComparisonType type, int value, Effect effect) {
public ControlsPermanentsControllerTriggeredAbility(FilterPermanent filter, CountType type, int value, Effect effect) {
super(Zone.BATTLEFIELD, effect);
this.filter = filter;
this.value = value;
@ -69,24 +69,7 @@ public class ControlsPermanentsControllerTriggeredAbility extends StateTriggered
@Override
public boolean checkTrigger(GameEvent event, Game game) {
int inputValue = game.getBattlefield().countAll(filter, getControllerId(), game);
switch (type) {
case Equal:
if (inputValue != value) {
return false;
}
break;
case GreaterThan:
if (inputValue <= value) {
return false;
}
break;
case LessThan:
if (inputValue >= value) {
return false;
}
break;
}
return true;
return CountType.compare(value, type, inputValue);
}
@Override

View file

@ -1,9 +1,10 @@
package mage.abilities.condition;
import java.io.Serializable;
import mage.abilities.Ability;
import mage.game.Game;
import java.io.Serializable;
/**
* Interface describing condition occurrence.
@ -13,24 +14,6 @@ import mage.game.Game;
@FunctionalInterface
public interface Condition extends Serializable {
enum ComparisonType {
GreaterThan(">"),
Equal("=="),
LessThan("<");
private final String text;
ComparisonType(String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}
/**
* Checks the game to see if this condition applies for the given ability.
*

View file

@ -27,22 +27,23 @@
*/
package mage.abilities.condition.common;
import java.util.UUID;
import mage.abilities.Ability;
import mage.abilities.CountType;
import mage.abilities.condition.Condition;
import mage.game.Game;
import mage.players.Player;
import java.util.UUID;
/**
*
* @author LevelX2
*/
public class CardsInAnyLibraryCondition implements Condition {
protected final Condition.ComparisonType type;
protected final CountType type;
protected final int value;
public CardsInAnyLibraryCondition(Condition.ComparisonType type, int value) {
public CardsInAnyLibraryCondition(CountType type, int value) {
this.type = type;
this.value = value;
}
@ -54,23 +55,8 @@ public class CardsInAnyLibraryCondition implements Condition {
for (UUID playerId : game.getState().getPlayersInRange(controller.getId(), game)) {
Player player = game.getPlayer(playerId);
if (player != null) {
switch (type) {
case GreaterThan:
if (player.getLibrary().size() > value) {
return true;
}
break;
case Equal:
if (player.getLibrary().size() == value) {
return true;
}
break;
case LessThan:
if (player.getLibrary().size() < value) {
return true;
}
break;
}
return CountType.compare(player.getLibrary().size(), type, value);
}
}
}
@ -81,13 +67,13 @@ public class CardsInAnyLibraryCondition implements Condition {
public String toString() {
StringBuilder sb = new StringBuilder("a library has ");
switch (type) {
case GreaterThan:
case MORE_THAN:
sb.append(value + 1).append(" or more cards in it ");
break;
case Equal:
case EQUAL_TO:
sb.append(value).append(" cards in it ");
break;
case LessThan:
case FEWER_THAN:
sb.append(value - 1).append(" or fewer cards in it ");
break;
}

View file

@ -29,8 +29,8 @@
package mage.abilities.condition.common;
import mage.abilities.Ability;
import mage.abilities.CountType;
import mage.abilities.condition.Condition;
import mage.filter.Filter;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.predicate.mageobject.PowerPredicate;
import mage.game.Game;
@ -43,7 +43,7 @@ public enum FerociousCondition implements Condition {
private static final FilterCreaturePermanent filter = new FilterCreaturePermanent();
static {
filter.add(new PowerPredicate(Filter.ComparisonType.GreaterThan, 3));
filter.add(new PowerPredicate(CountType.MORE_THAN, 3));
}

View file

@ -29,10 +29,10 @@
package mage.abilities.effects.common.cost;
import mage.abilities.Ability;
import mage.abilities.CountType;
import mage.abilities.effects.OneShotEffect;
import mage.cards.Card;
import mage.constants.Outcome;
import mage.filter.Filter;
import mage.filter.common.FilterNonlandCard;
import mage.filter.predicate.mageobject.ConvertedManaCostPredicate;
import mage.game.Game;
@ -58,7 +58,7 @@ public class CastWithoutPayingManaCostEffect extends OneShotEffect {
public CastWithoutPayingManaCostEffect(int maxCost) {
super(Outcome.PlayForFree);
filter = new FilterNonlandCard("card with converted mana cost " + maxCost + " or less from your hand");
filter.add(new ConvertedManaCostPredicate(Filter.ComparisonType.LessThan, maxCost + 1));
filter.add(new ConvertedManaCostPredicate(CountType.FEWER_THAN, maxCost + 1));
this.manaCost = maxCost;
this.staticText = "you may cast a card with converted mana cost " + maxCost + " or less from your hand without paying its mana cost";
}

View file

@ -28,11 +28,11 @@
package mage.abilities.effects.common.search;
import mage.abilities.Ability;
import mage.abilities.CountType;
import mage.abilities.effects.OneShotEffect;
import mage.cards.Card;
import mage.constants.Outcome;
import mage.constants.Zone;
import mage.filter.Filter;
import mage.filter.FilterCard;
import mage.filter.predicate.mageobject.ConvertedManaCostPredicate;
import mage.game.Game;
@ -67,7 +67,7 @@ public class SearchLibraryWithLessCMCPutInPlayEffect extends OneShotEffect {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
FilterCard advancedFilter = filter.copy(); // never change static objects so copy the object here before
advancedFilter.add(new ConvertedManaCostPredicate(Filter.ComparisonType.LessThan, source.getManaCostsToPay().getX() + 1));
advancedFilter.add(new ConvertedManaCostPredicate(CountType.FEWER_THAN, source.getManaCostsToPay().getX() + 1));
TargetCardInLibrary target = new TargetCardInLibrary(advancedFilter);
if (controller.searchLibrary(target, game)) {
if (!target.getTargets().isEmpty()) {

View file

@ -28,6 +28,7 @@
package mage.abilities.effects.keyword;
import mage.abilities.Ability;
import mage.abilities.CountType;
import mage.abilities.dynamicvalue.DynamicValue;
import mage.abilities.dynamicvalue.common.StaticValue;
import mage.abilities.effects.Effect;
@ -35,7 +36,6 @@ import mage.abilities.effects.OneShotEffect;
import mage.abilities.effects.common.counter.AddCountersTargetEffect;
import mage.constants.Outcome;
import mage.counters.CounterType;
import mage.filter.Filter;
import mage.filter.FilterPermanent;
import mage.filter.common.FilterControlledCreaturePermanent;
import mage.filter.common.FilterCreaturePermanent;
@ -96,7 +96,7 @@ public class BolsterEffect extends OneShotEffect {
if (leastToughness != Integer.MAX_VALUE) {
if (selectedCreature == null) {
FilterPermanent filter = new FilterControlledCreaturePermanent("creature you control with toughness " + leastToughness);
filter.add(new ToughnessPredicate(Filter.ComparisonType.Equal, leastToughness));
filter.add(new ToughnessPredicate(CountType.EQUAL_TO, leastToughness));
Target target = new TargetPermanent(1,1, filter, true);
if (controller.chooseTarget(outcome, target, source, game)) {
selectedCreature = game.getPermanent(target.getFirstTarget());

View file

@ -28,18 +28,19 @@
package mage.abilities.keyword;
import java.util.UUID;
import mage.abilities.CountType;
import mage.abilities.common.DiesTriggeredAbility;
import mage.abilities.dynamicvalue.DynamicValue;
import mage.abilities.dynamicvalue.common.StaticValue;
import mage.abilities.effects.common.ReturnToHandTargetEffect;
import mage.filter.Filter;
import mage.filter.FilterCard;
import mage.filter.predicate.mageobject.ConvertedManaCostPredicate;
import mage.filter.predicate.mageobject.SubtypePredicate;
import mage.game.Game;
import mage.target.common.TargetCardInYourGraveyard;
import java.util.UUID;
/**
*
* 702.45. Soulshift
@ -75,7 +76,7 @@ public class SoulshiftAbility extends DiesTriggeredAbility {
this.getTargets().clear();
int intValue = amount.calculate(game, this, null);
FilterCard filter = new FilterCard("Spirit card with converted mana cost " + intValue + " or less from your graveyard");
filter.add(new ConvertedManaCostPredicate(Filter.ComparisonType.LessThan, intValue + 1));
filter.add(new ConvertedManaCostPredicate(CountType.FEWER_THAN, intValue + 1));
filter.add(new SubtypePredicate("Spirit"));
this.addTarget(new TargetCardInYourGraveyard(filter));
super.trigger(game, controllerId); //To change body of generated methods, choose Tools | Templates.

View file

@ -1,6 +1,8 @@
package mage.abilities.keyword;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.CountType;
import mage.abilities.common.SimpleActivatedAbility;
import mage.abilities.costs.common.DiscardSourceCost;
import mage.abilities.costs.mana.ManaCostsImpl;
@ -8,17 +10,14 @@ import mage.abilities.effects.OneShotEffect;
import mage.cards.Cards;
import mage.cards.CardsImpl;
import mage.constants.Outcome;
import mage.constants.TimingRule;
import mage.constants.Zone;
import mage.filter.Filter;
import mage.filter.FilterCard;
import mage.filter.predicate.mageobject.ConvertedManaCostPredicate;
import mage.game.Game;
import mage.players.Player;
import mage.target.common.TargetCardInLibrary;
import mage.MageObject;
import mage.constants.TimingRule;
/**
*
* 702.52. Transmute
@ -79,7 +78,7 @@ class TransmuteEffect extends OneShotEffect {
MageObject sourceObject = game.getObject(source.getSourceId());
if (sourceObject != null && controller != null) {
FilterCard filter = new FilterCard("card with converted mana cost " + sourceObject.getConvertedManaCost());
filter.add(new ConvertedManaCostPredicate(Filter.ComparisonType.Equal, sourceObject.getConvertedManaCost()));
filter.add(new ConvertedManaCostPredicate(CountType.EQUAL_TO, sourceObject.getConvertedManaCost()));
TargetCardInLibrary target = new TargetCardInLibrary(1, filter);
if (controller.searchLibrary(target, game)) {
if (!target.getTargets().isEmpty()) {

View file

@ -27,37 +27,18 @@
*/
package mage.filter;
import java.io.Serializable;
import mage.filter.predicate.Predicate;
import mage.game.Game;
import java.io.Serializable;
/**
*
* @param <E>
* @author BetaSteward_at_googlemail.com
* @author North
*
* @param <E>
*/
public interface Filter<E> extends Serializable {
enum ComparisonType {
GreaterThan(">"),
Equal("=="),
LessThan("<");
private final String text;
ComparisonType(String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}
enum ComparisonScope {
Any, All
}

View file

@ -28,7 +28,7 @@
package mage.filter.predicate;
import mage.MageObject;
import mage.filter.Filter;
import mage.abilities.CountType;
import mage.game.Game;
/**
@ -38,10 +38,10 @@ import mage.game.Game;
*/
public abstract class IntComparePredicate<T extends MageObject> implements Predicate<T> {
protected final Filter.ComparisonType type;
protected final CountType type;
protected final int value;
public IntComparePredicate(Filter.ComparisonType type, int value) {
public IntComparePredicate(CountType type, int value) {
this.type = type;
this.value = value;
}
@ -51,24 +51,7 @@ public abstract class IntComparePredicate<T extends MageObject> implements Predi
@Override
public final boolean apply(T input, Game game) {
int inputValue = getInputValue(input);
switch (type) {
case Equal:
if (inputValue != value) {
return false;
}
break;
case GreaterThan:
if (inputValue <= value) {
return false;
}
break;
case LessThan:
if (inputValue >= value) {
return false;
}
break;
}
return true;
return CountType.compare(value, type, inputValue);
}
@Override

View file

@ -28,7 +28,7 @@
package mage.filter.predicate.mageobject;
import mage.MageObject;
import mage.filter.Filter;
import mage.abilities.CountType;
import mage.filter.predicate.IntComparePredicate;
/**
@ -37,7 +37,7 @@ import mage.filter.predicate.IntComparePredicate;
*/
public class ConvertedManaCostPredicate extends IntComparePredicate<MageObject> {
public ConvertedManaCostPredicate(Filter.ComparisonType type, int value) {
public ConvertedManaCostPredicate(CountType type, int value) {
super(type, value);
}

View file

@ -28,7 +28,7 @@
package mage.filter.predicate.mageobject;
import mage.MageObject;
import mage.filter.Filter;
import mage.abilities.CountType;
import mage.filter.predicate.IntComparePredicate;
/**
@ -37,7 +37,7 @@ import mage.filter.predicate.IntComparePredicate;
*/
public class PowerPredicate extends IntComparePredicate<MageObject> {
public PowerPredicate(Filter.ComparisonType type, int value) {
public PowerPredicate(CountType type, int value) {
super(type, value);
}

View file

@ -28,7 +28,7 @@
package mage.filter.predicate.mageobject;
import mage.MageObject;
import mage.filter.Filter;
import mage.abilities.CountType;
import mage.filter.predicate.IntComparePredicate;
/**
@ -37,7 +37,7 @@ import mage.filter.predicate.IntComparePredicate;
*/
public class ToughnessPredicate extends IntComparePredicate<MageObject> {
public ToughnessPredicate(Filter.ComparisonType type, int value) {
public ToughnessPredicate(CountType type, int value) {
super(type, value);
}