Fix #9649 and clean up counter effect text generation

This commit is contained in:
Alex W. Jackson 2022-10-14 00:41:02 -04:00
parent 5ec2cd0378
commit 332db3aecb
17 changed files with 117 additions and 374 deletions

View file

@ -1,6 +1,7 @@
package mage.counters;
import mage.util.CardUtil;
import java.io.Serializable;
import org.apache.log4j.Logger;
@ -56,20 +57,6 @@ public class Counter implements Serializable {
count += amount;
}
/**
* Decreases the {@code count} by one. Will not allow the count to be less
* than 0. If an attempt is made to make the count be less than zero, the
* call will be logged.
*/
public void decrease() {
if (count > 0) {
count--;
} else {
logger.warn("An attempt was made to set the counter '" + name
+ "' to less than 0. Setting to 0.");
}
}
/**
* Decreases the {@code count} by the passed in {@code amount}. Will not
* allow the count to be less than 0. If an attempt is made to make the
@ -105,6 +92,15 @@ public class Counter implements Serializable {
return count;
}
/**
* Returns a full description of this {@link Counter}, e.g. "a +1/+1 counter" or "two -1/-1 counters"
*
* @return a full description of this {@link Counter}
*/
public String getDescription() {
return CardUtil.numberToText(Math.max(count, 1), CounterType.findArticle(name)) + ' ' + name + (count > 1 ? " counters" : " counter");
}
/**
* Returns a deep copy of this object.
*

View file

@ -5,6 +5,9 @@ import mage.cards.Card;
import mage.filter.predicate.Predicate;
import mage.game.Game;
import java.util.HashMap;
import java.util.Map;
/**
* Enum for counters, names and instances.
*
@ -205,6 +208,14 @@ public enum CounterType {
private final String article;
private final CounterPredicate predicate;
private static final Map<String, CounterType> counterNameMap = new HashMap<>();
static {
for (CounterType counter : CounterType.values()) {
counterNameMap.put(counter.name, counter);
}
}
CounterType(String name) {
this(name, "aeiou".contains("" + name.charAt(0)) ? "an" : "a");
}
@ -301,12 +312,7 @@ public enum CounterType {
}
public static CounterType findByName(String name) {
for (CounterType counterType : values()) {
if (counterType.getName().equals(name)) {
return counterType;
}
}
return null;
return counterNameMap.get(name);
}
public static String findArticle(String name) {