Fixed UndyingAbility and PersistAbility giving their counters to late to the permanent.

This commit is contained in:
LevelX2 2013-06-01 17:42:13 +02:00
parent e3ee986c47
commit 2d7c4e08a8
2 changed files with 154 additions and 7 deletions

View file

@ -1,18 +1,23 @@
package mage.abilities.keyword;
import mage.Constants;
import mage.Constants.Duration;
import mage.Constants.Outcome;
import mage.abilities.Ability;
import mage.abilities.common.DiesTriggeredAbility;
import mage.abilities.effects.OneShotEffect;
import mage.abilities.effects.ReplacementEffectImpl;
import mage.abilities.effects.common.ReturnSourceFromGraveyardToBattlefieldEffect;
import mage.abilities.effects.common.counter.AddCountersSourceEffect;
import mage.counters.CounterType;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.target.targetpointer.FixedTarget;
public class PersistAbility extends DiesTriggeredAbility {
public PersistAbility() {
super(new ReturnSourceFromGraveyardToBattlefieldEffect());
this.addEffect(new AddCountersSourceEffect(CounterType.M1M1.createInstance()));
super(new PersistEffect());
this.addEffect(new ReturnSourceFromGraveyardToBattlefieldEffect());
}
public PersistAbility(final PersistAbility ability) {
@ -29,6 +34,7 @@ public class PersistAbility extends DiesTriggeredAbility {
if (super.checkTrigger(event, game)) {
Permanent p = (Permanent) game.getLastKnownInformation(event.getTargetId(), Constants.Zone.BATTLEFIELD);
if (p.getCounters().getCount(CounterType.M1M1) == 0) {
game.getState().setValue(new StringBuilder("persist").append(getSourceId()).toString(), new FixedTarget(p.getId()));
return true;
}
}
@ -40,3 +46,70 @@ public class PersistAbility extends DiesTriggeredAbility {
return "Persist <i>(When this creature dies, if it had no -1/-1 counters on it, return it to the battlefield under its owner's control with a -1/-1 counter on it.)</i>";
}
}
class PersistEffect extends OneShotEffect<PersistEffect> {
public PersistEffect() {
super(Outcome.Benefit);
this.staticText = "";
}
public PersistEffect(final PersistEffect effect) {
super(effect);
}
@Override
public PersistEffect copy() {
return new PersistEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
game.addEffect(new PersistReplacementEffect(), source);
return false;
}
}
class PersistReplacementEffect extends ReplacementEffectImpl<PersistReplacementEffect> {
PersistReplacementEffect() {
super(Duration.OneUse, Outcome.UnboostCreature, false);
selfScope = true;
staticText = "return it to the battlefield under its owner's control with a -1/-1 counter on it";
}
PersistReplacementEffect(final PersistReplacementEffect effect) {
super(effect);
}
@Override
public PersistReplacementEffect copy() {
return new PersistReplacementEffect(this);
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
Permanent permanent = game.getPermanent(event.getTargetId());
if (permanent != null) {
permanent.addCounters(CounterType.M1M1.createInstance(), game);
}
used = true;
return false;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
if (event.getType() == GameEvent.EventType.ENTERS_THE_BATTLEFIELD && event.getTargetId().equals(source.getSourceId())) {
Object fixedTarget = game.getState().getValue(new StringBuilder("persist").append(source.getSourceId()).toString());
if (fixedTarget instanceof FixedTarget && ((FixedTarget) fixedTarget).getFirst(game, source).equals(source.getSourceId())) {
return true;
}
}
return false;
}
@Override
public boolean apply(Game game, Ability source) {
return false;
}
}

View file

@ -1,21 +1,27 @@
package mage.abilities.keyword;
import mage.Constants;
import mage.Constants.Duration;
import mage.Constants.Outcome;
import mage.abilities.Ability;
import mage.abilities.common.DiesTriggeredAbility;
import mage.abilities.effects.OneShotEffect;
import mage.abilities.effects.ReplacementEffectImpl;
import mage.abilities.effects.common.ReturnSourceFromGraveyardToBattlefieldEffect;
import mage.abilities.effects.common.counter.AddCountersSourceEffect;
import mage.counters.CounterType;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.target.targetpointer.FixedTarget;
/**
* @author Loki
*/
public class UndyingAbility extends DiesTriggeredAbility {
public UndyingAbility() {
super(new ReturnSourceFromGraveyardToBattlefieldEffect());
this.addEffect(new AddCountersSourceEffect(CounterType.P1P1.createInstance()));
super(new UndyingEffect());
this.addEffect(new ReturnSourceFromGraveyardToBattlefieldEffect());
}
public UndyingAbility(final UndyingAbility ability) {
@ -32,6 +38,7 @@ public class UndyingAbility extends DiesTriggeredAbility {
if (super.checkTrigger(event, game)) {
Permanent p = (Permanent) game.getLastKnownInformation(event.getTargetId(), Constants.Zone.BATTLEFIELD);
if (!p.getCounters().containsKey(CounterType.P1P1) || p.getCounters().getCount(CounterType.P1P1) == 0) {
game.getState().setValue(new StringBuilder("undying").append(getSourceId()).toString(), new FixedTarget(p.getId()));
return true;
}
}
@ -40,6 +47,73 @@ public class UndyingAbility extends DiesTriggeredAbility {
@Override
public String getRule() {
return "Undying";
return "Undying <i>(When this creature dies, if it had no +1/+1 counters on it, return it to the battlefield under its owner's control with a +1/+1 counter on it.)</i>";
}
}
class UndyingEffect extends OneShotEffect<UndyingEffect> {
public UndyingEffect() {
super(Outcome.Benefit);
this.staticText = "";
}
public UndyingEffect(final UndyingEffect effect) {
super(effect);
}
@Override
public UndyingEffect copy() {
return new UndyingEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
game.addEffect(new UndyingReplacementEffect(), source);
return false;
}
}
class UndyingReplacementEffect extends ReplacementEffectImpl<UndyingReplacementEffect> {
UndyingReplacementEffect() {
super(Duration.OneUse, Outcome.BoostCreature, false);
selfScope = true;
staticText = "return it to the battlefield under its owner's control with a +1/+1 counter on it";
}
UndyingReplacementEffect(final UndyingReplacementEffect effect) {
super(effect);
}
@Override
public UndyingReplacementEffect copy() {
return new UndyingReplacementEffect(this);
}
@Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) {
Permanent permanent = game.getPermanent(event.getTargetId());
if (permanent != null) {
permanent.addCounters(CounterType.P1P1.createInstance(), game);
}
used = true;
return false;
}
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
if (event.getType() == GameEvent.EventType.ENTERS_THE_BATTLEFIELD && event.getTargetId().equals(source.getSourceId())) {
Object fixedTarget = game.getState().getValue(new StringBuilder("undying").append(source.getSourceId()).toString());
if (fixedTarget instanceof FixedTarget && ((FixedTarget) fixedTarget).getFirst(game, source).equals(source.getSourceId())) {
return true;
}
}
return false;
}
@Override
public boolean apply(Game game, Ability source) {
return false;
}
}