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

@ -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
*/
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

View file

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

View file

@ -19,7 +19,7 @@ public enum GetKickerXValue implements DynamicValue {
public int calculate(Game game, Ability sourceAbility, Effect effect) {
// Currently identical logic to the Manacost X value
// 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

View file

@ -14,7 +14,7 @@ public enum GetXLoyaltyValue implements DynamicValue {
@Override
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

View file

@ -14,7 +14,7 @@ public enum GetXValue implements DynamicValue {
@Override
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

View file

@ -16,7 +16,7 @@ public enum ManacostVariableValue implements DynamicValue {
@Override
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

View file

@ -57,7 +57,7 @@ public class EntersBattlefieldWithXCountersEffect extends OneShotEffect {
}
}
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) {
Counter counterToAdd = counter.copy();
counterToAdd.add(amount - counter.getCount());

View file

@ -137,7 +137,13 @@ public class KickerAbility extends StaticAbility implements OptionalAdditionalSo
String finalActivationKey = getActivationKey(needKickerCost);
Stream<Map.Entry<String, Object>> tagStream = costsTags.entrySet().stream()
.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()));
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);
}

View file

@ -121,7 +121,7 @@ class SquadTriggerAbility extends EntersBattlefieldTriggeredAbility {
@Override
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);
}
@Override
@ -148,7 +148,7 @@ class SquadEffectETB extends OneShotEffect {
@Override
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);
return effect.apply(game, source);
}

View file

@ -408,10 +408,6 @@ public class StackAbility extends StackObjectImpl implements Ability {
ability.setCostsTag(tag, value);
}
@Override
public Object getCostsTagOrDefault(String tag, Object defaultValue){
return ability.getCostsTagOrDefault(tag, defaultValue);
}
@Override
public AbilityType 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.
* Works in any moment (even before source ability activated)
* Do not use with null values, use checkSourceCostsTagExists instead
*
* @param game
* @param source
@ -1718,10 +1719,17 @@ public final class CardUtil {
* @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
*/
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);
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;
}