refactor: SourcePermanentPowerValue to enum (#13040)

* refactor: standard enum style for SourcePermanentToughnessValue

* refactor SourcePermanentPowerCount to enum SourcePermanentPowerValue

add comments on usage of NOT_NEGATIVE vs ALLOW_NEGATIVE
This commit is contained in:
xenohedron 2024-10-27 00:19:38 -04:00 committed by GitHub
parent 5070f8bef7
commit fb71ce8c85
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
124 changed files with 435 additions and 624 deletions

View file

@ -1,60 +0,0 @@
package mage.abilities.dynamicvalue.common;
import mage.abilities.Ability;
import mage.abilities.dynamicvalue.DynamicValue;
import mage.abilities.effects.Effect;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.permanent.Permanent;
/**
* @author Loki
*/
public class SourcePermanentPowerCount implements DynamicValue {
private final boolean allowNegativeValues;
public SourcePermanentPowerCount() {
this(true);
}
public SourcePermanentPowerCount(boolean allowNegativeValues) {
super();
this.allowNegativeValues = allowNegativeValues;
}
protected SourcePermanentPowerCount(final SourcePermanentPowerCount dynamicValue) {
super();
this.allowNegativeValues = dynamicValue.allowNegativeValues;
}
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
Permanent sourcePermanent = game.getPermanent(sourceAbility.getSourceId());
if (sourcePermanent == null
|| (sourceAbility.getSourceObjectZoneChangeCounter() > 0
&& sourcePermanent.getZoneChangeCounter(game) > sourceAbility.getSourceObjectZoneChangeCounter())) {
sourcePermanent = (Permanent) game.getLastKnownInformation(sourceAbility.getSourceId(), Zone.BATTLEFIELD);
}
if (sourcePermanent != null
&& (allowNegativeValues || sourcePermanent.getPower().getValue() >= 0)) {
return sourcePermanent.getPower().getValue();
}
return 0;
}
@Override
public SourcePermanentPowerCount copy() {
return new SourcePermanentPowerCount(this);
}
@Override
public String toString() {
return "X";
}
@Override
public String getMessage() {
return "{this}'s power";
}
}

View file

@ -0,0 +1,49 @@
package mage.abilities.dynamicvalue.common;
import mage.abilities.Ability;
import mage.abilities.dynamicvalue.DynamicValue;
import mage.abilities.effects.Effect;
import mage.game.Game;
import mage.game.permanent.Permanent;
/**
* @author xenohedron
*/
public enum SourcePermanentPowerValue implements DynamicValue {
ALLOW_NEGATIVE(true), // 107.1b, only for setting power/toughness/life to a specific value or doubling
NOT_NEGATIVE(false); // all other usages
private final boolean allowNegativeValues;
SourcePermanentPowerValue(boolean allowNegativeValues) {
this.allowNegativeValues = allowNegativeValues;
}
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
Permanent sourcePermanent = sourceAbility.getSourcePermanentOrLKI(game);
if (sourcePermanent == null) {
return 0;
}
int value = sourcePermanent.getPower().getValue();
if (allowNegativeValues || value > 0) {
return value;
}
return 0;
}
@Override
public SourcePermanentPowerValue copy() {
return this;
}
@Override
public String toString() {
return "X";
}
@Override
public String getMessage() {
return "{this}'s power";
}
}

View file

@ -3,46 +3,24 @@ package mage.abilities.dynamicvalue.common;
import mage.abilities.Ability;
import mage.abilities.dynamicvalue.DynamicValue;
import mage.abilities.effects.Effect;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.permanent.Permanent;
import java.io.ObjectStreamException;
/**
* @author LevelX2
* @author xenohedron
*/
public class SourcePermanentToughnessValue implements DynamicValue {
private static final SourcePermanentToughnessValue instance = new SourcePermanentToughnessValue();
private Object readResolve() throws ObjectStreamException {
return instance;
}
public static SourcePermanentToughnessValue getInstance() {
return instance;
}
private SourcePermanentToughnessValue() {
}
public enum SourcePermanentToughnessValue implements DynamicValue {
instance;
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
Permanent sourcePermanent = game.getPermanent(sourceAbility.getSourceId());
if (sourcePermanent == null) {
sourcePermanent = (Permanent) game.getLastKnownInformation(sourceAbility.getSourceId(), Zone.BATTLEFIELD);
}
if (sourcePermanent != null) {
return sourcePermanent.getToughness().getValue();
}
return 0;
Permanent sourcePermanent = sourceAbility.getSourcePermanentOrLKI(game);
return sourcePermanent == null ? 0 : sourcePermanent.getToughness().getValue();
}
@Override
public SourcePermanentToughnessValue copy() {
return new SourcePermanentToughnessValue();
return instance;
}
@Override