refactor AwakenAbility to remove inner class

This commit is contained in:
xenohedron 2024-02-16 23:06:09 -05:00
parent baa8a18c3e
commit cbad085169

View file

@ -1,7 +1,6 @@
package mage.abilities.keyword;
import mage.MageInt;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.SpellAbility;
import mage.abilities.costs.mana.ManaCostsImpl;
@ -20,7 +19,6 @@ import mage.target.Target;
import mage.target.common.TargetControlledPermanent;
import mage.target.targetpointer.FixedTarget;
import mage.util.CardUtil;
import org.apache.log4j.Logger;
import java.util.UUID;
@ -29,12 +27,8 @@ import java.util.UUID;
*/
public class AwakenAbility extends SpellAbility {
private static final Logger logger = Logger.getLogger(AwakenAbility.class);
private static final String filterMessage = "a land you control to awake";
private final String rule;
private final int awakenValue;
private static final FilterControlledLandPermanent filter = new FilterControlledLandPermanent(AwakenEffect.filterMessage);
public AwakenAbility(Card card, int awakenValue, String awakenCosts) {
super(card.getSpellAbility());
@ -47,9 +41,9 @@ public class AwakenAbility extends SpellAbility {
this.clearManaCostsToPay();
this.addCost(new ManaCostsImpl<>(awakenCosts));
this.addTarget(new TargetControlledPermanent(new FilterControlledLandPermanent(filterMessage)));
this.addEffect(new AwakenEffect());
this.awakenValue = awakenValue;
this.addTarget(new TargetControlledPermanent(filter));
this.addEffect(new AwakenEffect(awakenValue));
rule = "Awaken " + awakenValue + "&mdash;" + awakenCosts
+ " <i>(If you cast this spell for " + awakenCosts + ", also put "
+ CardUtil.getOneOneCountersText(awakenValue)
@ -58,7 +52,6 @@ public class AwakenAbility extends SpellAbility {
protected AwakenAbility(final AwakenAbility ability) {
super(ability);
this.awakenValue = ability.awakenValue;
this.rule = ability.rule;
}
@ -77,56 +70,52 @@ public class AwakenAbility extends SpellAbility {
return rule;
}
class AwakenEffect extends OneShotEffect {
}
private AwakenEffect() {
super(Outcome.BoostCreature);
this.staticText = "put " + CardUtil.getOneOneCountersText(awakenValue) +" on target land you control";
}
class AwakenEffect extends OneShotEffect {
protected AwakenEffect(final AwakenEffect effect) {
super(effect);
}
static final String filterMessage = "a land you control to awake";
@Override
public AwakenEffect copy() {
return new AwakenEffect(this);
}
private final int awakenValue;
@Override
public boolean apply(Game game, Ability source) {
UUID targetId = null;
if (source != null && source.getTargets() != null) {
for (Target target : source.getTargets()) {
if (target.getFilter() != null && target.getFilter().getMessage().equals(filterMessage)) {
targetId = target.getFirstTarget();
}
}
if (targetId != null) {
FixedTarget fixedTarget = new FixedTarget(targetId, game);
ContinuousEffect continuousEffect = new BecomesCreatureTargetEffect(new AwakenElementalToken(), false, true, Duration.Custom);
continuousEffect.setTargetPointer(fixedTarget);
game.addEffect(continuousEffect, source);
Effect effect = new AddCountersTargetEffect(CounterType.P1P1.createInstance(awakenValue));
effect.setTargetPointer(fixedTarget);
return effect.apply(game, source);
}
} else // source should never be null, but we are seeing a lot of NPEs from this section
if (source == null) {
logger.fatal("Source was null in AwakenAbility: Create a bug report or fix the source code");
} else if (source.getTargets() == null) {
MageObject sourceObj = source.getSourceObject(game);
if (sourceObj != null) {
Class<? extends MageObject> sourceClass = sourceObj.getClass();
if (sourceClass != null) {
logger.fatal("getTargets was null in AwakenAbility for " + sourceClass.toString() + " : Create a bug report or fix the source code");
}
}
}
return true;
}
AwakenEffect(int awakenValue) {
super(Outcome.BoostCreature);
this.awakenValue = awakenValue;
this.staticText = "put " + CardUtil.getOneOneCountersText(awakenValue) + " on target land you control and it becomes a 0/0 Elemental creature with haste";
}
private AwakenEffect(final AwakenEffect effect) {
super(effect);
this.awakenValue = effect.awakenValue;
}
@Override
public AwakenEffect copy() {
return new AwakenEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
UUID targetId = null;
if (source != null && source.getTargets() != null) {
for (Target target : source.getTargets()) {
if (target.getFilter() != null && target.getFilter().getMessage().equals(filterMessage)) {
targetId = target.getFirstTarget();
}
}
if (targetId != null) {
FixedTarget fixedTarget = new FixedTarget(targetId, game);
ContinuousEffect continuousEffect = new BecomesCreatureTargetEffect(new AwakenElementalToken(), false, true, Duration.Custom);
continuousEffect.setTargetPointer(fixedTarget);
game.addEffect(continuousEffect, source);
Effect effect = new AddCountersTargetEffect(CounterType.P1P1.createInstance(awakenValue));
effect.setTargetPointer(fixedTarget);
effect.apply(game, source);
}
return true;
}
return false;
}
}
class AwakenElementalToken extends TokenImpl {