Use generics to ensure the correct class types stored inside the costs tags.

This commit is contained in:
Steven Knipe 2023-11-17 02:37:06 -08:00
parent 193a19104c
commit 660288dfd7
12 changed files with 26 additions and 29 deletions

View file

@ -70,7 +70,7 @@ class AlteredEgoCopyApplier extends CopyApplier {
// except it enters with an additional X +1/+1 counters on it // except it enters with an additional X +1/+1 counters on it
blueprint.getAbilities().add( blueprint.getAbilities().add(
new EntersBattlefieldAbility(new AddCountersSourceEffect(CounterType.P1P1.createInstance( new EntersBattlefieldAbility(new AddCountersSourceEffect(CounterType.P1P1.createInstance(
(int)CardUtil.getSourceCostsTag(game, source, "X", 0) CardUtil.getSourceCostsTag(game, source, "X", 0)
))) )))
); );

View file

@ -170,13 +170,6 @@ public interface Ability extends Controllable, Serializable {
* Set tag to the value, initializes this ability's tags map if it is null * Set tag to the value, initializes this ability's tags map if it is null
*/ */
void setCostsTag(String tag, Object value); void setCostsTag(String tag, Object value);
/**
* Returns the value of the tag or defaultValue if the tag is not found in this ability's tag map
* does NOT check the source permanent's tags, use CardUtil.getSourceCostsTag for that
*
* @return The given tag value (or the default if not found)
*/
Object getCostsTagOrDefault(String tag, Object defaultValue);
/** /**
* Retrieves the effects that are put into the place by the resolution of * Retrieves the effects that are put into the place by the resolution of

View file

@ -721,12 +721,6 @@ public abstract class AbilityImpl implements Ability {
} }
costsTagMap.put(tag, value); costsTagMap.put(tag, value);
} }
public Object getCostsTagOrDefault(String tag, Object defaultValue){
if (costsTagMap != null && costsTagMap.containsKey(tag)){
return costsTagMap.get(tag);
}
return defaultValue;
}
@Override @Override
public Effects getEffects() { public Effects getEffects() {

View file

@ -19,7 +19,7 @@ public enum GetKickerXValue implements DynamicValue {
public int calculate(Game game, Ability sourceAbility, Effect effect) { public int calculate(Game game, Ability sourceAbility, Effect effect) {
// Currently identical logic to the Manacost X value // Currently identical logic to the Manacost X value
// which should be fine since you can only have one X at a time // which should be fine since you can only have one X at a time
return (int) CardUtil.getSourceCostsTag(game, sourceAbility, "X", 0); return CardUtil.getSourceCostsTag(game, sourceAbility, "X", 0);
} }
@Override @Override

View file

@ -14,7 +14,7 @@ public enum GetXLoyaltyValue implements DynamicValue {
@Override @Override
public int calculate(Game game, Ability sourceAbility, Effect effect) { public int calculate(Game game, Ability sourceAbility, Effect effect) {
return (int) CardUtil.getSourceCostsTag(game, sourceAbility, "X", 0); return CardUtil.getSourceCostsTag(game, sourceAbility, "X", 0);
} }
@Override @Override

View file

@ -14,7 +14,7 @@ public enum GetXValue implements DynamicValue {
@Override @Override
public int calculate(Game game, Ability sourceAbility, Effect effect) { public int calculate(Game game, Ability sourceAbility, Effect effect) {
return (int) CardUtil.getSourceCostsTag(game, sourceAbility, "X", 0); return CardUtil.getSourceCostsTag(game, sourceAbility, "X", 0);
} }
@Override @Override

View file

@ -16,7 +16,7 @@ public enum ManacostVariableValue implements DynamicValue {
@Override @Override
public int calculate(Game game, Ability sourceAbility, Effect effect) { public int calculate(Game game, Ability sourceAbility, Effect effect) {
return (int) CardUtil.getSourceCostsTag(game, sourceAbility, "X", 0); return CardUtil.getSourceCostsTag(game, sourceAbility, "X", 0);
} }
@Override @Override

View file

@ -57,7 +57,7 @@ public class EntersBattlefieldWithXCountersEffect extends OneShotEffect {
} }
} }
if (permanent != null) { if (permanent != null) {
int amount = ((int) CardUtil.getSourceCostsTag(game, source, "X", 0)) * multiplier; int amount = CardUtil.getSourceCostsTag(game, source, "X", 0) * multiplier;
if (amount > 0) { if (amount > 0) {
Counter counterToAdd = counter.copy(); Counter counterToAdd = counter.copy();
counterToAdd.add(amount - counter.getCount()); counterToAdd.add(amount - counter.getCount());

View file

@ -137,7 +137,13 @@ public class KickerAbility extends StaticAbility implements OptionalAdditionalSo
String finalActivationKey = getActivationKey(needKickerCost); String finalActivationKey = getActivationKey(needKickerCost);
Stream<Map.Entry<String, Object>> tagStream = costsTags.entrySet().stream() Stream<Map.Entry<String, Object>> tagStream = costsTags.entrySet().stream()
.filter(x -> x.getKey().startsWith(finalActivationKey)); .filter(x -> x.getKey().startsWith(finalActivationKey));
return tagStream.mapToInt(x -> (int)x.getValue()).sum(); return tagStream.mapToInt(x -> {
Object value = x.getValue();
if (!(value instanceof Integer)){
throw new IllegalStateException("Wrong code usage: Kicker tag "+x.getKey()+" needs Integer but has "+(value==null?"null":value.getClass().getName()));
}
return (int) value;
}).sum();
} }
/** /**
@ -182,7 +188,7 @@ public class KickerAbility extends StaticAbility implements OptionalAdditionalSo
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.KICKED, source.getSourceId(), source, source.getControllerId())); game.fireEvent(GameEvent.getEvent(GameEvent.EventType.KICKED, source.getSourceId(), source, source.getControllerId()));
String activationKey = getActivationKey(kickerCost.getText(true)); String activationKey = getActivationKey(kickerCost.getText(true));
Integer next = (int)source.getCostsTagOrDefault(activationKey,0)+1; Integer next = CardUtil.getSourceCostsTag(game, source, activationKey,0)+1;
source.setCostsTag(activationKey,next); source.setCostsTag(activationKey,next);
} }

View file

@ -121,7 +121,7 @@ class SquadTriggerAbility extends EntersBattlefieldTriggeredAbility {
@Override @Override
public boolean checkInterveningIfClause(Game game) { public boolean checkInterveningIfClause(Game game) {
int squadCount = (int)CardUtil.getSourceCostsTag(game, this, SquadAbility.SQUAD_ACTIVATION_VALUE_KEY,0); int squadCount = CardUtil.getSourceCostsTag(game, this, SquadAbility.SQUAD_ACTIVATION_VALUE_KEY,0);
return (squadCount > 0); return (squadCount > 0);
} }
@Override @Override
@ -148,7 +148,7 @@ class SquadEffectETB extends OneShotEffect {
@Override @Override
public boolean apply(Game game, Ability source) { public boolean apply(Game game, Ability source) {
int squadCount = (int)CardUtil.getSourceCostsTag(game, source, SquadAbility.SQUAD_ACTIVATION_VALUE_KEY,0); int squadCount = CardUtil.getSourceCostsTag(game, source, SquadAbility.SQUAD_ACTIVATION_VALUE_KEY,0);
CreateTokenCopySourceEffect effect = new CreateTokenCopySourceEffect(squadCount); CreateTokenCopySourceEffect effect = new CreateTokenCopySourceEffect(squadCount);
return effect.apply(game, source); return effect.apply(game, source);
} }

View file

@ -408,10 +408,6 @@ public class StackAbility extends StackObjectImpl implements Ability {
ability.setCostsTag(tag, value); ability.setCostsTag(tag, value);
} }
@Override @Override
public Object getCostsTagOrDefault(String tag, Object defaultValue){
return ability.getCostsTagOrDefault(tag, defaultValue);
}
@Override
public AbilityType getAbilityType() { public AbilityType getAbilityType() {
return ability.getAbilityType(); return ability.getAbilityType();
} }

View file

@ -1711,6 +1711,7 @@ public final class CardUtil {
/** /**
* Find a specific tag in the cost tags of either the source ability, or the permanent source of the ability. * Find a specific tag in the cost tags of either the source ability, or the permanent source of the ability.
* Works in any moment (even before source ability activated) * Works in any moment (even before source ability activated)
* Do not use with null values, use checkSourceCostsTagExists instead
* *
* @param game * @param game
* @param source * @param source
@ -1718,10 +1719,17 @@ public final class CardUtil {
* @param defaultValue A default value to return if the tag is not found * @param defaultValue A default value to return if the tag is not found
* @return The object stored by the tag if found, the default if not * @return The object stored by the tag if found, the default if not
*/ */
public static Object getSourceCostsTag(Game game, Ability source, String tag, Object defaultValue){ public static <T> T getSourceCostsTag(Game game, Ability source, String tag, T defaultValue){
Map<String, Object> costTags = getSourceCostsTagsMap(game, source); Map<String, Object> costTags = getSourceCostsTagsMap(game, source);
if (costTags != null) { if (costTags != null) {
return costTags.getOrDefault(tag, defaultValue); Object value = costTags.getOrDefault(tag, defaultValue);
if (value == null) {
throw new IllegalStateException("Wrong code usage: Costs tag " + tag + " has value stored of type null but is trying to be read. Use checkSourceCostsTagExists");
}
if (value.getClass() != defaultValue.getClass()) {
throw new IllegalStateException("Wrong code usage: Costs tag " + tag + " has value stored of type " + value.getClass().getName() + " different from default of type " + defaultValue.getClass().getName());
}
return (T) value;
} }
return defaultValue; return defaultValue;
} }