removed some variable.matches("[0-9][0-9]?") in favour of StringUtils.isNumeric and AbilityUtils.calculateAmount (that handles plain numbers correctly)

This commit is contained in:
Maxmtg
2013-04-09 06:49:40 +00:00
parent 1e38f101d5
commit b8e92ef7e5
6 changed files with 26 additions and 40 deletions

View File

@@ -3,6 +3,8 @@ package forge.card.ability.effects;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import forge.Card;
import forge.CardLists;
import forge.CardPredicates.Presets;
@@ -49,9 +51,8 @@ public class ChooseCardEffect extends SpellAbilityEffect {
choices = CardLists.filterControlledBy(choices, tgtPlayers.get(0));
}
final String numericAmount = sa.hasParam("Amount") ? sa.getParam("Amount") : "1";
final int validAmount = !numericAmount.matches("[0-9][0-9]?")
? CardFactoryUtil.xCount(host, host.getSVar(sa.getParam("Amount"))) : Integer.parseInt(numericAmount);
final String numericAmount = sa.getParamOrDefault("Amount", "1");
final int validAmount = StringUtils.isNumeric(numericAmount) ? Integer.parseInt(numericAmount) : CardFactoryUtil.xCount(host, host.getSVar(numericAmount));
for (final Player p : tgtPlayers) {
if (sa.hasParam("EachBasicType")) {

View File

@@ -3,6 +3,8 @@ package forge.card.ability.effects;
import java.util.List;
import java.util.Random;
import org.apache.commons.lang.StringUtils;
import forge.Card;
import forge.card.ability.SpellAbilityEffect;
import forge.card.cardfactory.CardFactoryUtil;
@@ -36,23 +38,10 @@ public class ChooseNumberEffect extends SpellAbilityEffect {
//final int max = sa.containsKey("Max") ? Integer.parseInt(sa.get("Max")) : 99;
final boolean random = sa.hasParam("Random");
final int min;
if (!sa.hasParam("Min")) {
min = Integer.parseInt("0");
} else if (sa.getParam("Min").matches("[0-9][0-9]?")) {
min = Integer.parseInt(sa.getParam("Min"));
} else {
min = CardFactoryUtil.xCount(card, card.getSVar(sa.getParam("Min")));
} // Allow variables for Min
final int max;
if (!sa.hasParam("Max")) {
max = Integer.parseInt("99");
} else if (sa.getParam("Max").matches("[0-9][0-9]?")) {
max = Integer.parseInt(sa.getParam("Max"));
} else {
max = CardFactoryUtil.xCount(card, card.getSVar(sa.getParam("Max")));
} // Allow variables for Max
final String sMin = sa.getParamOrDefault("Min", "0");
final int min = StringUtils.isNumeric(sMin) ? Integer.parseInt(sMin) : CardFactoryUtil.xCount(card, card.getSVar(sMin));
final String sMax = sa.getParamOrDefault("Max", "99");
final int max = StringUtils.isNumeric(sMax) ? Integer.parseInt(sMax) : CardFactoryUtil.xCount(card, card.getSVar(sMax));
final String[] choices = new String[max + 1];
if (!random) {

View File

@@ -4,6 +4,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Predicate;
import forge.Card;
@@ -122,9 +124,8 @@ public class ChooseSourceEffect extends SpellAbilityEffect {
return;
}
final String numericAmount = sa.hasParam("Amount") ? sa.getParam("Amount") : "1";
final int validAmount = !numericAmount.matches("[0-9][0-9]?")
? CardFactoryUtil.xCount(host, host.getSVar(sa.getParam("Amount"))) : Integer.parseInt(numericAmount);
final String numericAmount = sa.getParamOrDefault("Amount", "1");
final int validAmount = StringUtils.isNumeric(numericAmount) ? Integer.parseInt(numericAmount) : CardFactoryUtil.xCount(host, host.getSVar(numericAmount));
for (final Player p : tgtPlayers) {
if ((tgt == null) || p.canBeTargetedBy(sa)) {

View File

@@ -348,7 +348,7 @@ public class ManaPool {
}
int numColorless = 0;
if (manaStr.matches("[0-9][0-9]?")) {
if (StringUtils.isNumeric(manaStr)) {
numColorless = Integer.parseInt(manaStr);
}
if (numColorless >= totalMana) {

View File

@@ -318,8 +318,7 @@ public class SpellAbilityRestriction extends SpellAbilityVariables {
if (this.getLimitToCheck() != null) {
String limit = this.getLimitToCheck();
int activationLimit = limit.matches("[0-9][0-9]?")
? Integer.parseInt(limit) : AbilityUtils.calculateAmount(c, limit, sa);
int activationLimit = AbilityUtils.calculateAmount(c, limit, sa);
this.setActivationLimit(activationLimit);
if ((this.getActivationLimit() != -1) && (this.getNumberTurnActivations() >= this.getActivationLimit())) {

View File

@@ -22,6 +22,8 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import forge.Card;
import forge.CardLists;
import forge.CardUtil;
@@ -104,30 +106,26 @@ public class StaticAbilityContinuous {
if (params.containsKey("SetPower")) {
setP = params.get("SetPower");
setPower = setP.matches("[0-9][0-9]?") ? Integer.parseInt(setP)
: AbilityUtils.calculateAmount(hostCard, setP, null);
setPower = AbilityUtils.calculateAmount(hostCard, setP, null);
}
if (params.containsKey("SetToughness")) {
setT = params.get("SetToughness");
setToughness = setT.matches("[0-9][0-9]?") ? Integer.parseInt(setT)
: AbilityUtils.calculateAmount(hostCard, setT, null);
setToughness = AbilityUtils.calculateAmount(hostCard, setT, null);
}
if (params.containsKey("AddPower")) {
addP = params.get("AddPower");
powerBonus = addP.matches("[0-9][0-9]?") ? Integer.parseInt(addP)
: AbilityUtils.calculateAmount(hostCard, addP, null);
if (!addP.matches("[0-9][0-9]?") && !addP.equals("AffectedX")) {
powerBonus = AbilityUtils.calculateAmount(hostCard, addP, null);
if (!StringUtils.isNumeric(addP) && !addP.equals("AffectedX")) {
se.setXValue(powerBonus);
}
}
if (params.containsKey("AddToughness")) {
addT = params.get("AddToughness");
toughnessBonus = addT.matches("[0-9][0-9]?") ? Integer.parseInt(addT)
: AbilityUtils.calculateAmount(hostCard, addT, null);
if (!addT.matches("[0-9][0-9]?") && !addT.equals("AffectedX")) {
toughnessBonus = AbilityUtils.calculateAmount(hostCard, addT, null);
if (!StringUtils.isNumeric(addT) && !addT.equals("AffectedX")) {
se.setYValue(toughnessBonus);
}
}
@@ -312,16 +310,14 @@ public class StaticAbilityContinuous {
if (mhs.equals("Unlimited")) {
p.setUnlimitedHandSize(true);
} else {
int max = mhs.matches("[0-9][0-9]?") ? Integer.parseInt(mhs)
: AbilityUtils.calculateAmount(hostCard, mhs, null);
int max = AbilityUtils.calculateAmount(hostCard, mhs, null);
p.setMaxHandSize(max);
}
}
if (params.containsKey("RaiseMaxHandSize")) {
String rmhs = params.get("RaiseMaxHandSize");
int rmax = rmhs.matches("[0-9][0-9]?") ? Integer.parseInt(rmhs)
: AbilityUtils.calculateAmount(hostCard, rmhs, null);
int rmax = AbilityUtils.calculateAmount(hostCard, rmhs, null);
p.setMaxHandSize(p.getMaxHandSize() + rmax);
}
}