From b15e97fa6fa8cfb48491ee5b6bd1a82d047249c3 Mon Sep 17 00:00:00 2001 From: Ingmar Goudt Date: Fri, 6 Aug 2021 23:08:37 +0200 Subject: [PATCH] fix #8073, Vial Smasher will not target invalid opponents --- .../mage/cards/v/VialSmasherTheFierce.java | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/Mage.Sets/src/mage/cards/v/VialSmasherTheFierce.java b/Mage.Sets/src/mage/cards/v/VialSmasherTheFierce.java index 4c842b5236..920a008321 100644 --- a/Mage.Sets/src/mage/cards/v/VialSmasherTheFierce.java +++ b/Mage.Sets/src/mage/cards/v/VialSmasherTheFierce.java @@ -20,13 +20,13 @@ import mage.game.permanent.Permanent; import mage.game.stack.Spell; import mage.players.Player; import mage.target.TargetPermanent; -import mage.util.RandomUtil; import mage.watchers.common.SpellsCastWatcher; -import java.util.Iterator; +import java.util.Collections; import java.util.List; -import java.util.Set; +import java.util.Objects; import java.util.UUID; +import java.util.stream.Collectors; /** * @author LevelX2 @@ -123,31 +123,34 @@ class VialSmasherTheFierceEffect extends OneShotEffect { if (controller != null) { int damage = (Integer) getValue("VialSmasherTheFierceCMC"); if (damage > 0) { - Set opponents = game.getOpponents(source.getControllerId()); - int random = RandomUtil.nextInt(opponents.size()); - Iterator iterator = opponents.iterator(); - for (int i = 0; i < random; i++) { - iterator.next(); + List opponents = game.getOpponents(source.getControllerId()) + .stream() + .map(game::getPlayer) + .filter(Objects::nonNull) + .filter(o -> !o.hasLost()) + .filter(o -> !o.hasLeft()) + .collect(Collectors.toList()); + if(opponents.isEmpty()){ + return false; } - UUID opponentId = iterator.next(); - Player opponent = game.getPlayer(opponentId); - if (opponent != null) { - game.informPlayers(opponent.getLogName() + " was chosen at random."); - if (game.getBattlefield().getAllActivePermanents(new FilterPlaneswalkerPermanent(), opponentId, game).size() > 0) { - if (controller.chooseUse(Outcome.Damage, "Redirect to a planeswalker controlled by " + opponent.getLogName() + "?", source, game)) { - FilterPlaneswalkerPermanent filter = new FilterPlaneswalkerPermanent("a planeswalker controlled by " + opponent.getLogName()); - filter.add(new ControllerIdPredicate(opponent.getId())); - TargetPermanent target = new TargetPermanent(1, 1, filter, false); - if (target.choose(Outcome.Damage, controller.getId(), source.getSourceId(), game)) { - Permanent permanent = game.getPermanent(target.getFirstTarget()); - if (permanent != null) { - return permanent.damage(damage, source.getSourceId(), source, game, false, true) > 0; - } + Collections.shuffle(opponents); + Player opponent = opponents.get(0); + game.informPlayers(opponent.getLogName() + " was chosen at random."); + if (game.getBattlefield().getAllActivePermanents(new FilterPlaneswalkerPermanent(), opponent.getId(), game).size() > 0) { + if (controller.chooseUse(Outcome.Damage, "Redirect to a planeswalker controlled by " + opponent.getLogName() + "?", source, game)) { + FilterPlaneswalkerPermanent filter = new FilterPlaneswalkerPermanent("a planeswalker controlled by " + opponent.getLogName()); + filter.add(new ControllerIdPredicate(opponent.getId())); + TargetPermanent target = new TargetPermanent(1, 1, filter, false); + if (target.choose(Outcome.Damage, controller.getId(), source.getSourceId(), game)) { + Permanent permanent = game.getPermanent(target.getFirstTarget()); + if (permanent != null) { + return permanent.damage(damage, source.getSourceId(), source, game, false, true) > 0; } } } - opponent.damage(damage, source.getSourceId(), source, game); } + opponent.damage(damage, source.getSourceId(), source, game); + } return true; }