This commit is contained in:
jeffwadsworth 2021-02-14 01:02:58 -06:00
parent 7f6482221a
commit da03e7c50b

View file

@ -1,11 +1,12 @@
package mage.cards.m; package mage.cards.m;
import mage.MageItem; import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.effects.OneShotEffect; import mage.abilities.effects.OneShotEffect;
import mage.abilities.effects.ReplacementEffectImpl; import mage.abilities.effects.ReplacementEffectImpl;
import mage.abilities.keyword.ForetellAbility; import mage.abilities.keyword.ForetellAbility;
import mage.cards.Card;
import mage.cards.CardImpl; import mage.cards.CardImpl;
import mage.cards.CardSetInfo; import mage.cards.CardSetInfo;
import mage.constants.*; import mage.constants.*;
@ -15,15 +16,16 @@ import mage.filter.predicate.Predicates;
import mage.game.Game; import mage.game.Game;
import mage.game.events.EntersTheBattlefieldEvent; import mage.game.events.EntersTheBattlefieldEvent;
import mage.game.events.GameEvent; import mage.game.events.GameEvent;
import mage.game.events.ZoneChangeGroupEvent;
import mage.game.permanent.Permanent; import mage.game.permanent.Permanent;
import mage.target.TargetPermanent; import mage.target.TargetPermanent;
import mage.watchers.Watcher;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import mage.MageItem;
import mage.abilities.common.SimpleStaticAbility;
import mage.abilities.effects.ContinuousEffectImpl;
import mage.cards.Card;
import mage.game.events.ZoneChangeGroupEvent;
import mage.target.targetpointer.FixedTarget;
import mage.watchers.Watcher;
/** /**
* @author TheElk801 * @author TheElk801
@ -62,8 +64,8 @@ class MysticReflectionEffect extends OneShotEffect {
MysticReflectionEffect() { MysticReflectionEffect() {
super(Outcome.Benefit); super(Outcome.Benefit);
staticText = "Choose target nonlegendary creature. The next time one or more creatures or planeswalkers " + staticText = "Choose target nonlegendary creature. The next time one or more creatures or planeswalkers "
"enter the battlefield this turn, they enter as copies of the chosen creature."; + "enter the battlefield this turn, they enter as copies of the chosen creature.";
} }
private MysticReflectionEffect(final MysticReflectionEffect effect) { private MysticReflectionEffect(final MysticReflectionEffect effect) {
@ -77,31 +79,38 @@ class MysticReflectionEffect extends OneShotEffect {
@Override @Override
public boolean apply(Game game, Ability source) { public boolean apply(Game game, Ability source) {
Permanent permanent = game.getPermanent(source.getFirstTarget()); Permanent targetedPermanent = game.getPermanent(source.getFirstTarget());
// store the permanent that was targeted
game.getState().setValue("MysticReflection" + source.getSourceId().toString(), targetedPermanent);
MysticReflectionWatcher watcher = game.getState().getWatcher(MysticReflectionWatcher.class); MysticReflectionWatcher watcher = game.getState().getWatcher(MysticReflectionWatcher.class);
if (permanent == null || watcher == null) { // The sourceId must be sent to the next class, otherwise it gets lost. Thus, the identifier parameter.
return false; // Otherwise, the state of the permanent that was targeted gets lost if it leaves the battlefield, etc.
} // The zone is ALL because if the targeted permanent leaves the battlefield, the replacement effect still applies.
game.addEffect(new MysticReflectionCopyEffect(permanent, watcher.getEnteredThisTurn()), source); SimpleStaticAbility staticAbilityOnCard = new SimpleStaticAbility(Zone.ALL, new MysticReflectionReplacementEffect(watcher.getEnteredThisTurn(), source.getSourceId().toString()));
MysticReflectionGainAbilityEffect gainAbilityEffect = new MysticReflectionGainAbilityEffect(staticAbilityOnCard);
gainAbilityEffect.setTargetPointer(new FixedTarget(targetedPermanent.getMainCard().getId()));
game.addEffect(gainAbilityEffect, source);
return true; return true;
} }
} }
class MysticReflectionCopyEffect extends ReplacementEffectImpl { class MysticReflectionReplacementEffect extends ReplacementEffectImpl {
private final Permanent permanent; final private int enteredThisTurn;
private final int enteredThisTurn; final private String identifier;
MysticReflectionCopyEffect(Permanent permanent, int enteredThisTurn) { public MysticReflectionReplacementEffect(int enteredThisTurn, String identifier) {
super(Duration.Custom, Outcome.Copy, false); super(Duration.EndOfTurn, Outcome.Copy, false);
this.permanent = permanent;
this.enteredThisTurn = enteredThisTurn; this.enteredThisTurn = enteredThisTurn;
this.identifier = identifier;
staticText = "The next time one or more creatures or planeswalkers "
+ "enter the battlefield this turn, they enter as copies of {this}";
} }
private MysticReflectionCopyEffect(MysticReflectionCopyEffect effect) { public MysticReflectionReplacementEffect(MysticReflectionReplacementEffect effect) {
super(effect); super(effect);
this.permanent = effect.permanent;
this.enteredThisTurn = effect.enteredThisTurn; this.enteredThisTurn = effect.enteredThisTurn;
this.identifier = effect.identifier;
} }
@Override @Override
@ -111,34 +120,33 @@ class MysticReflectionCopyEffect extends ReplacementEffectImpl {
@Override @Override
public boolean applies(GameEvent event, Ability source, Game game) { public boolean applies(GameEvent event, Ability source, Game game) {
if (permanent == null) {
discard();
return false;
}
MysticReflectionWatcher watcher = game.getState().getWatcher(MysticReflectionWatcher.class); MysticReflectionWatcher watcher = game.getState().getWatcher(MysticReflectionWatcher.class);
if (watcher != null && watcher.getEnteredThisTurn() > this.enteredThisTurn) { if (watcher != null) {
discard(); if (watcher.getEnteredThisTurn() > this.enteredThisTurn) {
return false; discard();
return false;
}
} }
Permanent perm = ((EntersTheBattlefieldEvent) event).getTarget(); Permanent permanentEnteringTheBattlefield = ((EntersTheBattlefieldEvent) event).getTarget();
return perm != null Permanent targetedPermanent = (Permanent) game.getState().getValue("MysticReflection" + identifier);
&& (perm.isCreature() || perm.isPlaneswalker()) return permanentEnteringTheBattlefield != null
&& perm.isControlledBy(source.getControllerId()); && targetedPermanent != null
&& permanentEnteringTheBattlefield.isCreature();
} }
@Override @Override
public boolean replaceEvent(GameEvent event, Ability source, Game game) { public boolean replaceEvent(GameEvent event, Ability source, Game game) {
if (this.permanent != null) { Permanent targetedPermanent = (Permanent) game.getState().getValue("MysticReflection" + identifier);
game.copyPermanent(this.permanent, event.getTargetId(), source, null); if (targetedPermanent != null) {
game.copyPermanent(targetedPermanent, event.getTargetId(), source, null);
} }
return false; return false;
} }
@Override @Override
public MysticReflectionCopyEffect copy() { public MysticReflectionReplacementEffect copy() {
return new MysticReflectionCopyEffect(this); return new MysticReflectionReplacementEffect(this);
} }
} }
class MysticReflectionWatcher extends Watcher { class MysticReflectionWatcher extends Watcher {
@ -181,3 +189,39 @@ class MysticReflectionWatcher extends Watcher {
return enteredThisTurn; return enteredThisTurn;
} }
} }
class MysticReflectionGainAbilityEffect extends ContinuousEffectImpl {
final private Ability ability;
public MysticReflectionGainAbilityEffect(Ability ability) {
super(Duration.EndOfTurn, Layer.AbilityAddingRemovingEffects_6, SubLayer.NA, Outcome.AddAbility);
this.ability = ability;
}
public MysticReflectionGainAbilityEffect(final MysticReflectionGainAbilityEffect effect) {
super(effect);
this.ability = effect.ability;
}
@Override
public MysticReflectionGainAbilityEffect copy() {
return new MysticReflectionGainAbilityEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
Permanent targetedPermanent = (Permanent) game.getState().getValue("MysticReflection" + source.getSourceId().toString());
// The ability must be put on the card. If it leaves the battlefield, the replacement effect must still fire and copy its "permanent" state.
if (targetedPermanent == null) {
return false;
}
Card card = targetedPermanent.getMainCard();
if (card != null
&& !card.getAbilities().contains(ability)) {
game.getState().addOtherAbility(card, ability);
return true;
}
return false;
}
}