Cost: CostDiscard & CostTapType can now be merged if the Type is the same and amount is numeric in both cases.

This commit is contained in:
Hanmac
2016-07-31 07:44:49 +00:00
parent b5d3b86693
commit 2b19c3fa9f

View File

@@ -22,6 +22,8 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.google.common.collect.Lists;
import forge.card.mana.ManaCost;
@@ -745,6 +747,7 @@ public class Cost {
public Cost add(Cost cost1) {
CostPartMana costPart2 = this.getCostMana();
List<CostPart> toRemove = Lists.newArrayList();
for (final CostPart part : cost1.getCostParts()) {
if (part instanceof CostPartMana && costPart2 != null) {
ManaCostBeingPaid oldManaCost = new ManaCostBeingPaid(((CostPartMana) part).getMana());
@@ -752,12 +755,35 @@ public class Cost {
String r2 = costPart2.getRestiction();
String r1 = ((CostPartMana) part).getRestiction();
String r = r1 == null ? r2 : ( r2 == null ? r1 : r1 + "." + r2);
getCostParts().remove(costPart2);
getCostParts().add(0, new CostPartMana(oldManaCost.toManaCost(), r));
costParts.remove(costPart2);
costParts.add(0, new CostPartMana(oldManaCost.toManaCost(), r));
} else if (part instanceof CostDiscard || part instanceof CostTapType) {
boolean alreadyAdded = false;
for (final CostPart other : costParts) {
if (other.getClass().equals(part.getClass()) &&
part.getType().equals(other.getType()) &&
StringUtils.isNumeric(part.getAmount()) &&
StringUtils.isNumeric(other.getAmount())) {
final String amount = String.valueOf(Integer.parseInt(part.getAmount()) + Integer.parseInt(other.getAmount()));
if (part instanceof CostDiscard) {
costParts.add(new CostDiscard(amount, part.getType(), part.getTypeDescription()));
} else if (part instanceof CostTapType) {
CostTapType tappart = (CostTapType)part;
costParts.add(new CostTapType(amount, part.getType(), part.getTypeDescription(), !tappart.canTapSource));
}
toRemove.add(other);
alreadyAdded = true;
break;
}
}
if (!alreadyAdded) {
costParts.add(part);
}
} else {
getCostParts().add(part);
costParts.add(part);
}
}
costParts.removeAll(toRemove);
this.sort();
return this;
}