From abc6b11c32e4350e8846963b454ebe3adfe00cc9 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Fri, 9 Oct 2015 16:30:28 +0200 Subject: [PATCH] * Mizzium Meddler - Fixed target change handling --- .../sets/magicorigins/MizziumMeddler.java | 106 +------------- .../src/mage/sets/newphyrexia/Spellskite.java | 120 +--------------- ...getOfTargetSpellAbilityToSourceEffect.java | 130 ++++++++++++++++++ 3 files changed, 136 insertions(+), 220 deletions(-) create mode 100644 Mage/src/mage/abilities/effects/common/ChangeATargetOfTargetSpellAbilityToSourceEffect.java diff --git a/Mage.Sets/src/mage/sets/magicorigins/MizziumMeddler.java b/Mage.Sets/src/mage/sets/magicorigins/MizziumMeddler.java index c0a7dc19ae..a59cd456d3 100644 --- a/Mage.Sets/src/mage/sets/magicorigins/MizziumMeddler.java +++ b/Mage.Sets/src/mage/sets/magicorigins/MizziumMeddler.java @@ -29,23 +29,14 @@ package mage.sets.magicorigins; import java.util.UUID; import mage.MageInt; -import mage.MageObject; import mage.abilities.Ability; import mage.abilities.common.EntersBattlefieldTriggeredAbility; -import mage.abilities.effects.OneShotEffect; +import mage.abilities.effects.common.ChangeATargetOfTargetSpellAbilityToSourceEffect; import mage.abilities.keyword.FlashAbility; import mage.cards.CardImpl; import mage.constants.CardType; -import mage.constants.Outcome; import mage.constants.Rarity; -import mage.game.Game; -import mage.game.stack.Spell; -import mage.game.stack.StackAbility; -import mage.game.stack.StackObject; -import mage.players.Player; -import mage.target.Target; import mage.target.TargetStackObject; -import mage.target.Targets; /** * @@ -63,9 +54,9 @@ public class MizziumMeddler extends CardImpl { // Flash this.addAbility(FlashAbility.getInstance()); - - // When Mizzium Meddler enters the battlefield, change a target of target spell or ability to Mizzium Meddler. - Ability ability = new EntersBattlefieldTriggeredAbility(new MizziumMeddlerEffect()); + + // When Mizzium Meddler enters the battlefield, you may change a target of target spell or ability to Mizzium Meddler. + Ability ability = new EntersBattlefieldTriggeredAbility(new ChangeATargetOfTargetSpellAbilityToSourceEffect(), true); ability.addTarget(new TargetStackObject()); this.addAbility(ability); } @@ -79,92 +70,3 @@ public class MizziumMeddler extends CardImpl { return new MizziumMeddler(this); } } - -class MizziumMeddlerEffect extends OneShotEffect { - - public MizziumMeddlerEffect() { - super(Outcome.Neutral); - staticText = "Change a target of target spell or ability to {this}"; - } - - public MizziumMeddlerEffect(final MizziumMeddlerEffect effect) { - super(effect); - } - - @Override - public boolean apply(Game game, Ability source) { - StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget()); - MageObject sourceObject = game.getObject(source.getSourceId()); - if (stackObject != null && sourceObject != null) { - Targets targets; - Ability sourceAbility; - MageObject oldTarget = null; - if (stackObject instanceof Spell) { - Spell spell = (Spell)stackObject; - sourceAbility = spell.getSpellAbility(); - targets = spell.getSpellAbility().getTargets(); - } else if (stackObject instanceof StackAbility) { - StackAbility stackAbility = (StackAbility)stackObject; - sourceAbility = stackAbility; - targets = stackAbility.getTargets(); - } else { - return false; - } - boolean twoTimesTarget = false; - if (targets.size() == 1 && targets.get(0).getTargets().size() == 1) { - Target target = targets.get(0); - if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) { - oldTarget = game.getObject(targets.getFirstTarget()); - target.clearChosen(); - // The source is still the spell on the stack - target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game); - } - } else { - Player player = game.getPlayer(source.getControllerId()); - for (Target target: targets) { - for (UUID targetId: target.getTargets()) { - MageObject object = game.getObject(targetId); - String name; - if (object == null) { - Player targetPlayer = game.getPlayer(targetId); - name = targetPlayer.getLogName(); - } else { - name = object.getName(); - } - if (!targetId.equals(source.getSourceId()) && target.getTargets().contains(source.getSourceId())) { - // you can't change this target to MizziumMeddler because MizziumMeddler is already another targetId of that target. - twoTimesTarget = true; - continue; - } - if (name != null && player.chooseUse(Outcome.Neutral, new StringBuilder("Change target from ").append(name).append(" to ").append(sourceObject.getName()).append("?").toString(), source, game)) { - if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) { - oldTarget = game.getObject(targets.getFirstTarget()); - target.remove(targetId); - // The source is still the spell on the stack - target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game); - break; - } - } - } - } - } - if (oldTarget != null) { - game.informPlayers(sourceObject.getLogName() + ": Changed target of " +stackObject.getLogName() + " from " + oldTarget.getLogName() + " to " + sourceObject.getLogName()); - } else { - if (twoTimesTarget) { - game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its not valid to target it twice for " + stackObject.getName()); - } else { - game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its no valid target for " + stackObject.getName()); - } - } - return true; - } - return false; - } - - @Override - public MizziumMeddlerEffect copy() { - return new MizziumMeddlerEffect(this); - } - -} \ No newline at end of file diff --git a/Mage.Sets/src/mage/sets/newphyrexia/Spellskite.java b/Mage.Sets/src/mage/sets/newphyrexia/Spellskite.java index ca7cfbd7e8..4e12742197 100644 --- a/Mage.Sets/src/mage/sets/newphyrexia/Spellskite.java +++ b/Mage.Sets/src/mage/sets/newphyrexia/Spellskite.java @@ -29,24 +29,15 @@ package mage.sets.newphyrexia; import java.util.UUID; import mage.MageInt; -import mage.MageObject; import mage.abilities.Ability; import mage.abilities.common.SimpleActivatedAbility; import mage.abilities.costs.mana.ManaCostsImpl; -import mage.abilities.effects.OneShotEffect; +import mage.abilities.effects.common.ChangeATargetOfTargetSpellAbilityToSourceEffect; import mage.cards.CardImpl; import mage.constants.CardType; -import mage.constants.Outcome; import mage.constants.Rarity; import mage.constants.Zone; -import mage.game.Game; -import mage.game.stack.Spell; -import mage.game.stack.StackAbility; -import mage.game.stack.StackObject; -import mage.players.Player; -import mage.target.Target; import mage.target.TargetStackObject; -import mage.target.Targets; /** * @@ -62,7 +53,7 @@ public class Spellskite extends CardImpl { this.toughness = new MageInt(4); // {UP}: Change a target of target spell or ability to Spellskite. - Ability ability = new SimpleActivatedAbility(Zone.BATTLEFIELD, new SpellskiteEffect(), new ManaCostsImpl("{UP}")); + Ability ability = new SimpleActivatedAbility(Zone.BATTLEFIELD, new ChangeATargetOfTargetSpellAbilityToSourceEffect(), new ManaCostsImpl("{UP}")); ability.addTarget(new TargetStackObject()); this.addAbility(ability); } @@ -76,110 +67,3 @@ public class Spellskite extends CardImpl { return new Spellskite(this); } } - -class SpellskiteEffect extends OneShotEffect { - - public SpellskiteEffect() { - super(Outcome.Neutral); - staticText = "Change a target of target spell or ability to {this}"; - } - - public SpellskiteEffect(final SpellskiteEffect effect) { - super(effect); - } - - @Override - public boolean apply(Game game, Ability source) { - StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget()); - MageObject sourceObject = game.getObject(source.getSourceId()); - if (stackObject != null && sourceObject != null) { - Targets targets = new Targets(); - Ability sourceAbility; - String oldTargetName = null; - if (stackObject instanceof Spell) { - Spell spell = (Spell) stackObject; - sourceAbility = spell.getSpellAbility(); - } else if (stackObject instanceof StackAbility) { - StackAbility stackAbility = (StackAbility) stackObject; - sourceAbility = stackAbility; - } else { - return false; - } - for (UUID modeId : sourceAbility.getModes().getSelectedModes()) { - sourceAbility.getModes().setActiveMode(modeId); - targets.addAll(sourceAbility.getTargets()); - } - - boolean twoTimesTarget = false; - if (targets.size() == 1 && targets.get(0).getTargets().size() == 1) { - Target target = targets.get(0); - if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) { - oldTargetName = getTargetName(targets.getFirstTarget(), game); - target.clearChosen(); - // The source is still the spell on the stack - target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game); - } - } else { - Player controller = game.getPlayer(source.getControllerId()); - boolean validTargets = false; - do { - for (Target target : targets) { - for (UUID targetId : target.getTargets()) { - String name = getTargetName(targets.getFirstTarget(), game); - if (!targetId.equals(source.getSourceId()) && target.getTargets().contains(source.getSourceId())) { - // you can't change this target to Spellskite because Spellskite is already another targetId of that target. - twoTimesTarget = true; - continue; - } - if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) { - validTargets = true; - if (name != null - && controller.chooseUse(Outcome.Neutral, "Change target from " + name + " to " + sourceObject.getLogName() + "?", source, game)) { - oldTargetName = getTargetName(targetId, game); - target.remove(targetId); - // The source is still the spell on the stack - target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game); - break; - } - } - } - if (oldTargetName != null) { - break; - } - } - if (oldTargetName == null) { - game.informPlayer(controller, "You have to select at least one target to change to spellskite!"); - } - } while (validTargets && oldTargetName == null); - } - if (oldTargetName != null) { - game.informPlayers(sourceObject.getLogName() + ": Changed target of " + stackObject.getLogName() + " from " + oldTargetName + " to " + sourceObject.getLogName()); - } else { - if (twoTimesTarget) { - game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its not valid to target it twice for " + stackObject.getLogName()); - } else { - game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its no valid target for " + stackObject.getLogName()); - } - } - return true; - } - return false; - } - - @Override - public SpellskiteEffect copy() { - return new SpellskiteEffect(this); - } - - private String getTargetName(UUID objectId, Game game) { - MageObject object = game.getObject(objectId); - if (object != null) { - return object.getLogName(); - } - Player player = game.getPlayer(objectId); - if (player != null) { - return player.getLogName(); - } - return null; - } -} diff --git a/Mage/src/mage/abilities/effects/common/ChangeATargetOfTargetSpellAbilityToSourceEffect.java b/Mage/src/mage/abilities/effects/common/ChangeATargetOfTargetSpellAbilityToSourceEffect.java new file mode 100644 index 0000000000..e660cd1967 --- /dev/null +++ b/Mage/src/mage/abilities/effects/common/ChangeATargetOfTargetSpellAbilityToSourceEffect.java @@ -0,0 +1,130 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package mage.abilities.effects.common; + +import java.util.UUID; +import mage.MageObject; +import mage.abilities.Ability; +import mage.abilities.effects.OneShotEffect; +import mage.constants.Outcome; +import mage.game.Game; +import mage.game.stack.Spell; +import mage.game.stack.StackAbility; +import mage.game.stack.StackObject; +import mage.players.Player; +import mage.target.Target; +import mage.target.Targets; + +/** + * + * @author LevelX2 + */ +public class ChangeATargetOfTargetSpellAbilityToSourceEffect extends OneShotEffect { + + public ChangeATargetOfTargetSpellAbilityToSourceEffect() { + super(Outcome.Neutral); + staticText = "Change a target of target spell or ability to {this}"; + } + + public ChangeATargetOfTargetSpellAbilityToSourceEffect(final ChangeATargetOfTargetSpellAbilityToSourceEffect effect) { + super(effect); + } + + @Override + public boolean apply(Game game, Ability source) { + StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget()); + MageObject sourceObject = game.getObject(source.getSourceId()); + if (stackObject != null && sourceObject != null) { + Targets targets = new Targets(); + Ability sourceAbility; + String oldTargetName = null; + if (stackObject instanceof Spell) { + Spell spell = (Spell) stackObject; + sourceAbility = spell.getSpellAbility(); + } else if (stackObject instanceof StackAbility) { + StackAbility stackAbility = (StackAbility) stackObject; + sourceAbility = stackAbility; + } else { + return false; + } + for (UUID modeId : sourceAbility.getModes().getSelectedModes()) { + sourceAbility.getModes().setActiveMode(modeId); + targets.addAll(sourceAbility.getTargets()); + } + + boolean twoTimesTarget = false; + if (targets.size() == 1 && targets.get(0).getTargets().size() == 1) { + Target target = targets.get(0); + if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) { + oldTargetName = getTargetName(targets.getFirstTarget(), game); + target.clearChosen(); + // The source is still the spell on the stack + target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game); + } + } else { + Player controller = game.getPlayer(source.getControllerId()); + boolean validTargets = false; + do { + for (Target target : targets) { + for (UUID targetId : target.getTargets()) { + String name = getTargetName(targets.getFirstTarget(), game); + if (!targetId.equals(source.getSourceId()) && target.getTargets().contains(source.getSourceId())) { + // you can't change this target to source because the source is already another targetId of that target. + twoTimesTarget = true; + continue; + } + if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) { + validTargets = true; + if (name != null + && controller.chooseUse(Outcome.Neutral, "Change target from " + name + " to " + sourceObject.getLogName() + "?", source, game)) { + oldTargetName = getTargetName(targetId, game); + target.remove(targetId); + // The source is still the spell on the stack + target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game); + break; + } + } + } + if (oldTargetName != null) { + break; + } + } + if (oldTargetName == null) { + game.informPlayer(controller, "You have to select at least one target to change to " + sourceObject.getIdName() + "!"); + } + } while (validTargets && oldTargetName == null); + } + if (oldTargetName != null) { + game.informPlayers(sourceObject.getLogName() + ": Changed target of " + stackObject.getLogName() + " from " + oldTargetName + " to " + sourceObject.getLogName()); + } else { + if (twoTimesTarget) { + game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its not valid to target it twice for " + stackObject.getLogName()); + } else { + game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its no valid target for " + stackObject.getLogName()); + } + } + return true; + } + return false; + } + + @Override + public ChangeATargetOfTargetSpellAbilityToSourceEffect copy() { + return new ChangeATargetOfTargetSpellAbilityToSourceEffect(this); + } + + private String getTargetName(UUID objectId, Game game) { + MageObject object = game.getObject(objectId); + if (object != null) { + return object.getLogName(); + } + Player player = game.getPlayer(objectId); + if (player != null) { + return player.getLogName(); + } + return null; + } +}